Skip to content

Commit

Permalink
Merge pull request #16 from jesperlndk/feature/fetch-headers
Browse files Browse the repository at this point in the history
Feature: Resolve headers
  • Loading branch information
kfiroo committed Mar 30, 2017
2 parents f5cb4b7 + fc4e10f commit bd41f80
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
10 changes: 6 additions & 4 deletions CachedImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const styles = StyleSheet.create({
});

function getImageProps(props) {
return _.omit(props, ['source', 'defaultSource', 'activityIndicatorProps', 'style', 'useQueryParamsInCacheKey', 'renderImage']);
return _.omit(props, ['source', 'defaultSource', 'activityIndicatorProps', 'style', 'useQueryParamsInCacheKey', 'renderImage', 'resolveHeaders']);
}

const CACHED_IMAGE_REF = 'cachedImage';
Expand All @@ -45,14 +45,16 @@ const CachedImage = React.createClass({
useQueryParamsInCacheKey: React.PropTypes.oneOfType([
React.PropTypes.bool,
React.PropTypes.array
]).isRequired
]).isRequired,
resolveHeaders: React.PropTypes.func
},

getDefaultProps() {
return {
renderImage: props => (<Image ref={CACHED_IMAGE_REF} {...props}/>),
activityIndicatorProps: {},
useQueryParamsInCacheKey: false
useQueryParamsInCacheKey: false,
resolveHeaders: () => Promise.resolve({})
};
},

Expand Down Expand Up @@ -118,7 +120,7 @@ const CachedImage = React.createClass({
// try to get the image path from cache
ImageCacheProvider.getCachedImagePath(url, options)
// try to put the image in cache if
.catch(() => ImageCacheProvider.cacheImage(url, options))
.catch(() => ImageCacheProvider.cacheImage(url, options, this.props.resolveHeaders))
.then(cachedImagePath => {
this.safeSetState({
cachedImagePath
Expand Down
10 changes: 6 additions & 4 deletions ImageCacheProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,15 @@ function ensurePath(filePath) {
* @param toFile
* @returns {Promise}
*/
function downloadImage(fromUrl, toFile) {
function downloadImage(fromUrl, toFile, headers) {
// use toFile as the key as is was created using the cacheKey
if (!_.has(activeDownloads, toFile)) {
// create an active download for this file
activeDownloads[toFile] = new Promise((resolve, reject) => {
const downloadOptions = {
fromUrl,
toFile
toFile,
headers
};
RNFS.downloadFile(downloadOptions).promise
.then(res => {
Expand Down Expand Up @@ -175,10 +176,11 @@ function getCachedImagePath(url, options = defaultOptions) {
})
}

function cacheImage(url, options = defaultOptions) {
function cacheImage(url, options = defaultOptions, resolveHeaders) {
const filePath = getCachedImageFilePath(url, options);
return ensurePath(filePath)
.then(() => downloadImage(url, filePath));
.then(() => resolveHeaders())
.then(headers => downloadImage(url, filePath, headers));
}

function deleteCachedImage(url, options = defaultOptions) {
Expand Down

0 comments on commit bd41f80

Please sign in to comment.