Skip to content

Commit e799d05

Browse files
author
Ray Schamp
committed
feat: add Storage.store
Storage.store attempts an update or create on the webHelper first, and then supplies the resulting id to BuiltinHelper to cache the result.
1 parent 042300a commit e799d05

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/ScratchStorage.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,27 @@ class ScratchStorage {
183183
tryNextHelper();
184184
});
185185
}
186+
187+
/**
188+
* Store an asset by type & ID.
189+
* @param {AssetType} assetType - The type of asset to fetch. This also determines which asset store to use.
190+
* @param {DataFormat} [dataFormat] - Optional: load this format instead of the AssetType's default.
191+
* @param {DataFormat} [data] - Optional: store this data, rather than the data in the asset
192+
* @param {?string} assetId - The ID of the asset to fetch: a project ID, MD5, etc.
193+
* @return {Promise.<id, title>} A promise for asset metadata
194+
*/
195+
store (assetType, dataFormat, data, assetId) {
196+
dataFormat = dataFormat || assetType.runtimeFormat;
197+
return new Promise(
198+
(resolve, reject) =>
199+
this.webHelper.store(assetType, dataFormat, data, assetId)
200+
.then(body => {
201+
this.builtinHelper.store(assetType, dataFormat, data, body.id);
202+
return resolve(body);
203+
})
204+
.catch(error => reject(error))
205+
);
206+
}
186207
}
187208

188209
module.exports = ScratchStorage;

src/WebHelper.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,56 @@ class WebHelper extends Helper {
119119
tryNextSource();
120120
});
121121
}
122+
123+
/**
124+
* Create or update an asset with provided data. The create function is called if no asset id is provided
125+
* @param {AssetType} assetType - The type of asset to create or update.
126+
* @param {?DataFormat} dataFormat - DataFormat of the data for the stored asset.
127+
* @param {Buffer} data - The data for the cached asset.
128+
* @param {?string} assetId - The ID of the asset to fetch: a project ID, MD5, etc.
129+
* @return {Promise.<object>} A promise for the response from the create or update request
130+
*/
131+
store (assetType, dataFormat, data, assetId) {
132+
const asset = new Asset(assetType, assetId, dataFormat);
133+
// If we have an asset id, we should update, otherwise create to get an id
134+
const create = assetId === null || typeof assetId === 'undefined';
135+
136+
// Use the first source with the appropriate asset type and url function
137+
const store = this.stores.filter(s =>
138+
// Only use stores for the incoming asset type
139+
s.types.indexOf(assetType.name) !== -1 && (
140+
// Only use stores that have a create function if this is a create request
141+
// or an update function if this is an update request
142+
(create && s.create) || s.update
143+
)
144+
)[0];
145+
146+
const method = create ? 'post' : 'put';
147+
148+
return new Promise((resolve, reject) => {
149+
if (!store) return reject('No appropriate stores');
150+
151+
let reqConfig = create ? store.create(asset) : store.update(asset);
152+
if (typeof reqConfig === 'string') {
153+
reqConfig = {
154+
url: reqConfig
155+
};
156+
}
157+
return nets({
158+
body: data,
159+
method: method,
160+
...reqConfig
161+
}, (err, resp, body) => {
162+
if (err || Math.floor(resp.statusCode / 100) !== 2) {
163+
return reject(err || resp.statusCode);
164+
}
165+
return resolve({
166+
id: body['content-name'],
167+
...body
168+
});
169+
});
170+
});
171+
}
122172
}
123173

124174
module.exports = WebHelper;

0 commit comments

Comments
 (0)