change rn-fetch-blob to expo-filesystem

This commit is contained in:
Reinaldo Neto 2023-05-19 17:56:16 -03:00
parent 74e6e8fd8b
commit 4dcb12876b
1 changed files with 12 additions and 31 deletions

View File

@ -1,7 +1,6 @@
import * as FileSystem from 'expo-file-system'; import * as FileSystem from 'expo-file-system';
import * as mime from 'react-native-mime-types'; import * as mime from 'react-native-mime-types';
import { isEmpty } from 'lodash'; import { isEmpty } from 'lodash';
import RNFetchBlob, { FetchBlobResponse, StatefulPromise } from 'rn-fetch-blob';
import { sanitizeLikeString } from '../database/utils'; import { sanitizeLikeString } from '../database/utils';
import { store } from '../store/auxStore'; import { store } from '../store/auxStore';
@ -24,7 +23,7 @@ const defaultType = {
[MediaTypes.video]: 'mp4' [MediaTypes.video]: 'mp4'
}; };
const downloadQueue: { [index: string]: StatefulPromise<FetchBlobResponse> } = {}; const downloadQueue: { [index: string]: FileSystem.DownloadResumable } = {};
export const mediaDownloadKey = (mediaType: MediaTypes, messageId: string) => `${mediaType}-${messageId}`; export const mediaDownloadKey = (mediaType: MediaTypes, messageId: string) => `${mediaType}-${messageId}`;
@ -36,7 +35,7 @@ export async function cancelDownload(mediaType: MediaTypes, messageId: string):
const downloadKey = mediaDownloadKey(mediaType, messageId); const downloadKey = mediaDownloadKey(mediaType, messageId);
if (!isEmpty(downloadQueue[downloadKey])) { if (!isEmpty(downloadQueue[downloadKey])) {
try { try {
await downloadQueue[downloadKey].cancel(); await downloadQueue[downloadKey].cancelAsync();
} catch { } catch {
// Do nothing // Do nothing
} }
@ -55,37 +54,19 @@ export function downloadMediaFile({
downloadUrl: string; downloadUrl: string;
path: string; path: string;
}): Promise<string> { }): Promise<string> {
return new Promise((resolve, reject) => { return new Promise(async (resolve, reject) => {
const downloadKey = mediaDownloadKey(mediaType, messageId);
const options = {
timeout: 10000,
indicator: true,
overwrite: true,
// The RNFetchBlob just save the file when didn't exist the file:// at the begin of path
path: path.replace('file://', '')
};
downloadQueue[downloadKey] = RNFetchBlob.config(options).fetch('GET', downloadUrl);
downloadQueue[downloadKey].then(response => {
if (response.respInfo.status >= 200 && response.respInfo.status < 400) {
// If response is all good...
try { try {
resolve(path); const downloadKey = mediaDownloadKey(mediaType, messageId);
} catch (e) { downloadQueue[downloadKey] = FileSystem.createDownloadResumable(downloadUrl, path);
reject(); const result = await downloadQueue[downloadKey].downloadAsync();
log(e); if (result?.uri) {
} finally { return resolve(result.uri);
delete downloadQueue[downloadKey];
} }
} else { reject();
delete downloadQueue[downloadKey]; } catch {
reject(); reject();
} }
}); });
downloadQueue[downloadKey].catch(() => {
delete downloadQueue[downloadKey];
reject();
});
});
} }
export const LOCAL_DOCUMENT_PATH = `${FileSystem.documentDirectory}`; export const LOCAL_DOCUMENT_PATH = `${FileSystem.documentDirectory}`;