scratch-vm/test/fixtures/attach-test-storage.js
Christopher Willis-Ford eb931fd99b Specify dataFormat when loading asset for import
When importing a project we know the file extension for each asset to be
loaded. This change provides that information to the storage system so
that we can load assets which don't use the default. For example, this
allows loading JPG-format backdrops.

In support of this change, there's a new function on `StringUtil` called
`splitFirst`, which splits a string on the first instance of a separator
character. This change includes unit tests for this new function.
2017-05-24 15:35:10 -07:00

47 lines
1.5 KiB
JavaScript

const ScratchStorage = require('scratch-storage');
const ASSET_SERVER = 'https://cdn.assets.scratch.mit.edu/';
const PROJECT_SERVER = 'https://cdn.projects.scratch.mit.edu/';
/**
* @param {Asset} asset - calculate a URL for this asset.
* @returns {string} a URL to download a project file.
*/
const getProjectUrl = function (asset) {
const assetIdParts = asset.assetId.split('.');
const assetUrlParts = [PROJECT_SERVER, 'internalapi/project/', assetIdParts[0], '/get/'];
if (assetIdParts[1]) {
assetUrlParts.push(assetIdParts[1]);
}
return assetUrlParts.join('');
};
/**
* @param {Asset} asset - calculate a URL for this asset.
* @returns {string} a URL to download a project asset (PNG, WAV, etc.)
*/
const getAssetUrl = function (asset) {
const assetUrlParts = [
ASSET_SERVER,
'internalapi/asset/',
asset.assetId,
'.',
asset.dataFormat,
'/get/'
];
return assetUrlParts.join('');
};
/**
* Construct a new instance of ScratchStorage, provide it with default web sources, and attach it to the provided VM.
* @param {VirtualMachine} vm - the VM which will own the new ScratchStorage instance.
*/
const attachTestStorage = function (vm) {
const storage = new ScratchStorage();
const AssetType = storage.AssetType;
storage.addWebSource([AssetType.Project], getProjectUrl);
storage.addWebSource([AssetType.ImageVector, AssetType.ImageBitmap, AssetType.Sound], getAssetUrl);
vm.attachStorage(storage);
};
module.exports = attachTestStorage;