using share for download storage

This commit is contained in:
balzack 2023-08-12 22:02:22 -07:00
parent f2919bface
commit e59f025e5a
2 changed files with 22 additions and 25 deletions

View File

@ -13,8 +13,9 @@ export function BinaryAsset({ asset, dismiss }) {
const download = async () => {
try {
const url = asset.encrypted ? `file://${asset.decrypted}` : asset.data;
await actions.download(asset.label, asset.extension, url);
const ext = asset.extension.toLowerCase();
const url = asset.encrypted ? asset.decrypted : asset.data;
await actions.download(asset.label, ext, asset.encrypted, url);
}
catch (err) {
Alert.alert(

View File

@ -31,14 +31,20 @@ export function useBinaryAsset() {
}, [dimensions]);
const actions = {
download: async (label, extension, url) => {
download: async (label, extension, cached, url) => {
if (!state.downloading) {
try {
updateState({ downloading: true });
let src;
if (cached) {
src = url
}
else {
const blob = await RNFetchBlob.config({ fileCache: true }).fetch("GET", url);
const src = blob.path();
if (Platform.OS === 'ios') {
src = blob.path();
}
const path = `${RNFetchBlob.fs.dirs.DocumentDir}`
const dst = `${path}/${label}.${extension.toLowerCase()}`
if (RNFetchBlob.fs.exists(dst)) {
@ -52,16 +58,6 @@ export function useBinaryAsset() {
console.log(err);
}
RNFetchBlob.fs.unlink(dst);
}
else {
const path = `${RNFetchBlob.fs.dirs.DownloadDir}`
const dst = `${path}/${label}.${extension.toLowerCase()}`
if (RNFetchBlob.fs.exists(dst)) {
RNFetchBlob.fs.unlink(dst);
}
await RNFetchBlob.fs.mv(src, dst);
RNFetchBlob.fs.unlink(dst);
}
updateState({ downloading: false });
}