Skip to content

Commit 5fff5a7

Browse files
author
Artur Chrusciel
committed
Check for file existence
1 parent 98d8dd7 commit 5fff5a7

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

ImageCacheManager.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,27 @@ module.exports = (defaultOptions = {}, urlCache = MemoryCache, fs = fsUtils, pat
3535
return urlCache.get(cacheableUrl)
3636
.then(fileRelativePath => {
3737
if (!fileRelativePath) {
38-
// console.log('ImageCacheManager: cache miss', cacheableUrl);
38+
// console.log('ImageCacheManager: url cache miss', cacheableUrl);
3939
throw new Error('URL expired or not in cache');
4040
}
41-
// console.log('ImageCacheManager: cache hit', cacheableUrl);
42-
return `${options.cacheLocation}/${fileRelativePath}`;
41+
// console.log('ImageCacheManager: url cache hit', cacheableUrl);
42+
const cachedFilePath = `${options.cacheLocation}/${fileRelativePath}`;
43+
44+
return fs.exists(cachedFilePath)
45+
.then((exists) => {
46+
if (exists) {
47+
return cachedFilePath
48+
} else {
49+
throw new Error('file under URL stored in url cache doesn\'t exsts');
50+
}
51+
});
4352
})
4453
// url is not found in the cache or is expired
4554
.catch(() => {
46-
// const filePath = path.getImageFilePath(cacheableUrl, options.cacheLocation);
4755
const fileRelativePath = path.getImageRelativeFilePath(cacheableUrl);
48-
// remove expired file if exists
49-
5056
const filePath = `${options.cacheLocation}/${fileRelativePath}`
5157

58+
// remove expired file if exists
5259
return fs.deleteFile(filePath)
5360
// get the image to cache (download / copy / etc)
5461
.then(() => getCachedFile(filePath))

0 commit comments

Comments
 (0)