|
|
@ -12,7 +12,6 @@ import { |
|
|
|
inject, |
|
|
|
inject, |
|
|
|
isImage, |
|
|
|
isImage, |
|
|
|
message, |
|
|
|
message, |
|
|
|
mimeTypes, |
|
|
|
|
|
|
|
ref, |
|
|
|
ref, |
|
|
|
useApi, |
|
|
|
useApi, |
|
|
|
useAttachment, |
|
|
|
useAttachment, |
|
|
@ -230,40 +229,12 @@ export const [useProvideAttachmentCell, useAttachmentCell] = useInjectionState( |
|
|
|
|
|
|
|
|
|
|
|
/** download a file */ |
|
|
|
/** download a file */ |
|
|
|
async function downloadFile(item: AttachmentType) { |
|
|
|
async function downloadFile(item: AttachmentType) { |
|
|
|
const src = getAttachmentSrc(item) |
|
|
|
const src = await getAttachmentSrc(item) |
|
|
|
const mimeType = mimeTypes[item?.mimetype?.split('/')?.pop() || 'txt'] |
|
|
|
if (src) { |
|
|
|
// test if the first source is accessible or not
|
|
|
|
;(await import('file-saver')).saveAs(src, item.title) |
|
|
|
await fetch(src) |
|
|
|
} else { |
|
|
|
.then((res) => { |
|
|
|
message.error('Failed to download file') |
|
|
|
// 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') !== 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) => { |
|
|
|
const FileIcon = (icon: string) => { |
|
|
|