import { createRNFSTurbo } from "./createRNFSTurbo"; import type { Configuration } from "./NativeRNFSTurboModule"; import type { RNFSTurboInterface, OverloadedStatResult, OverloadedReadDirItem, Algorithms, MkdirOptions, OverloadedReadResult, DownloadFileOptions, DownloadResult, DownloadError, DownloadResultFunc, DownloadErrorFunc, OverloadedDownloadResult, UploadFileOptions, UploadResult, UploadError, UploadResultFunc, UploadErrorFunc, OverloadedUploadResult, FSInfoResult, ScanResult, ScanResultFunc, OverloadedScanResult, ReadOptions, WriteOptions, MoveCopyOptions, } from "./Types"; /** * A single RNFSTurbo instance. */ class RNFSTurbo implements RNFSTurboInterface { private nativeInstance: RNFSTurboInterface; private functionCache: Partial; private configuration: Configuration; /** * 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 = {}; } private getFunctionFromCache( functionName: T, ): RNFSTurbo[T] { if (this.functionCache[functionName] == null) { this.functionCache[functionName] = this.nativeInstance[functionName]; } return this.functionCache[functionName] as RNFSTurbo[T]; } get MainBundlePath(): string { return this.configuration.mainBundlePath; } get CachesDirectoryPath(): string { return this.configuration.cachesDirectoryPath; } get DocumentDirectoryPath(): string { return this.configuration.documentDirectoryPath; } get TemporaryDirectoryPath(): string { return this.configuration.temporaryDirectoryPath; } get LibraryDirectoryPath(): string { return this.configuration.libraryDirectoryPath; } get ExternalDirectoryPath(): string { return this.configuration.externalDirectoryPath; } get ExternalStorageDirectoryPath(): string { return this.configuration.externalStorageDirectoryPath; } get ExternalCachesDirectoryPath(): string { return this.configuration.externalCachesDirectoryPath; } get DownloadDirectoryPath(): string { return this.configuration.downloadDirectoryPath; } get PicturesDirectoryPath(): string { return this.configuration.picturesDirectoryPath; } get RoamingDirectoryPath(): string { return this.configuration.roamingDirectoryPath; } stat( filepath: string, isNewFormat?: T, ): OverloadedStatResult { const func = this.getFunctionFromCache("stat"); const result = func(filepath, isNewFormat); if (!isNewFormat) { result.ctime = new Date((result.ctime as number) * 1000); result.mtime = new Date((result.mtime as number) * 1000); } return result; } readDir( dirpath: string, isNewFormat?: T, ): OverloadedReadDirItem[] { const func = this.getFunctionFromCache("readDir"); const result = func(dirpath, isNewFormat); if (!isNewFormat) { return result.map((item) => ({ ...item, ctime: new Date((item.ctime as number) * 1000), mtime: new Date((item.mtime as number) * 1000), })); } return result; } readDirAssets( dirpath: string, isNewFormat?: T, ): OverloadedReadDirItem[] { const func = this.getFunctionFromCache("readDirAssets"); return func(dirpath, isNewFormat); } readdir(dirpath: string): string[] { const func = this.getFunctionFromCache("readdir"); return func(dirpath); } readFile( filepath: string, options?: T, ): OverloadedReadResult { const func = this.getFunctionFromCache("readFile"); return func(filepath, options); } read( filepath: string, length: number, position: number, options?: T, ): OverloadedReadResult { const func = this.getFunctionFromCache("read"); return func(filepath, length, position, options); } readFileAssets(filepath: string, options?: ReadOptions): string[] { const func = this.getFunctionFromCache("readFileAssets"); return func(filepath, options); } readFileRes(filepath: string, options?: ReadOptions): string[] { const func = this.getFunctionFromCache("readFileRes"); return func(filepath, options); } writeFile( filepath: string, contents: string | number[], options?: WriteOptions, ): void { const func = this.getFunctionFromCache("writeFile"); return func(filepath, contents, options); } appendFile( filepath: string, contents: string | number[], options?: WriteOptions, ): void { const func = this.getFunctionFromCache("appendFile"); return func(filepath, contents, options); } write( filepath: string, contents: string | number[], position?: number, options?: WriteOptions, ): void { const func = this.getFunctionFromCache("write"); return func(filepath, contents, position, options); } moveFile( filepath: string, destPath: string, options?: MoveCopyOptions, ): void { const func = this.getFunctionFromCache("moveFile"); return func(filepath, destPath, options); } copyFolder( srcFolderPath: string, destFolderPath: string, options?: MoveCopyOptions, ): void { const func = this.getFunctionFromCache("copyFolder"); return func(srcFolderPath, destFolderPath, options); } copyFile( filepath: string, destPath: string, options?: MoveCopyOptions, ): void { const func = this.getFunctionFromCache("copyFile"); return func(filepath, destPath, options); } copyFileAssets(filepath: string, destPath: string): void { const func = this.getFunctionFromCache("copyFileAssets"); return func(filepath, destPath); } copyFileRes(filepath: string, destPath: string): void { const func = this.getFunctionFromCache("copyFileRes"); return func(filepath, destPath); } copyAssetsFileIOS( imageUri: string, destPath: string, width: number, height: number, scale?: number, compression?: number, resizeMode?: string, ): string | undefined { const func = this.getFunctionFromCache("copyAssetsFileIOS"); return func( imageUri, destPath, width, height, scale, compression, resizeMode, ); } copyAssetsVideoIOS(videoUri: string, destPath: string): string | undefined { const func = this.getFunctionFromCache("copyAssetsVideoIOS"); return func(videoUri, destPath); } unlink(filepath: string, checkExistence = true): void { const func = this.getFunctionFromCache("unlink"); return func(filepath, checkExistence); } exists(filepath: string): boolean { const func = this.getFunctionFromCache("exists"); return func(filepath); } existsAssets(filepath: string): boolean { const func = this.getFunctionFromCache("existsAssets"); return func(filepath); } existsRes(filepath: string): boolean { const func = this.getFunctionFromCache("existsRes"); return func(filepath); } hash(filepath: string, algorithm: Algorithms): string { const func = this.getFunctionFromCache("hash"); return func(filepath, algorithm); } touch( filepath: string, mtime?: Date | number, ctime?: Date | number, ): number { const func = this.getFunctionFromCache("touch"); return func(filepath, mtime?.valueOf(), ctime?.valueOf()); } mkdir(filepath: string, options?: MkdirOptions): void { const func = this.getFunctionFromCache("mkdir"); return func(filepath, options); } downloadFile( options: DownloadFileOptions, completeCallback?: T, errorCallback?: DownloadErrorFunc, ): OverloadedDownloadResult { const func = this.getFunctionFromCache("downloadFile"); let resolvePromise: ((res: DownloadResult) => void) | undefined; let rejectPromise: ((res: DownloadError) => void) | undefined; const promise = !completeCallback ? new Promise((resolve, reject) => { resolvePromise = resolve; rejectPromise = reject; }) : undefined; const tmpCallbackFunc = completeCallback || ((res: DownloadResult) => { resolvePromise?.(res); }); const tmpErrorFunc = (res: DownloadError) => { errorCallback?.(res); rejectPromise?.(res); }; const jobId: any = func(options, tmpCallbackFunc, tmpErrorFunc); if (completeCallback) { return { jobId } as OverloadedDownloadResult; } return { jobId, promise, } as OverloadedDownloadResult; } stopDownload(jobId: number): void { const func = this.getFunctionFromCache("stopDownload"); return func(jobId); } resumeDownload(jobId: number): void { const func = this.getFunctionFromCache("resumeDownload"); return func(jobId); } isResumable(jobId: number): boolean { const func = this.getFunctionFromCache("isResumable"); return func(jobId); } completeHandlerIOS(jobId: number): void { const func = this.getFunctionFromCache("completeHandlerIOS"); return func(jobId); } uploadFiles( options: UploadFileOptions, completeCallback?: T, errorCallback?: UploadErrorFunc, ): OverloadedUploadResult { const func = this.getFunctionFromCache("uploadFiles"); let resolvePromise: ((res: UploadResult) => void) | undefined; let rejectPromise: ((res: UploadError) => void) | undefined; const promise = !completeCallback ? new Promise((resolve, reject) => { resolvePromise = resolve; rejectPromise = reject; }) : undefined; const tmpCallbackFunc = completeCallback || ((res: UploadResult) => { resolvePromise?.(res); }); const tmpErrorFunc = (res: UploadError) => { errorCallback?.(res); rejectPromise?.(res); }; const jobId: any = func(options, tmpCallbackFunc, tmpErrorFunc); if (completeCallback) { return { jobId } as OverloadedUploadResult; } return { jobId, promise, } as OverloadedUploadResult; } stopUpload(jobId: number): void { const func = this.getFunctionFromCache("stopUpload"); return func(jobId); } getFSInfo(): FSInfoResult { const func = this.getFunctionFromCache("getFSInfo"); return func(); } scanFile( path: string, completeCallback?: T, ): OverloadedScanResult { const func = this.getFunctionFromCache("scanFile"); let resolvePromise: ((path: string) => void) | undefined; const promise = !completeCallback ? new Promise((resolve) => { resolvePromise = resolve; }) : undefined; const tmpCallbackFunc = completeCallback || ((res: ScanResult) => { resolvePromise?.(res.path); }); const jobId: any = func(path, tmpCallbackFunc); if (completeCallback) { return { jobId } as OverloadedScanResult; } return promise as OverloadedScanResult; } getAllExternalFilesDirs(): string[] { const func = this.getFunctionFromCache("getAllExternalFilesDirs"); return func(); } pathForGroup(groupIdentifier: string): string { const func = this.getFunctionFromCache("pathForGroup"); return func(groupIdentifier); } } const RNFSTurboInstance = new RNFSTurbo(); export default RNFSTurboInstance;