2017-04-20 19:17:05 -04:00
|
|
|
const ScratchStorage = require('scratch-storage');
|
2017-03-09 18:50:43 -05:00
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
const ASSET_SERVER = 'https://cdn.assets.scratch.mit.edu/';
|
|
|
|
const PROJECT_SERVER = 'https://cdn.projects.scratch.mit.edu/';
|
2017-03-09 18:50:43 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Asset} asset - calculate a URL for this asset.
|
|
|
|
* @returns {string} a URL to download a project file.
|
|
|
|
*/
|
2017-04-20 19:17:05 -04:00
|
|
|
const getProjectUrl = function (asset) {
|
|
|
|
const assetIdParts = asset.assetId.split('.');
|
|
|
|
const assetUrlParts = [PROJECT_SERVER, 'internalapi/project/', assetIdParts[0], '/get/'];
|
2017-03-09 18:50:43 -05:00
|
|
|
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.)
|
|
|
|
*/
|
2017-04-20 19:17:05 -04:00
|
|
|
const getAssetUrl = function (asset) {
|
|
|
|
const assetUrlParts = [
|
2017-03-09 18:50:43 -05:00
|
|
|
ASSET_SERVER,
|
|
|
|
'internalapi/asset/',
|
|
|
|
asset.assetId,
|
|
|
|
'.',
|
|
|
|
asset.assetType.runtimeFormat,
|
|
|
|
'/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.
|
|
|
|
*/
|
2017-04-20 19:17:05 -04:00
|
|
|
const attachTestStorage = function (vm) {
|
|
|
|
const storage = new ScratchStorage();
|
|
|
|
const AssetType = storage.AssetType;
|
2017-03-09 18:50:43 -05:00
|
|
|
storage.addWebSource([AssetType.Project], getProjectUrl);
|
|
|
|
storage.addWebSource([AssetType.ImageVector, AssetType.ImageBitmap, AssetType.Sound], getAssetUrl);
|
|
|
|
vm.attachStorage(storage);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = attachTestStorage;
|