Browse Source

fix(nc-gui): revise download attachment logic

pull/5046/head
Wing-Kam Wong 2 years ago
parent
commit
024643d2ea
  1. 12
      packages/nc-gui/components/cell/attachment/utils.ts

12
packages/nc-gui/components/cell/attachment/utils.ts

@ -12,6 +12,7 @@ import {
inject,
isImage,
message,
mimeTypes,
ref,
useApi,
useAttachment,
@ -230,21 +231,28 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState(
/** download a file */
async function downloadFile(item: AttachmentType) {
const src = getAttachmentSrc(item)
const mimeType = mimeTypes[item?.mimetype?.split('/')?.pop() || 'txt']
// test if the first source is accessible or not
await fetch(src)
.then((res) => {
if (!res.ok || res.headers.get('Content-Type') !== item.mimetype) {
// if not accessible or the content type doesn't match
// then throw the error to trigger the fallback source
if (!res.ok || res.headers.get('Content-Type') !== mimeType) {
throw new Error('Failed to download file')
}
// if accessible, then call blob()
res.blob()
})
.then(async (_) => {
// save the file from the first source
;(await import('file-saver')).saveAs(src, item.title)
})
.catch(async (_) => {
// try the fallback source with similar logic
const fallbackSrc = item.url!
await fetch(fallbackSrc)
.then((res) => {
if (!res.ok || res.headers.get('Content-Type') !== item.mimetype) {
if (!res.ok || res.headers.get('Content-Type') !== mimeType) {
throw new Error('Failed to download file')
}
res.blob()

Loading…
Cancel
Save