UNPKG

react-native-fs-turbo

Version:

React-Native library for working with Android/iOS file system, written using JSI and C++ TurboModules

287 lines (286 loc) 8.88 kB
"use strict"; import { createRNFSTurbo } from "./createRNFSTurbo"; /** * A single RNFSTurbo instance. */ class RNFSTurbo { /** * Creates a new RNFSTurbo instance with the given Configuration. * If no custom `id` is supplied, `'rnfsturbo'` will be used. */ constructor() { const { configuration, instance } = createRNFSTurbo(); this.nativeInstance = instance; this.configuration = configuration; this.functionCache = {}; } getFunctionFromCache(functionName) { if (this.functionCache[functionName] == null) { this.functionCache[functionName] = this.nativeInstance[functionName]; } return this.functionCache[functionName]; } get MainBundlePath() { return this.configuration.mainBundlePath; } get CachesDirectoryPath() { return this.configuration.cachesDirectoryPath; } get DocumentDirectoryPath() { return this.configuration.documentDirectoryPath; } get TemporaryDirectoryPath() { return this.configuration.temporaryDirectoryPath; } get LibraryDirectoryPath() { return this.configuration.libraryDirectoryPath; } get ExternalDirectoryPath() { return this.configuration.externalDirectoryPath; } get ExternalStorageDirectoryPath() { return this.configuration.externalStorageDirectoryPath; } get ExternalCachesDirectoryPath() { return this.configuration.externalCachesDirectoryPath; } get DownloadDirectoryPath() { return this.configuration.mainBundlePath; } get PicturesDirectoryPath() { return this.configuration.picturesDirectoryPath; } get RoamingDirectoryPath() { return this.configuration.roamingDirectoryPath; } stat(filepath, isNewFormat) { const func = this.getFunctionFromCache("stat"); return func(filepath, isNewFormat); } readDir(dirpath, isNewFormat) { const func = this.getFunctionFromCache("readDir"); return func(dirpath, isNewFormat); } readDirAssets(dirpath, isNewFormat) { const func = this.getFunctionFromCache("readDirAssets"); return func(dirpath, isNewFormat); } readdir(dirpath) { const func = this.getFunctionFromCache("readdir"); return func(dirpath); } readFile(filepath, options) { const func = this.getFunctionFromCache("readFile"); return func(filepath, options); } read(filepath, length, position, options) { const func = this.getFunctionFromCache("read"); return func(filepath, length, position, options); } readFileAssets(filepath, options) { const func = this.getFunctionFromCache("readFileAssets"); return func(filepath, options); } readFileRes(filepath, options) { const func = this.getFunctionFromCache("readFileRes"); return func(filepath, options); } writeFile(filepath, contents, options) { const func = this.getFunctionFromCache("writeFile"); return func(filepath, contents, options); } appendFile(filepath, contents, options) { const func = this.getFunctionFromCache("appendFile"); return func(filepath, contents, options); } write(filepath, contents, position, options) { const func = this.getFunctionFromCache("write"); return func(filepath, contents, position, options); } moveFile(filepath, destPath, options) { const func = this.getFunctionFromCache("moveFile"); return func(filepath, destPath, options); } copyFolder(srcFolderPath, destFolderPath, options) { const func = this.getFunctionFromCache("copyFolder"); return func(srcFolderPath, destFolderPath, options); } copyFile(filepath, destPath, options) { const func = this.getFunctionFromCache("copyFile"); return func(filepath, destPath, options); } copyFileAssets(filepath, destPath) { const func = this.getFunctionFromCache("copyFileAssets"); return func(filepath, destPath); } copyFileRes(filepath, destPath) { const func = this.getFunctionFromCache("copyFileRes"); return func(filepath, destPath); } copyAssetsFileIOS(imageUri, destPath, width, height, scale, compression, resizeMode) { const func = this.getFunctionFromCache("copyAssetsFileIOS"); return func(imageUri, destPath, width, height, scale, compression, resizeMode); } copyAssetsVideoIOS(videoUri, destPath) { const func = this.getFunctionFromCache("copyAssetsVideoIOS"); return func(videoUri, destPath); } unlink(filepath, checkExistence = true) { const func = this.getFunctionFromCache("unlink"); return func(filepath, checkExistence); } exists(filepath) { const func = this.getFunctionFromCache("exists"); return func(filepath); } existsAssets(filepath) { const func = this.getFunctionFromCache("existsAssets"); return func(filepath); } existsRes(filepath) { const func = this.getFunctionFromCache("existsRes"); return func(filepath); } hash(filepath, algorithm) { const func = this.getFunctionFromCache("hash"); return func(filepath, algorithm); } touch(filepath, mtime, ctime) { const func = this.getFunctionFromCache("touch"); return func(filepath, mtime?.valueOf(), ctime?.valueOf()); } mkdir(filepath, options) { const func = this.getFunctionFromCache("mkdir"); return func(filepath, options); } downloadFile(options, completeCallback, errorCallback) { const funcResult = { resolve: null, reject: null }; const func = this.getFunctionFromCache("downloadFile"); const tmpCallbackFunc = completeCallback || (res => { funcResult.resolve = res; }); const tmpErrorFunc = errorCallback || (res => { funcResult.reject = res; }); const promise = !completeCallback ? new Promise((resolve, reject) => { const callbackInterval = setInterval(() => { if (funcResult.resolve) { clearInterval(callbackInterval); resolve(funcResult.resolve); } else if (funcResult.reject) { clearInterval(callbackInterval); reject(funcResult.reject); } }, 100); }) : undefined; const jobId = func(options, tmpCallbackFunc, tmpErrorFunc); if (completeCallback) { return { jobId }; } return { jobId, promise }; } stopDownload(jobId) { const func = this.getFunctionFromCache("stopDownload"); return func(jobId); } resumeDownload(jobId) { const func = this.getFunctionFromCache("resumeDownload"); return func(jobId); } isResumable(jobId) { const func = this.getFunctionFromCache("isResumable"); return func(jobId); } completeHandlerIOS(jobId) { const func = this.getFunctionFromCache("completeHandlerIOS"); return func(jobId); } uploadFiles(options, completeCallback, errorCallback) { const funcResult = { resolve: null, reject: null }; const func = this.getFunctionFromCache("uploadFiles"); const tmpCallbackFunc = completeCallback || (res => { funcResult.resolve = res; }); const tmpErrorFunc = errorCallback || (res => { funcResult.reject = res; }); const promise = !completeCallback ? new Promise((resolve, reject) => { const callbackInterval = setInterval(() => { if (funcResult.resolve) { clearInterval(callbackInterval); resolve(funcResult.resolve); } else if (funcResult.reject) { clearInterval(callbackInterval); reject(funcResult.reject); } }, 100); }) : undefined; const jobId = func(options, tmpCallbackFunc, tmpErrorFunc); if (completeCallback) { return { jobId }; } return { jobId, promise }; } stopUpload(jobId) { const func = this.getFunctionFromCache("stopUpload"); return func(jobId); } getFSInfo() { const func = this.getFunctionFromCache("getFSInfo"); return func(); } scanFile(path, completeCallback) { const funcResult = { resolve: null }; const func = this.getFunctionFromCache("scanFile"); const tmpCallbackFunc = completeCallback || (res => { funcResult.resolve = res; }); const promise = !completeCallback ? new Promise(resolve => { const callbackInterval = setInterval(() => { if (funcResult.resolve) { clearInterval(callbackInterval); resolve(funcResult.resolve.path); } }, 100); }) : undefined; const jobId = func(path, tmpCallbackFunc); if (completeCallback) { return { jobId }; } return promise; } getAllExternalFilesDirs() { const func = this.getFunctionFromCache("getAllExternalFilesDirs"); return func(); } pathForGroup(groupIdentifier) { const func = this.getFunctionFromCache("pathForGroup"); return func(groupIdentifier); } } const RNFSTurboInstance = new RNFSTurbo(); export default RNFSTurboInstance; //# sourceMappingURL=RNFSTurbo.js.map