-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathindex.android.js
116 lines (103 loc) · 3.99 KB
/
index.android.js
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
110
111
112
113
114
115
116
import {PermissionsAndroid, Platform} from 'react-native';
import RNFetchBlob from 'react-native-blob-util';
import * as FileUtils from './FileUtils';
/**
* Android permission check to store images
* @returns {Promise<Boolean>}
*/
function hasAndroidPermission() {
// On Android API Level 33 and above, these permissions do nothing and always return 'never_ask_again'
// More info here: https://stackoverflow.com/a/74296799
if (Platform.Version >= 33) {
return Promise.resolve(true);
}
// Read and write permission
const writePromise = PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE);
const readPromise = PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE);
return Promise.all([writePromise, readPromise]).then(([hasWritePermission, hasReadPermission]) => {
if (hasWritePermission && hasReadPermission) {
return true; // Return true if permission is already given
}
// Ask for permission if not given
return PermissionsAndroid.requestMultiple([PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE, PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE]).then(
(status) => status['android.permission.READ_EXTERNAL_STORAGE'] === 'granted' && status['android.permission.WRITE_EXTERNAL_STORAGE'] === 'granted',
);
});
}
/**
* Handling the download
* @param {String} url
* @param {String} fileName
* @returns {Promise<Void>}
*/
function handleDownload(url, fileName) {
return new Promise((resolve) => {
const dirs = RNFetchBlob.fs.dirs;
// Android files will download to Download directory
const path = dirs.DownloadDir;
const attachmentName = FileUtils.appendTimeToFileName(fileName) || FileUtils.getAttachmentName(url);
const isLocalFile = url.startsWith('file://');
let attachmentPath = isLocalFile ? url : undefined;
let fetchedAttachment = Promise.resolve();
if (!isLocalFile) {
// Fetching the attachment
fetchedAttachment = RNFetchBlob.config({
fileCache: true,
path: `${path}/${attachmentName}`,
addAndroidDownloads: {
useDownloadManager: true,
notification: false,
path: `${path}/Expensify/${attachmentName}`,
},
}).fetch('GET', url);
}
// Resolving the fetched attachment
fetchedAttachment
.then((attachment) => {
if (!isLocalFile && (!attachment || !attachment.info())) {
return Promise.reject();
}
if (!isLocalFile) {
attachmentPath = attachment.path();
}
return RNFetchBlob.MediaCollection.copyToMediaStore(
{
name: attachmentName,
parentFolder: 'Expensify',
mimeType: null,
},
'Download',
attachmentPath,
);
})
.then(() => {
RNFetchBlob.fs.unlink(attachmentPath);
FileUtils.showSuccessAlert();
})
.catch(() => {
FileUtils.showGeneralErrorAlert();
})
.finally(() => resolve());
});
}
/**
* Checks permission and downloads the file for Android
* @param {String} url
* @param {String} fileName
* @returns {Promise<Void>}
*/
export default function fileDownload(url, fileName) {
return new Promise((resolve) => {
hasAndroidPermission()
.then((hasPermission) => {
if (hasPermission) {
return handleDownload(url, fileName);
}
FileUtils.showPermissionErrorAlert();
})
.catch(() => {
FileUtils.showPermissionErrorAlert();
})
.finally(() => resolve());
});
}