File size: 1,388 Bytes
30c32c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const StringUtil = require('./string-util');

class AssetUtil {
    /**
     * @param {Runtime} runtime runtime with storage attached
     * @param {JSZip} zip optional JSZip to search for asset in
     * @param {Storage.assetType} assetType scratch-storage asset type
     * @param {string} md5ext full md5 with file extension
     * @returns {Promise<Storage.Asset>} scratch-storage asset object
     */
    static getByMd5ext(runtime, zip, assetType, md5ext) {
        const storage = runtime.storage;
        const idParts = StringUtil.splitFirst(md5ext, '.');
        const md5 = idParts[0];
        const ext = idParts[1].toLowerCase();

        if (zip) {
            // Search the root of the zip
            let file = zip.file(md5ext);

            // Search subfolders of the zip
            // This matches behavior of deserialize-assets.js
            if (!file) {
                const fileMatch = new RegExp(`^([^/]*/)?${md5ext}$`);
                file = zip.file(fileMatch)[0];
            }

            if (file) {
                return file.async('uint8array').then(data => runtime.storage.createAsset(
                    assetType,
                    ext,
                    data,
                    md5,
                    false
                ));
            }
        }

        return storage.load(assetType, md5, ext);
    }
}

module.exports = AssetUtil;