@@ -192,6 +192,55 @@ export class FileSystem {
192
192
return null ;
193
193
}
194
194
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
+
195
244
/**
196
245
*
197
246
* Used to download files to local filesystem.
0 commit comments