forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ios.ts
109 lines (98 loc) · 4 KB
/
index.ios.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import {CameraRoll} from '@react-native-camera-roll/camera-roll';
import type {PhotoIdentifier} from '@react-native-camera-roll/camera-roll';
import RNFetchBlob from 'react-native-blob-util';
import CONST from '@src/CONST';
import * as FileUtils from './FileUtils';
import type {FileDownload} from './types';
/**
* Downloads the file to Documents section in iOS
*/
function downloadFile(fileUrl: string, fileName: string) {
const dirs = RNFetchBlob.fs.dirs;
// The iOS files will download to documents directory
const path = dirs.DocumentDir;
// Fetching the attachment
return RNFetchBlob.config({
fileCache: true,
path: `${path}/${fileName}`,
addAndroidDownloads: {
useDownloadManager: true,
notification: true,
path: `${path}/Expensify/${fileName}`,
},
}).fetch('GET', fileUrl);
}
/**
* Download the image to photo lib in iOS
*/
function downloadImage(fileUrl: string) {
return CameraRoll.saveAsset(fileUrl);
}
/**
* Download the video to photo lib in iOS
*/
function downloadVideo(fileUrl: string, fileName: string): Promise<PhotoIdentifier> {
return new Promise((resolve, reject) => {
let documentPathUri: string | null = null;
let cameraRollAsset: PhotoIdentifier;
// Because CameraRoll doesn't allow direct downloads of video with remote URIs, we first download as documents, then copy to photo lib and unlink the original file.
downloadFile(fileUrl, fileName)
.then((attachment) => {
documentPathUri = attachment.data;
if (!documentPathUri) {
throw new Error('Error downloading video');
}
return CameraRoll.saveAsset(documentPathUri);
})
.then((attachment) => {
cameraRollAsset = attachment;
if (!documentPathUri) {
throw new Error('Error downloading video');
}
return RNFetchBlob.fs.unlink(documentPathUri);
})
.then(() => {
resolve(cameraRollAsset);
})
.catch((err) => reject(err));
});
}
/**
* Download the file based on type(image, video, other file types)for iOS
*/
const fileDownload: FileDownload = (fileUrl, fileName, successMessage) =>
new Promise((resolve) => {
let fileDownloadPromise;
const fileType = FileUtils.getFileType(fileUrl);
// 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)`
const attachmentName = FileUtils.appendTimeToFileName(fileName || FileUtils.getFileName(fileUrl));
switch (fileType) {
case CONST.ATTACHMENT_FILE_TYPE.IMAGE:
fileDownloadPromise = downloadImage(fileUrl);
break;
case CONST.ATTACHMENT_FILE_TYPE.VIDEO:
fileDownloadPromise = downloadVideo(fileUrl, attachmentName);
break;
default:
fileDownloadPromise = downloadFile(fileUrl, attachmentName);
break;
}
fileDownloadPromise
.then((attachment) => {
if (!attachment) {
return;
}
FileUtils.showSuccessAlert(successMessage);
})
.catch((err: Error) => {
// iOS shows permission popup only once. Subsequent request will only throw an error.
// We catch the error and show a redirection link to the settings screen
if (err.message === CONST.IOS_CAMERAROLL_ACCESS_ERROR) {
FileUtils.showPermissionErrorAlert();
} else {
FileUtils.showGeneralErrorAlert();
}
})
.finally(() => resolve());
});
export default fileDownload;