import {Platform} from 'react-native'; import { mkdir, readFile, readFileAssets, unlink, exists, stopDownload, downloadFile, MainBundlePath, DownloadDirectoryPath, LibraryDirectoryPath, ExternalDirectoryPath, writeFile, DownloadProgressCallbackResult, DownloadBeginCallbackResult, } from 'react-native-fs'; import {IFs, IFsDownloadParams, IFsDownloadProgressParams} from "./fs"; const fs: IFs = { getAssetsPath: () => Platform.OS === 'android' ? '' : `${MainBundlePath}/`, getExternalPath: () => Platform.OS === 'android' ? ExternalDirectoryPath : LibraryDirectoryPath, getAndroidDownloadPath: () => DownloadDirectoryPath, mkdir: function (path: string): void { mkdir(path).then() }, readFile: function (path: string): Promise { return readFile(path) }, readFileAssets: function (path: string): Promise { if (Platform.OS === 'android') { return readFileAssets(path) } else if (Platform.OS === 'ios') { return readFile(path) } }, white: function (path: string, content: string, encoding?: string): void { writeFile(path, content, encoding).then() }, has: function (path: string): Promise { return exists(path); }, remove: async function (path: string): Promise { const data = await unlink(path).catch(() => false); return !!data }, download: function (fromLink: string, toFilePath: string, params?: IFsDownloadParams): void { params = params || {}; const options = { fromUrl: fromLink, toFile: toFilePath, background: true, progressDivider: params.progressDivider ?? 5, begin: (res: DownloadBeginCallbackResult) => { if (typeof params.beginCallback === "function") params.beginCallback(res); }, progress: (res: DownloadProgressCallbackResult) => { let result: IFsDownloadProgressParams = { ...res, percentage: (res.bytesWritten / res.contentLength) * 100, ratio: (res.bytesWritten / res.contentLength), totalSize: res.contentLength / 1024 / 1024, currentSize: res.bytesWritten / 1024 / 1024 }; if (typeof params.progressCallback === "function") params.progressCallback(result); } } downloadFile(options).promise.then(res => { if (typeof params.successCallback === 'function') params.successCallback(res); }).catch(err => { if (typeof params.errorCallback === 'function') params.errorCallback(err.toString()); }) }, downloadStop: function (jobId: string): void { stopDownload(Number(jobId)); } } export default fs export type { IFs, IFsDownloadParams }