From e1797af0b48bfaec3feb3a0dbb5f1f4787690bb6 Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Tue, 7 Feb 2023 14:07:10 +0800 Subject: [PATCH] fix(nc-gui): revise download attachment logic --- .../components/cell/attachment/utils.ts | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/nc-gui/components/cell/attachment/utils.ts b/packages/nc-gui/components/cell/attachment/utils.ts index d9e0ed6ea9..b99c82c383 100644 --- a/packages/nc-gui/components/cell/attachment/utils.ts +++ b/packages/nc-gui/components/cell/attachment/utils.ts @@ -14,6 +14,7 @@ import { message, ref, useApi, + useAttachment, useFileDialog, useI18n, useInjectionState, @@ -60,6 +61,8 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState( const { t } = useI18n() + const { getAttachmentSrc } = useAttachment() + const defaultAttachmentMeta = { ...(appInfo.value.ee && { // Maximum Number of Attachments per cell @@ -226,7 +229,33 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState( /** download a file */ async function downloadFile(item: AttachmentType) { - ;(await import('file-saver')).saveAs(item.url || item.data, item.title) + const src = getAttachmentSrc(item) + await fetch(src) + .then((res) => { + if (!res.ok || res.headers.get('Content-Type') !== item.mimetype) { + throw new Error('Failed to download file') + } + res.blob() + }) + .then(async (_) => { + ;(await import('file-saver')).saveAs(src, item.title) + }) + .catch(async (_) => { + const fallbackSrc = item.url! + await fetch(fallbackSrc) + .then((res) => { + if (!res.ok || res.headers.get('Content-Type') !== item.mimetype) { + throw new Error('Failed to download file') + } + res.blob() + }) + .then(async (_) => { + ;(await import('file-saver')).saveAs(fallbackSrc, item.title) + }) + .catch(async (e) => { + message.error(e.message) + }) + }) } const FileIcon = (icon: string) => {