feat(web): use blob download flow in chat info attachment menu
Some checks failed
CI / test (push) Has been cancelled
Some checks failed
CI / test (push) Has been cancelled
This commit is contained in:
@@ -961,9 +961,22 @@ export function ChatInfoPanel({ chatId, open, onClose }: Props) {
|
||||
<a className="block rounded px-2 py-1.5 text-sm hover:bg-slate-800" href={attachmentCtx.url} rel="noreferrer" target="_blank">
|
||||
Open
|
||||
</a>
|
||||
<a className="block rounded px-2 py-1.5 text-sm hover:bg-slate-800" href={attachmentCtx.url} download>
|
||||
<button
|
||||
className="block w-full rounded px-2 py-1.5 text-left text-sm hover:bg-slate-800"
|
||||
onClick={async () => {
|
||||
try {
|
||||
await downloadFileFromUrl(attachmentCtx.url);
|
||||
showToast("File downloaded");
|
||||
} catch {
|
||||
showToast("Download failed");
|
||||
} finally {
|
||||
setAttachmentCtx(null);
|
||||
}
|
||||
}}
|
||||
type="button"
|
||||
>
|
||||
Download
|
||||
</a>
|
||||
</button>
|
||||
<button
|
||||
className="block w-full rounded px-2 py-1.5 text-left text-sm hover:bg-slate-800"
|
||||
onClick={async () => {
|
||||
@@ -1299,3 +1312,20 @@ function shortLink(url: string): string {
|
||||
return url;
|
||||
}
|
||||
}
|
||||
|
||||
async function downloadFileFromUrl(url: string): Promise<void> {
|
||||
const response = await fetch(url, { mode: "cors" });
|
||||
if (!response.ok) {
|
||||
throw new Error("Download failed");
|
||||
}
|
||||
const blob = await response.blob();
|
||||
const filename = extractFileName(url);
|
||||
const blobUrl = window.URL.createObjectURL(blob);
|
||||
const link = document.createElement("a");
|
||||
link.href = blobUrl;
|
||||
link.download = filename;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
link.remove();
|
||||
window.URL.revokeObjectURL(blobUrl);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user