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 () => { const download = async () => {
try { try {
const url = asset.encrypted ? `file://${asset.decrypted}` : asset.data; const ext = asset.extension.toLowerCase();
await actions.download(asset.label, asset.extension, url); const url = asset.encrypted ? asset.decrypted : asset.data;
await actions.download(asset.label, ext, asset.encrypted, url);
} }
catch (err) { catch (err) {
Alert.alert( Alert.alert(

View File

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