Skip to content

Commit bf9ec47

Browse files
author
Wonday
committed
fix isUrlCached(), add getCacheFilename()
1 parent 80e8ee6 commit bf9ec47

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ import CachedImage from 'react-native-image-cache-wrapper';
147147
CachedImage.clearCache();
148148
```
149149

150-
**CachedImage.isUrlCached(url)**
150+
**CachedImage.isUrlCached(url,success=(cachFile)=>void,fail=(error)=>void))**
151151

152152
check if a url is cached.
153153

@@ -156,11 +156,22 @@ Example:
156156
import CachedImage from 'react-native-image-cache-wrapper';
157157
158158
// check if a url is cached.
159-
if (CachedImage.isUrlCached(url)) {
160-
// do something
161-
};
159+
CachedImage.isUrlCached(url,(exists)=>{
160+
alert(exists);
161+
});
162162
```
163163

164+
**CachedImage.getCacheFilename(url)**
165+
166+
make a cache filename.
167+
168+
Example:
169+
```
170+
import CachedImage from 'react-native-image-cache-wrapper';
171+
172+
// check if a url is cached.
173+
let cachedFilename = CachedImage.getCacheFilename(url);
174+
```
164175

165176
**CachedImage.cacheDir**
166177

index.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,26 @@ export default class CachedImage extends Component {
4646
/**
4747
* check if a url is cached
4848
*/
49-
static isUrlCached = async (url) => {
49+
static isUrlCached = (url: string, success: Function, failure: Function) => {
5050
const cacheFile = _getCacheFilename(url);
51-
return !!(await RNFetchBlob.fs.stat(cacheFile));
51+
RNFetchBlob.fs.exists(cacheFile)
52+
.then((exists) => {
53+
success && success(exists);
54+
})
55+
.catch((error) => {
56+
failure && failure(error);
57+
});
5258
};
5359

60+
/**
61+
* make a cache filename
62+
* @param url
63+
* @returns {string}
64+
*/
65+
static getCacheFilename = (url) => {
66+
return _getCacheFilename(url);
67+
}
68+
5469
/**
5570
* Same as ReactNaive.Image.getSize only it will not download the image if it has a cached version
5671
* @param url
@@ -298,4 +313,4 @@ async function _saveCacheFile(url: string, success: Function, failure: Function)
298313
} catch (error) {
299314
failure && failure(error);
300315
}
301-
}
316+
}

0 commit comments

Comments
 (0)