-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
DownloadUtils.ts
73 lines (63 loc) · 2.92 KB
/
DownloadUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import * as ApiUtils from '@libs/ApiUtils';
import tryResolveUrlFromApiRoot from '@libs/tryResolveUrlFromApiRoot';
import * as Link from '@userActions/Link';
import CONST from '@src/CONST';
import * as FileUtils from './FileUtils';
import type {FileDownload} from './types';
const createDownloadLink = (href: string, fileName: string) => {
// creating anchor tag to initiate download
const link = document.createElement('a');
// adding href to anchor
link.href = href;
link.style.display = 'none';
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- Disabling this line for safeness as nullish coalescing works only if the value is undefined or null, and since fileName can be an empty string we want to default to `FileUtils.getFileName(url)`
link.download = fileName;
// Append to html link element page
document.body.appendChild(link);
// Start download
link.click();
// Clean up and remove the link
URL.revokeObjectURL(link.href);
link.parentNode?.removeChild(link);
};
/**
* The function downloads an attachment on web/desktop platforms.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const fetchFileDownload: FileDownload = (url, fileName, successMessage = '', shouldOpenExternalLink = false, formData = undefined, requestType = 'get', onDownloadFailed?: () => void) => {
const resolvedUrl = tryResolveUrlFromApiRoot(url);
const isApiUrl = resolvedUrl.startsWith(ApiUtils.getApiRoot());
const isAttachmentUrl = CONST.ATTACHMENT_LOCAL_URL_PREFIX.some((prefix) => resolvedUrl.startsWith(prefix));
const isSageUrl = url === CONST.EXPENSIFY_PACKAGE_FOR_SAGE_INTACCT;
if (
// We have two file download cases that we should allow: 1. downloading attachments 2. downloading Expensify package for Sage Intacct
shouldOpenExternalLink ||
(!isApiUrl && !isAttachmentUrl && !isSageUrl)
) {
// Different origin URLs might pose a CORS issue during direct downloads.
// Opening in a new tab avoids this limitation, letting the browser handle the download.
Link.openExternalLink(url);
return Promise.resolve();
}
const fetchOptions: RequestInit = {
method: requestType,
body: formData,
};
return fetch(url, fetchOptions)
.then((response) => response.blob())
.then((blob) => {
// Create blob link to download
const href = URL.createObjectURL(new Blob([blob]));
const completeFileName = FileUtils.appendTimeToFileName(fileName ?? FileUtils.getFileName(url));
createDownloadLink(href, completeFileName);
})
.catch(() => {
if (onDownloadFailed) {
onDownloadFailed();
} else {
// file could not be downloaded, open sourceURL in new tab
Link.openExternalLink(url);
}
});
};
export default fetchFileDownload;