mirror of
https://github.com/balzack/databag.git
synced 2025-04-23 01:55:17 +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) {
|
||||
|
||||
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 dir = Platform.OS === 'ios' ? RNFS.DocumentDirectoryPath : RNFS.DownloadDirectoryPath;
|
||||
const sharePath = `${dir}/${name}.${type}`;
|
||||
if (await RNFS.exists(sharePath)) {
|
||||
await RNFS.unlink(sharePath);
|
||||
}
|
||||
|
||||
await RNFS.moveFile(downloadPath, sharePath);
|
||||
|
||||
if (Platform.OS === 'ios') {
|
||||
await Share.share({ url: sharePath });
|
||||
} else {
|
||||
await RNFS.scanFile(sharePath);
|
||||
}
|
||||
const options = { fileCache: true, filename: name };
|
||||
const download = await RNFetchBlob.config(options).fetch("GET", uri);
|
||||
const downloadPath = download.path();
|
||||
|
||||
await RNFS.unlink(sharePath);
|
||||
const type = extension ? extension : (await fileType(downloadPath))?.ext;
|
||||
|
||||
const sharePath = `${RNFS.DocumentDirectoryPath}/${name}.${type}`;
|
||||
if (await RNFS.exists(sharePath)) {
|
||||
await RNFS.unlink(sharePath);
|
||||
}
|
||||
await RNFS.moveFile(downloadPath, sharePath);
|
||||
|
||||
await Share.share({ url: sharePath });
|
||||
await RNFS.unlink(sharePath);
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user