Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Resolve headers #16

Merged
merged 3 commits into from
Mar 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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