import { useEffect, useState } from 'preact/hooks'; import { Download, Eye, Lock } from 'lucide-preact'; import { accessPublicSend, accessPublicSendFile, decryptPublicSend, decryptPublicSendFileBytes } from '@/lib/api/send'; import StandalonePageFrame from '@/components/StandalonePageFrame'; import { t } from '@/lib/i18n'; interface PublicSendPageProps { accessId: string; keyPart: string | null; } export default function PublicSendPage(props: PublicSendPageProps) { const [loading, setLoading] = useState(true); const [password, setPassword] = useState(''); const [needPassword, setNeedPassword] = useState(false); const [error, setError] = useState(''); const [sendData, setSendData] = useState(null); const [busy, setBusy] = useState(false); async function loadSend(pass?: string): Promise { setBusy(true); setError(''); try { const data = await accessPublicSend(props.accessId, props.keyPart, pass); if (!props.keyPart) { setError(t('txt_this_link_is_missing_decryption_key')); setSendData(null); return; } const decrypted = await decryptPublicSend(data, props.keyPart); setSendData(decrypted); setNeedPassword(false); } catch (e) { const err = e as Error & { status?: number }; if (err.status === 401) { setNeedPassword(true); setError(t('txt_this_send_is_password_protected')); } else { setError(err.message || t('txt_failed_to_open_send')); } setSendData(null); } finally { setBusy(false); setLoading(false); } } async function downloadFile(): Promise { if (!sendData?.id || !sendData?.file?.id) return; setBusy(true); setError(''); try { const url = await accessPublicSendFile(sendData.id, sendData.file.id, props.keyPart, password || undefined); const resp = await fetch(url); if (!resp.ok) throw new Error(t('txt_download_failed')); const encryptedBytes = await resp.arrayBuffer(); let blob: Blob; if (props.keyPart) { try { const decryptedBytes = await decryptPublicSendFileBytes(encryptedBytes, props.keyPart); blob = new Blob([decryptedBytes as unknown as BlobPart], { type: 'application/octet-stream' }); } catch { // Legacy compatibility: early web-created file sends uploaded plaintext bytes. blob = new Blob([encryptedBytes], { type: 'application/octet-stream' }); } } else { blob = new Blob([encryptedBytes], { type: 'application/octet-stream' }); } const obj = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = obj; a.download = sendData.decFileName || sendData.file?.fileName || t('txt_send_file'); document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(obj); } catch (e) { const err = e as Error; setError(err.message || t('txt_download_failed')); } finally { setBusy(false); } } useEffect(() => { void loadSend(); }, [props.accessId, props.keyPart]); return (
{loading &&

{t('txt_loading')}

} {!loading && needPassword && (
{ e.preventDefault(); void loadSend(password); }} >
)} {!loading && sendData && ( <>

{sendData.decName || t('txt_no_name')}

{sendData.type === 0 ? (
{sendData.decText || ''}
) : (
{t('txt_file')} {sendData.decFileName || sendData.file?.fileName || sendData.file?.sizeName || t('txt_encrypted_file')}
)} {!!sendData.expirationDate &&

{t('txt_expires_at_value', { value: sendData.expirationDate })}

} )} {!loading && !sendData && !needPassword && !error && (

{t('txt_send_unavailable')}

)} {!!error &&

{error}

}
); }