From 1dab533bddb4c79b776f4e5e2da0086bbbc41e60 Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Sat, 18 Jan 2025 16:48:34 -0800 Subject: [PATCH] isolating platform specific download code --- app/client/mobile/src/download.ts | 55 ++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/app/client/mobile/src/download.ts b/app/client/mobile/src/download.ts index ac245b97..a7f5cbbc 100644 --- a/app/client/mobile/src/download.ts +++ b/app/client/mobile/src/download.ts @@ -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); + } + } }