From b89d6c8e046b2166b28f351202be1ac56aa2f55d Mon Sep 17 00:00:00 2001 From: Mandeep Baines Date: Fri, 8 Sep 2017 10:41:36 -0700 Subject: [PATCH] consistent use of URL for resolving asset source Reviewed By: frantic Differential Revision: D5759789 fbshipit-source-id: daf33b6b66c4bff7f43213ee49286eafb6775f00 --- Libraries/Image/AssetSourceResolver.js | 20 ++++++------- .../__tests__/resolveAssetSource-test.js | 2 +- Libraries/Image/resolveAssetSource.js | 30 +++++++++---------- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/Libraries/Image/AssetSourceResolver.js b/Libraries/Image/AssetSourceResolver.js index e8a747148..53d80be0c 100644 --- a/Libraries/Image/AssetSourceResolver.js +++ b/Libraries/Image/AssetSourceResolver.js @@ -51,13 +51,13 @@ class AssetSourceResolver { serverUrl: ?string; // where the bundle is being run from - bundlePath: ?string; + bundleUrl: ?string; // the asset to resolve asset: PackagerAsset; - constructor(serverUrl: ?string, bundlePath: ?string, asset: PackagerAsset) { + constructor(serverUrl: ?string, bundleUrl: ?string, asset: PackagerAsset) { this.serverUrl = serverUrl; - this.bundlePath = bundlePath; + this.bundleUrl = bundleUrl; this.asset = asset; } @@ -66,7 +66,7 @@ class AssetSourceResolver { } isLoadedFromFileSystem(): boolean { - return !!this.bundlePath; + return !!this.bundleUrl; } defaultAsset(): ResolvedAssetSource { @@ -79,7 +79,7 @@ class AssetSourceResolver { this.drawableFolderInBundle() : this.resourceIdentifierWithoutScale(); } else { - return this.scaledAssetPathInBundle(); + return this.scaledAssetURLInBundle(); } } @@ -105,10 +105,10 @@ class AssetSourceResolver { /** * Resolves to where the bundle is running from, with a scaled asset filename - * E.g. '/sdcard/bundle/assets/AwesomeModule/icon@2x.png' + * E.g. 'file:///sdcard/bundle/assets/AwesomeModule/icon@2x.png' */ - scaledAssetPathInBundle(): ResolvedAssetSource { - const path = this.bundlePath || ''; + scaledAssetURLInBundle(): ResolvedAssetSource { + const path = this.bundleUrl || 'file://'; return this.fromSource(path + getScaledAssetPath(this.asset)); } @@ -129,9 +129,9 @@ class AssetSourceResolver { * E.g. 'file:///sdcard/AwesomeModule/drawable-mdpi/icon.png' */ drawableFolderInBundle(): ResolvedAssetSource { - const path = this.bundlePath || ''; + const path = this.bundleUrl || 'file://'; return this.fromSource( - 'file://' + path + getAssetPathInDrawableFolder(this.asset) + path + getAssetPathInDrawableFolder(this.asset) ); } diff --git a/Libraries/Image/__tests__/resolveAssetSource-test.js b/Libraries/Image/__tests__/resolveAssetSource-test.js index 061283cb1..7cd48f747 100644 --- a/Libraries/Image/__tests__/resolveAssetSource-test.js +++ b/Libraries/Image/__tests__/resolveAssetSource-test.js @@ -122,7 +122,7 @@ describe('resolveAssetSource', () => { __packager_asset: true, width: 100, height: 200, - uri: '/Path/To/Sample.app/assets/module/a/logo.png', + uri: 'file:///Path/To/Sample.app/assets/module/a/logo.png', scale: 1, }); }); diff --git a/Libraries/Image/resolveAssetSource.js b/Libraries/Image/resolveAssetSource.js index 71ec3715c..c2f4dba12 100644 --- a/Libraries/Image/resolveAssetSource.js +++ b/Libraries/Image/resolveAssetSource.js @@ -19,7 +19,7 @@ const NativeModules = require('NativeModules'); import type { ResolvedAssetSource } from 'AssetSourceResolver'; -let _customSourceTransformer, _serverURL, _bundleSourcePath; +let _customSourceTransformer, _serverURL, _bundleSourceURL; function getDevServerURL(): ?string { if (_serverURL === undefined) { @@ -36,28 +36,28 @@ function getDevServerURL(): ?string { return _serverURL; } -function getBundleSourcePath(): ?string { - if (_bundleSourcePath === undefined) { +function getBundleSourceURL(): ?string { + if (_bundleSourceURL === undefined) { const scriptURL = NativeModules.SourceCode.scriptURL; if (!scriptURL) { // scriptURL is falsy, we have nothing to go on here - _bundleSourcePath = null; - return _bundleSourcePath; + _bundleSourceURL = null; + return _bundleSourceURL; } if (scriptURL.startsWith('assets://')) { - // running from within assets, no offline path to use - _bundleSourcePath = null; - return _bundleSourcePath; + // android: running from within assets, no offline path to use + _bundleSourceURL = null; + return _bundleSourceURL; } - if (scriptURL.startsWith('file://')) { - // cut off the protocol - _bundleSourcePath = scriptURL.substring(7, scriptURL.lastIndexOf('/') + 1); - } else { - _bundleSourcePath = scriptURL.substring(0, scriptURL.lastIndexOf('/') + 1); + _bundleSourceURL = scriptURL.substring(0, scriptURL.lastIndexOf('/') + 1); + if (!scriptURL.startsWith('file://')) { + // Add file protocol in case we have an absolute file path and not a URL. + // This shouldn't really be necessary. scriptURL should be a URL. + _bundleSourceURL = 'file://' + _bundleSourceURL; } } - return _bundleSourcePath; + return _bundleSourceURL; } function setCustomSourceTransformer( @@ -80,7 +80,7 @@ function resolveAssetSource(source: any): ?ResolvedAssetSource { return null; } - const resolver = new AssetSourceResolver(getDevServerURL(), getBundleSourcePath(), asset); + const resolver = new AssetSourceResolver(getDevServerURL(), getBundleSourceURL(), asset); if (_customSourceTransformer) { return _customSourceTransformer(resolver); }