react-native-blob-util
react-native-blob-util copied to clipboard
The cache is still there after unlink it with no error thrown - v0.17.3
In my react native 0.70.x app with react-native-blob-util 0.17.3, an image is saved to the cache after downloading over the internet, and was deleted as needed. However after performing unlink, the cache file is still there and was not deleted. There is no error thrown when performing unlink. MacOS 13 and android simulator.
Here is the code to save cache:
httpDownload = async (cacheFile, path) => {
try {
ReactNativeBlobUtil.config({
// response data will be saved to this path if it has access right.
path: cacheFile,
fileCache : true, //makes response data to be stored as a file, this is much more performant.
})
.fetch(
'GET',
path
)
.then(async (res) => {
if (res && res.respInfo && res.respInfo.headers && !res.respInfo.headers["Content-Encoding"] && !res.respInfo.headers["Transfer-Encoding"] && res.respInfo.headers["Content-Length"]) {
const expectedContentLength = res.respInfo.headers["Content-Length"];
let actualContentLength;
try {
const fileStats = await ReactNativeBlobUtil.fs.stat(res.path());
if (!fileStats || !fileStats.size) {
await _unlinkFile(cacheFile);
throw new Error("FileNotFound:"+path); //<<==
}
actualContentLength = fileStats.size;
if (expectedContentLength != actualContentLength) {
//throw new Error("DownloadFailed:"+path); //<<==
await _unlinkFile(cacheFile);
throw new Error("FileSizeNotMatch:"+path);
}
} catch (err) {
console.error("Error during image download:", error);
await _unlinkFile(cacheFile); // Perform cleanup if necessary
// Handle the error or notify the user
}
}
})
.catch(async (error) => {
//_unlinkFile(tempCacheFile);
await _unlinkFile(cacheFile);
console.log("Error in cached image ReactnativeBlobUtil.config GET _httpDownload: ", error);
});
} catch (err) {
console.log("Error in http download cache : ", err);
};
};
Here is the code to delete the cache saved:
deleteBadCache = async (url) => { //url is the original file name before cache. also remove the cached file name from sameURL which keeps array of file name cached.
try {
let cacheFile = _getCacheFilename(url); //<<==
await _unlinkFile(cacheFile);
let _chk = await _isUrlCached(url); //<<==check if the file was deleted
console.log("CachedImage.sameURL AFTER delete bad cache : ", `${_chk}`); //<<==should be false. However true was returned
return null;
} catch (err) {
console.log("err in deletebadcache : ", err);
};
}
_unlinkFile = async (file) => {. //<<==ex, /data/user/0/com.myapp/cache/CachedImage/b931fe72277209ab39e8662ae27bf062b0815756.jpg
try {
return await ReactNativeBlobUtil.fs.unlink(file);
} catch (e) {
console.log("Error when deleting cache file");
};
}
Here is the function _isUrlCached to check if a cache exists (url has the image file name):
_isUrlCached = async (url) => {
try {
const cacheFile = _getCacheFilename(url);
await ReactNativeBlobUtil.fs.exists(cacheFile); //throw error if there is NO
return true;
} catch(err) {
return false;
};
};
Sounds like a bug yes. Need to test this one and see what's wrong.
Hi @RonRadtke and @emclab , we are experiencing a similar issue where the file-cache grows very large, even though we run the cleanup function: ReactNativeBlobUtil.fs.unlink(file)
Do you perhaps know when this bug will be fixed?
same here react native version 73.9 "react-native-blob-util": "^0.19.11",
Clearing the cache with fs.unlink or flush works like a charm on iOS, but I also have this issue on Android!
It keeps duplicating files when running ReactNativeBlobUtil.android.actionViewIntent.
Code
const { fs, config } = ReactNativeBlobUtil
const configfb: ReactNativeBlobUtilConfig = {
fileCache: false,
path: filePath,
addAndroidDownloads: {
useDownloadManager: true,
notification: false,
mime: mime,
path: filePath,
},
}
return config(configfb)
.fetch('GET', url)
.then((res) => {
ReactNativeBlobUtil.android.actionViewIntent(res.path(), mime)
return res
})
Config
"react-native": "0.74.3",
"react-native-blob-util": "0.19.11",