-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathstorage.js
92 lines (88 loc) · 3.15 KB
/
storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import {ScratchStorage} from 'scratch-storage';
import defaultProject from './default-project';
/**
* Wrapper for ScratchStorage which adds default web sources.
* @todo make this more configurable
*/
class Storage extends ScratchStorage {
constructor () {
super();
this.cacheDefaultProject();
}
addOfficialScratchWebStores () {
this.addWebStore(
[this.AssetType.Project],
this.getProjectGetConfig.bind(this),
this.getProjectCreateConfig.bind(this),
this.getProjectUpdateConfig.bind(this)
);
this.addWebStore(
[this.AssetType.ImageVector, this.AssetType.ImageBitmap, this.AssetType.Sound],
this.getAssetGetConfig.bind(this),
// We set both the create and update configs to the same method because
// storage assumes it should update if there is an assetId, but the
// asset store uses the assetId as part of the create URI.
this.getAssetCreateConfig.bind(this),
this.getAssetCreateConfig.bind(this)
);
this.addWebStore(
[this.AssetType.Sound],
asset => `static/extension-assets/scratch3_music/${asset.assetId}.${asset.dataFormat}`
);
}
setProjectHost (projectHost) {
this.projectHost = projectHost;
}
setProjectToken (projectToken) {
this.projectToken = projectToken;
}
getProjectGetConfig (projectAsset) {
const path = `${this.projectHost}/${projectAsset.assetId}`;
const qs = this.projectToken ? `?token=${this.projectToken}` : '';
return path + qs;
}
getProjectCreateConfig () {
return {
url: `${this.projectHost}/`,
withCredentials: true
};
}
getProjectUpdateConfig (projectAsset) {
return {
url: `${this.projectHost}/${projectAsset.assetId}`,
withCredentials: true
};
}
setAssetHost (assetHost) {
this.assetHost = assetHost;
}
getAssetGetConfig (asset) {
return `${this.assetHost}/internalapi/asset/${asset.assetId}.${asset.dataFormat}/get/`;
}
getAssetCreateConfig (asset) {
return {
// There is no such thing as updating assets, but storage assumes it
// should update if there is an assetId, and the asset store uses the
// assetId as part of the create URI. So, force the method to POST.
// Then when storage finds this config to use for the "update", still POSTs
method: 'post',
url: `${this.assetHost}/${asset.assetId}.${asset.dataFormat}`,
withCredentials: true
};
}
setTranslatorFunction (translator) {
this.translator = translator;
this.cacheDefaultProject();
}
cacheDefaultProject () {
const defaultProjectAssets = defaultProject(this.translator);
defaultProjectAssets.forEach(asset => this.builtinHelper._store(
this.AssetType[asset.assetType],
this.DataFormat[asset.dataFormat],
asset.data,
asset.id
));
}
}
const storage = new Storage();
export default storage;