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>
This commit is contained in:
1 parent
8d3062a5af
commit
e07af251c0
1 file changed
+35
@@ -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()
|
||||
}
|
||||
Reference in new issue
Block a user