Skip to content

Commit

Permalink
fix isUrlCached(), add getCacheFilename()
Browse files Browse the repository at this point in the history
  • Loading branch information
Wonday committed Sep 24, 2019
1 parent 80e8ee6 commit bf9ec47
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ import CachedImage from 'react-native-image-cache-wrapper';
CachedImage.clearCache();
```

**CachedImage.isUrlCached(url)**
**CachedImage.isUrlCached(url,success=(cachFile)=>void,fail=(error)=>void))**

check if a url is cached.

Expand All @@ -156,11 +156,22 @@ Example:
import CachedImage from 'react-native-image-cache-wrapper';
// check if a url is cached.
if (CachedImage.isUrlCached(url)) {
// do something
};
CachedImage.isUrlCached(url,(exists)=>{
alert(exists);
});
```

**CachedImage.getCacheFilename(url)**

make a cache filename.

Example:
```
import CachedImage from 'react-native-image-cache-wrapper';
// check if a url is cached.
let cachedFilename = CachedImage.getCacheFilename(url);
```

**CachedImage.cacheDir**

Expand Down
21 changes: 18 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,26 @@ export default class CachedImage extends Component {
/**
* check if a url is cached
*/
static isUrlCached = async (url) => {
static isUrlCached = (url: string, success: Function, failure: Function) => {
const cacheFile = _getCacheFilename(url);
return !!(await RNFetchBlob.fs.stat(cacheFile));
RNFetchBlob.fs.exists(cacheFile)
.then((exists) => {
success && success(exists);
})
.catch((error) => {
failure && failure(error);
});
};

/**
* make a cache filename
* @param url
* @returns {string}
*/
static getCacheFilename = (url) => {
return _getCacheFilename(url);
}

/**
* Same as ReactNaive.Image.getSize only it will not download the image if it has a cached version
* @param url
Expand Down Expand Up @@ -298,4 +313,4 @@ async function _saveCacheFile(url: string, success: Function, failure: Function)
} catch (error) {
failure && failure(error);
}
}
}

0 comments on commit bf9ec47

Please sign in to comment.