isolating platform specific download code

This commit is contained in:
Roland Osborne 2025-01-18 16:48:34 -08:00
parent 370e6ca8de
commit 1dab533bdd

View File

@ -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);
}
}
}