From 537741037d691e1b7a851f17bbf81d157f941a3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Ba=CC=88der?= Date: Thu, 5 May 2022 10:28:00 +0200 Subject: [PATCH] Implemented check for complete download. Now the file first gets a suffix and is moved after download copmleted to prevent broken images after the app closed while loading --- lib/FileSystem.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/FileSystem.js b/lib/FileSystem.js index 9d13223..68c7695 100644 --- a/lib/FileSystem.js +++ b/lib/FileSystem.js @@ -294,24 +294,28 @@ export class FileSystem { // Hit network and download file to local disk. let result = null; - try { + const incompleteFilePath = path + '.incomplete'; + try { result = await RNFetchBlob .config({ - path: path + path: incompleteFilePath }) .fetch('GET', url); + if (result) { + await RNFetchBlob.fs.mv(incompleteFilePath, path); + } } catch(error) { // File must be manually removed on download error https://github.com/wkh237/react-native-fetch-blob/issues/331 - await RNFetchBlob.fs.unlink(path); + await RNFetchBlob.fs.unlink(incompleteFilePath); throw error; } return { - path: result.path(), + path, fileName: pathLib.basename(path) }; @@ -395,4 +399,4 @@ export default function FileSystemFactory(cachePruneTriggerLimit = null, fileDir if (!(this instanceof FileSystem)) { return new FileSystem(cachePruneTriggerLimit, fileDirName); } -} \ No newline at end of file +}