2015-04-21 17:48:54 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
* @providesModule resolveAssetSource
|
2015-05-12 16:50:41 +00:00
|
|
|
*
|
|
|
|
* Resolves an asset into a `source` for `Image`.
|
2015-04-21 17:48:54 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2015-05-08 00:30:41 +00:00
|
|
|
var AssetRegistry = require('AssetRegistry');
|
2015-04-22 23:31:13 +00:00
|
|
|
var PixelRatio = require('PixelRatio');
|
2015-05-12 16:50:41 +00:00
|
|
|
var Platform = require('Platform');
|
2015-04-21 17:48:54 +00:00
|
|
|
var SourceCode = require('NativeModules').SourceCode;
|
|
|
|
|
|
|
|
var _serverURL;
|
|
|
|
|
2015-05-12 16:50:41 +00:00
|
|
|
function getDevServerURL() {
|
|
|
|
if (!__DEV__) {
|
|
|
|
// In prod we want assets to be loaded from the archive
|
|
|
|
return null;
|
|
|
|
}
|
2015-04-21 17:48:54 +00:00
|
|
|
if (_serverURL === undefined) {
|
|
|
|
var scriptURL = SourceCode.scriptURL;
|
2015-04-22 20:11:30 +00:00
|
|
|
var match = scriptURL && scriptURL.match(/^https?:\/\/.*?\//);
|
|
|
|
if (match) {
|
2015-05-12 16:50:41 +00:00
|
|
|
// Bundle was loaded from network
|
2015-04-22 20:11:30 +00:00
|
|
|
_serverURL = match[0];
|
2015-04-21 17:48:54 +00:00
|
|
|
} else {
|
2015-05-12 16:50:41 +00:00
|
|
|
// Bundle was loaded from file
|
2015-04-21 17:48:54 +00:00
|
|
|
_serverURL = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return _serverURL;
|
|
|
|
}
|
|
|
|
|
2015-05-12 16:50:41 +00:00
|
|
|
/**
|
|
|
|
* Returns the path at which the asset can be found in the archive
|
|
|
|
*/
|
|
|
|
function getPathInArchive(asset) {
|
|
|
|
if (Platform.OS === 'android') {
|
|
|
|
var assetDir = getBasePath(asset);
|
|
|
|
// E.g. 'assets_awesomemodule_icon'
|
|
|
|
// The Android resource system picks the correct scale.
|
|
|
|
return (assetDir + '/' + asset.name)
|
|
|
|
.toLowerCase()
|
|
|
|
.replace(/\//g, '_') // Encode folder structure in file name
|
2015-06-01 19:09:52 +00:00
|
|
|
.replace(/([^a-z0-9_])/g, '') // Remove illegal chars
|
|
|
|
.replace(/^assets_/, ''); // Remove "assets_" prefix
|
2015-05-12 16:50:41 +00:00
|
|
|
} else {
|
|
|
|
// E.g. 'assets/AwesomeModule/icon@2x.png'
|
|
|
|
return getScaledAssetPath(asset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an absolute URL which can be used to fetch the asset
|
|
|
|
* from the devserver
|
|
|
|
*/
|
|
|
|
function getPathOnDevserver(devServerUrl, asset) {
|
|
|
|
return devServerUrl + getScaledAssetPath(asset) + '?hash=' + asset.hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a path like 'assets/AwesomeModule'
|
|
|
|
*/
|
|
|
|
function getBasePath(asset) {
|
|
|
|
// TODO(frantic): currently httpServerLocation is used both as
|
|
|
|
// path in http URL and path within IPA. Should we have zipArchiveLocation?
|
|
|
|
var path = asset.httpServerLocation;
|
|
|
|
if (path[0] === '/') {
|
|
|
|
path = path.substr(1);
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a path like 'assets/AwesomeModule/icon@2x.png'
|
|
|
|
*/
|
|
|
|
function getScaledAssetPath(asset) {
|
|
|
|
var scale = pickScale(asset.scales, PixelRatio.get());
|
|
|
|
var scaleSuffix = scale === 1 ? '' : '@' + scale + 'x';
|
|
|
|
var assetDir = getBasePath(asset);
|
|
|
|
return assetDir + '/' + asset.name + scaleSuffix + '.' + asset.type;
|
|
|
|
}
|
|
|
|
|
2015-04-22 23:31:13 +00:00
|
|
|
function pickScale(scales, deviceScale) {
|
|
|
|
// Packager guarantees that `scales` array is sorted
|
|
|
|
for (var i = 0; i < scales.length; i++) {
|
|
|
|
if (scales[i] >= deviceScale) {
|
|
|
|
return scales[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If nothing matches, device scale is larger than any available
|
|
|
|
// scales, so we return the biggest one. Unless the array is empty,
|
|
|
|
// in which case we default to 1
|
|
|
|
return scales[scales.length - 1] || 1;
|
|
|
|
}
|
|
|
|
|
2015-04-21 17:48:54 +00:00
|
|
|
function resolveAssetSource(source) {
|
2015-05-08 00:30:41 +00:00
|
|
|
if (typeof source === 'object') {
|
2015-04-22 20:11:30 +00:00
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
2015-05-08 00:30:41 +00:00
|
|
|
var asset = AssetRegistry.getAssetByID(source);
|
|
|
|
if (asset) {
|
|
|
|
return assetToImageSource(asset);
|
2015-04-21 17:48:54 +00:00
|
|
|
}
|
|
|
|
|
2015-05-08 00:30:41 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function assetToImageSource(asset) {
|
2015-05-12 16:50:41 +00:00
|
|
|
var devServerURL = getDevServerURL();
|
|
|
|
if (devServerURL) {
|
2015-04-22 20:11:30 +00:00
|
|
|
return {
|
2015-05-08 00:30:41 +00:00
|
|
|
width: asset.width,
|
|
|
|
height: asset.height,
|
2015-05-12 16:50:41 +00:00
|
|
|
uri: getPathOnDevserver(devServerURL, asset),
|
2015-04-22 20:11:30 +00:00
|
|
|
isStatic: false,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return {
|
2015-05-08 00:30:41 +00:00
|
|
|
width: asset.width,
|
|
|
|
height: asset.height,
|
2015-05-12 16:50:41 +00:00
|
|
|
uri: getPathInArchive(asset),
|
2015-04-22 20:11:30 +00:00
|
|
|
isStatic: true,
|
|
|
|
};
|
2015-04-21 17:48:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = resolveAssetSource;
|
2015-04-22 23:31:13 +00:00
|
|
|
module.exports.pickScale = pickScale;
|