fix: attachment download - remote opens in new tab (sync, no CORS fetch), local downloads get toast feedback

This commit is contained in:
root
2026-10-17 11:02:53 +08:00
parent cf4647f706
commit 6d686c1d69
12 changed files with 54 additions and 32 deletions
@@ -1,6 +1,6 @@
import { useEffect, useState } from 'react';
import { Download, Loader2 } from 'lucide-react';
import { Button, Dialog } from '@/components/ui';
import { Button, Dialog, useToast } from '@/components/ui';
import { readFull } from '@/lib/att-cache';
import type { Attachment } from '@/lib/types';
@@ -38,6 +38,7 @@ const KIND_LABEL: Record<AttKind, string> = {
};
export function AttachmentPreview({ att, onClose }: { att: Attachment | null; onClose: () => void }) {
const toast = useToast();
const [loading, setLoading] = useState(false);
const [error, setError] = useState('');
const [content, setContent] = useState<{ kind: AttKind; objectUrl?: string; html?: string; text?: string } | null>(null);
@@ -107,7 +108,28 @@ export function AttachmentPreview({ att, onClose }: { att: Attachment | null; on
title={(att ? '附件预览 · ' + att.file_name : '附件预览') + (att ? '(' + KIND_LABEL[kind] + ')' : '')}
footer={att ? (
<>
<Button variant="secondary" className="mr-auto" onClick={() => { void readFull(att).then(r => forceDownload(att.file_name, att.mime ?? undefined, r.data_b64)); }}>
<Button variant="secondary" className="mr-auto" onClick={() => {
// 远程附件:同步新标签打开(用户手势内触发,不会被拦截;右键即可另存)
if (att.remote_url) {
const a = document.createElement('a');
a.href = att.remote_url;
a.target = '_blank';
a.rel = 'noopener';
a.click();
toast('success', '已在新的标签页打开,右键图片可「另存为」');
return;
}
// 本地附件:base64 下载,失败给出提示
void (async () => {
try {
toast('success', '已开始下载');
const r = await readFull(att);
forceDownload(att.file_name, att.mime ?? undefined, r.data_b64);
} catch (e) {
toast('error', '下载失败:' + (e as Error).message);
}
})();
}}>
<Download size={13} /> 下载
</Button>
<Button variant="primary" onClick={onClose}>关闭</Button>