From e07af251c08b0c9e8bd807adbe9061aecd3368e1 Mon Sep 17 00:00:00 2001 From: wuzilong <704541015@qq.com> Date: Tue, 8 Sep 2026 20:21:54 +0800 Subject: [PATCH] feat(web-admin): add fetchAttachmentBlob for zip packing. Reuse attachment download-url flow to load file bytes without triggering anchor download. Co-authored-by: Cursor --- web-admin/src/utils/download-attachment.ts | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/web-admin/src/utils/download-attachment.ts b/web-admin/src/utils/download-attachment.ts index 7bd57a74f..3121694db 100644 --- a/web-admin/src/utils/download-attachment.ts +++ b/web-admin/src/utils/download-attachment.ts @@ -182,3 +182,38 @@ export async function downloadAttachmentFile( } triggerAnchorDownload(href, fileName) } + +/** 附件内容:预签名 URL → Blob(供 ZIP 打包);预签名失败时回退直连。 */ +export async function fetchAttachmentBlob( + file: AttachmentDownloadItem, + options?: { fullFileUrl?: (url: string) => string } +): Promise { + const fileUrl = String(file.fileUrl || '').trim() + if (!fileUrl) { + throw new Error('empty fileUrl') + } + const fileName = String(file.fileName || '').trim() || fileUrl.split('/').pop() || 'download' + + let href = '' + try { + const res = await fetchAttachmentDownloadUrl({ fileUrl, fileName }) + if (res?.downloadUrl) href = res.downloadUrl + } catch { + // fall through + } + + if (!href) { + const resolveUrl = options?.fullFileUrl || ((url: string) => url) + href = resolveUrl(fileUrl) + } + + if (!href) { + throw new Error('无法获取文件下载地址') + } + + const resp = await fetch(href) + if (!resp.ok) { + throw new Error(`download failed: ${resp.status}`) + } + return resp.blob() +}