Browse Source

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 <cursoragent@cursor.com>
wuzilong 2 months ago
parent
commit
e07af251c0
1 changed files with 35 additions and 0 deletions
  1. 35 0
      web-admin/src/utils/download-attachment.ts

+ 35 - 0
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<Blob> {
+  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()
+}