|
|
@ -12,6 +12,7 @@ import { |
|
|
|
inject, |
|
|
|
inject, |
|
|
|
isImage, |
|
|
|
isImage, |
|
|
|
message, |
|
|
|
message, |
|
|
|
|
|
|
|
mimeTypes, |
|
|
|
ref, |
|
|
|
ref, |
|
|
|
useApi, |
|
|
|
useApi, |
|
|
|
useAttachment, |
|
|
|
useAttachment, |
|
|
@ -230,21 +231,28 @@ 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 = getAttachmentSrc(item) |
|
|
|
|
|
|
|
const mimeType = mimeTypes[item?.mimetype?.split('/')?.pop() || 'txt'] |
|
|
|
|
|
|
|
// test if the first source is accessible or not
|
|
|
|
await fetch(src) |
|
|
|
await fetch(src) |
|
|
|
.then((res) => { |
|
|
|
.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') |
|
|
|
throw new Error('Failed to download file') |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// if accessible, then call blob()
|
|
|
|
res.blob() |
|
|
|
res.blob() |
|
|
|
}) |
|
|
|
}) |
|
|
|
.then(async (_) => { |
|
|
|
.then(async (_) => { |
|
|
|
|
|
|
|
// save the file from the first source
|
|
|
|
;(await import('file-saver')).saveAs(src, item.title) |
|
|
|
;(await import('file-saver')).saveAs(src, item.title) |
|
|
|
}) |
|
|
|
}) |
|
|
|
.catch(async (_) => { |
|
|
|
.catch(async (_) => { |
|
|
|
|
|
|
|
// try the fallback source with similar logic
|
|
|
|
const fallbackSrc = item.url! |
|
|
|
const fallbackSrc = item.url! |
|
|
|
await fetch(fallbackSrc) |
|
|
|
await fetch(fallbackSrc) |
|
|
|
.then((res) => { |
|
|
|
.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') |
|
|
|
throw new Error('Failed to download file') |
|
|
|
} |
|
|
|
} |
|
|
|
res.blob() |
|
|
|
res.blob() |
|
|
|