mirror of
https://github.com/balzack/databag.git
synced 2025-05-04 23:45:21 +00:00
isolating platform specific download code
This commit is contained in:
parent
370e6ca8de
commit
1dab533bdd
@ -5,25 +5,42 @@ import RNFetchBlob from 'rn-fetch-blob';
|
|||||||
|
|
||||||
export async function Download(uri: string, name: string, extension?: string) {
|
export async function Download(uri: string, name: string, extension?: string) {
|
||||||
|
|
||||||
|
if (Platform.OS === 'ios') {
|
||||||
const options = { fileCache: true, filename: name };
|
const options = { fileCache: true, filename: name };
|
||||||
const download = await RNFetchBlob.config(options).fetch("GET", uri);
|
const download = await RNFetchBlob.config(options).fetch("GET", uri);
|
||||||
const downloadPath = download.path();
|
const downloadPath = download.path();
|
||||||
|
|
||||||
const type = extension ? extension : (await fileType(downloadPath))?.ext;
|
const type = extension ? extension : (await fileType(downloadPath))?.ext;
|
||||||
|
|
||||||
const dir = Platform.OS === 'ios' ? RNFS.DocumentDirectoryPath : RNFS.DownloadDirectoryPath;
|
const sharePath = `${RNFS.DocumentDirectoryPath}/${name}.${type}`;
|
||||||
const sharePath = `${dir}/${name}.${type}`;
|
|
||||||
if (await RNFS.exists(sharePath)) {
|
if (await RNFS.exists(sharePath)) {
|
||||||
await RNFS.unlink(sharePath);
|
await RNFS.unlink(sharePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
await RNFS.moveFile(downloadPath, sharePath);
|
await RNFS.moveFile(downloadPath, sharePath);
|
||||||
|
|
||||||
if (Platform.OS === 'ios') {
|
|
||||||
await Share.share({ url: sharePath });
|
await Share.share({ url: sharePath });
|
||||||
|
await RNFS.unlink(sharePath);
|
||||||
} else {
|
} else {
|
||||||
|
if (uri.startsWith('file:')) {
|
||||||
|
const type = extension ? extension : (await fileType(uri))?.ext;
|
||||||
|
const sharePath = `${RNFS.DownloadDirectoryPath}/${name}.${type}`;
|
||||||
|
if (await RNFS.exists(sharePath)) {
|
||||||
|
await RNFS.unlink(sharePath);
|
||||||
|
}
|
||||||
|
await RNFS.copyFile(uri, sharePath);
|
||||||
|
await RNFS.scanFile(sharePath);
|
||||||
|
} else {
|
||||||
|
const options = { fileCache: true, filename: name };
|
||||||
|
const download = await RNFetchBlob.config(options).fetch("GET", uri);
|
||||||
|
const downloadPath = download.path();
|
||||||
|
|
||||||
|
const type = extension ? extension : (await fileType(downloadPath))?.ext;
|
||||||
|
const sharePath = `${RNFS.DownloadDirectoryPath}/${name}.${type}`;
|
||||||
|
if (await RNFS.exists(sharePath)) {
|
||||||
|
await RNFS.unlink(sharePath);
|
||||||
|
}
|
||||||
|
await RNFS.moveFile(downloadPath, sharePath);
|
||||||
await RNFS.scanFile(sharePath);
|
await RNFS.scanFile(sharePath);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
await RNFS.unlink(sharePath);
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user