Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit 24e37d7

Browse files
committed
Add method to move or copy a file to the cache
1 parent 41f76f7 commit 24e37d7

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/FileSystem.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,55 @@ export class FileSystem {
192192
return null;
193193
}
194194

195+
/**
196+
*
197+
* Manually move or copy a file to the cache.
198+
* Can be used to pre-warm caches.
199+
* If calling this method repeatedly to cache a long list of files,
200+
* be sure to use a queue and limit concurrency so your app performance does not suffer.
201+
*
202+
* @param local {String} - path to the local file.
203+
* @param url {String} - url of file to download.
204+
* @param permanent {Boolean} - whether the file should be saved to the tmp or permanent cache directory.
205+
* @param move {Boolean} - whether the file should be copied or moved.
206+
* @returns {Promise} promise that resolves to an object that contains cached file info.
207+
*/
208+
async cacheLocalFile(local, url, permanent = false, move = false) {
209+
const fileName = await this.getFileNameFromUrl(url);
210+
let path = this.baseFilePath + (permanent ? 'permanent/' : 'cache/') + fileName;
211+
this._validatePath(path, true);
212+
213+
// Logic here prunes cache directory on "cache" writes to ensure cache doesn't get too large.
214+
if (!permanent) {
215+
await this.pruneCache();
216+
}
217+
218+
// Move or copy the file to the cache
219+
try {
220+
const cacheDirExists = await this.exists(permanent ? 'permanent' : 'cache');
221+
if (!cacheDirExists) {
222+
await RNFS.mkdir(`${this.baseFilePath}${permanent ? 'permanent' : 'cache'}`);
223+
}
224+
225+
await RNFSUnlinkIfExists(path);
226+
const { promise } = move ? RNFS.moveFile(local, path) : RNFS.copyFile(local, path);
227+
await promise;
228+
} catch (error) {
229+
await RNFSUnlinkIfExists(path);
230+
return {
231+
url: null,
232+
cacheType: permanent ? 'permanent' : 'cache',
233+
path: null,
234+
};
235+
}
236+
237+
return {
238+
url: url,
239+
cacheType: permanent ? 'permanent' : 'cache',
240+
path: path,
241+
};
242+
}
243+
195244
/**
196245
*
197246
* Used to download files to local filesystem.

src/imageCacheHoc.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,24 @@ export default function imageCacheHoc(Image, options = {}) {
7979
};
8080
}
8181

82+
/**
83+
*
84+
* Manually move or copy a file to the cache.
85+
* Can be used to pre-warm caches.
86+
* If calling this method repeatedly to cache a long list of files,
87+
* be sure to use a queue and limit concurrency so your app performance does not suffer.
88+
*
89+
* @param local {String} - path to the local file.
90+
* @param url {String} - url of file to download.
91+
* @param permanent {Boolean} - whether the file should be saved to the tmp or permanent cache directory.
92+
* @param move {Boolean} - whether the file should be copied or moved.
93+
* @returns {Promise} promise that resolves to an object that contains cached file info.
94+
*/
95+
static async cacheLocalFile(local, url, permanent = false, move = false) {
96+
const fileSystem = FileSystemFactory(options.cachePruneTriggerLimit, options.fileDirName);
97+
return fileSystem.cacheLocalFile(local, url, permanent, move);
98+
}
99+
82100
/**
83101
*
84102
* Delete all locally stored image files created by react-native-image-cache-hoc (cache AND permanent).

0 commit comments

Comments
 (0)