Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit d9a7c21

Browse files
committed
Return valid asset path for platform and push repeated code up to the getLocalFilePathFromUrl method
1 parent 6935947 commit d9a7c21

File tree

2 files changed

+11
-15
lines changed

2 files changed

+11
-15
lines changed

src/FileSystem.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export class FileSystem {
152152
*
153153
* @param url {String} - url of file to download.
154154
* @param permanent {Boolean} - True persists the file locally indefinitely, false caches the file temporarily (until file is removed during cache pruning).
155-
* @returns {Promise} promise that resolves to the local file path of downloaded url file.
155+
* @returns {Promise<string|null>} promise that resolves to the local file path of downloaded url file.
156156
*/
157157
async getLocalFilePathFromUrl(url, permanent) {
158158
let filePath = null;
@@ -173,7 +173,11 @@ export class FileSystem {
173173
filePath = result.path;
174174
}
175175

176-
return filePath;
176+
if (filePath) {
177+
return Platform.OS === 'android' ? 'file://' + filePath : filePath;
178+
}
179+
180+
return null;
177181
}
178182

179183
/**

src/imageCacheHoc.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
// Load dependencies.
1717
import React from 'react';
18-
import { Platform } from 'react-native';
1918
import PropTypes from 'prop-types';
2019
import FileSystemFactory, { FileSystem } from './FileSystem';
2120
import traverse from 'traverse';
@@ -233,14 +232,11 @@ export default function imageCacheHoc(Image, options = {}) {
233232
this.setState({ localFilePath });
234233

235234
if (!this.InvalidUrl && this.props.onLoadFinished) {
236-
Image.getSize(
237-
Platform.OS === 'ios' ? localFilePath : 'file://' + localFilePath,
238-
(width, height) => {
239-
if (this._isMounted) {
240-
this.props.onLoadFinished(width, height);
241-
}
235+
Image.getSize(localFilePath, (width, height) => {
236+
if (this._isMounted) {
237+
this.props.onLoadFinished(width, height);
242238
}
243-
);
239+
});
244240
}
245241
}
246242
}
@@ -259,15 +255,11 @@ export default function imageCacheHoc(Image, options = {}) {
259255
render() {
260256
// If media loaded, render full image component, else render placeholder.
261257
if (this.state.localFilePath && !this.InvalidUrl) {
262-
// Build platform specific file resource uri.
263-
const localFileUri =
264-
Platform.OS == 'ios' ? this.state.localFilePath : 'file://' + this.state.localFilePath; // Android requires the traditional 3 prefixed slashes file:/// in a localhost absolute file uri.
265-
266258
// Extract props proprietary to this HOC before passing props through.
267259
let { permanent, ...filteredProps } = this.props; // eslint-disable-line no-unused-vars
268260

269261
let props = Object.assign({}, filteredProps, {
270-
source: { uri: localFileUri },
262+
source: { uri: this.state.localFilePath },
271263
});
272264
return <Image {...props} />;
273265
} else {

0 commit comments

Comments
 (0)