Skip to content

Commit bd41f80

Browse files
authored
Merge pull request #16 from jesperlndk/feature/fetch-headers
Feature: Resolve headers
2 parents f5cb4b7 + fc4e10f commit bd41f80

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

CachedImage.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const styles = StyleSheet.create({
3333
});
3434

3535
function getImageProps(props) {
36-
return _.omit(props, ['source', 'defaultSource', 'activityIndicatorProps', 'style', 'useQueryParamsInCacheKey', 'renderImage']);
36+
return _.omit(props, ['source', 'defaultSource', 'activityIndicatorProps', 'style', 'useQueryParamsInCacheKey', 'renderImage', 'resolveHeaders']);
3737
}
3838

3939
const CACHED_IMAGE_REF = 'cachedImage';
@@ -45,14 +45,16 @@ const CachedImage = React.createClass({
4545
useQueryParamsInCacheKey: React.PropTypes.oneOfType([
4646
React.PropTypes.bool,
4747
React.PropTypes.array
48-
]).isRequired
48+
]).isRequired,
49+
resolveHeaders: React.PropTypes.func
4950
},
5051

5152
getDefaultProps() {
5253
return {
5354
renderImage: props => (<Image ref={CACHED_IMAGE_REF} {...props}/>),
5455
activityIndicatorProps: {},
55-
useQueryParamsInCacheKey: false
56+
useQueryParamsInCacheKey: false,
57+
resolveHeaders: () => Promise.resolve({})
5658
};
5759
},
5860

@@ -118,7 +120,7 @@ const CachedImage = React.createClass({
118120
// try to get the image path from cache
119121
ImageCacheProvider.getCachedImagePath(url, options)
120122
// try to put the image in cache if
121-
.catch(() => ImageCacheProvider.cacheImage(url, options))
123+
.catch(() => ImageCacheProvider.cacheImage(url, options, this.props.resolveHeaders))
122124
.then(cachedImagePath => {
123125
this.safeSetState({
124126
cachedImagePath

ImageCacheProvider.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,15 @@ function ensurePath(filePath) {
9090
* @param toFile
9191
* @returns {Promise}
9292
*/
93-
function downloadImage(fromUrl, toFile) {
93+
function downloadImage(fromUrl, toFile, headers) {
9494
// use toFile as the key as is was created using the cacheKey
9595
if (!_.has(activeDownloads, toFile)) {
9696
// create an active download for this file
9797
activeDownloads[toFile] = new Promise((resolve, reject) => {
9898
const downloadOptions = {
9999
fromUrl,
100-
toFile
100+
toFile,
101+
headers
101102
};
102103
RNFS.downloadFile(downloadOptions).promise
103104
.then(res => {
@@ -175,10 +176,11 @@ function getCachedImagePath(url, options = defaultOptions) {
175176
})
176177
}
177178

178-
function cacheImage(url, options = defaultOptions) {
179+
function cacheImage(url, options = defaultOptions, resolveHeaders) {
179180
const filePath = getCachedImageFilePath(url, options);
180181
return ensurePath(filePath)
181-
.then(() => downloadImage(url, filePath));
182+
.then(() => resolveHeaders())
183+
.then(headers => downloadImage(url, filePath, headers));
182184
}
183185

184186
function deleteCachedImage(url, options = defaultOptions) {

0 commit comments

Comments
 (0)