ElectronStorageHelper.js
1.31 KB
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
const fs = require('fs');
const path = require('path');
const staticAssets = path.resolve(__static, 'assets');
/**
* Allow the storage module to load files bundled in the Electron application.
*/
class ElectronStorageHelper {
constructor (storageInstance) {
this.parent = storageInstance;
}
/**
* Fetch an asset but don't process dependencies.
* @param {AssetType} assetType - The type of asset to fetch.
* @param {string} assetId - The ID of the asset to fetch: a project ID, MD5, etc.
* @param {DataFormat} dataFormat - The file format / file extension of the asset to fetch: PNG, JPG, etc.
* @return {Promise.<Asset>} A promise for the contents of the asset.
*/
load (assetType, assetId, dataFormat) {
assetId = path.basename(assetId);
dataFormat = path.basename(dataFormat);
return new Promise((resolve, reject) => {
fs.readFile(
path.resolve(staticAssets, `${assetId}.${dataFormat}`),
(err, data) => {
if (err) {
reject(err);
} else {
resolve(new this.parent.Asset(assetType, assetId, dataFormat, data));
}
}
);
});
}
}
module.exports = ElectronStorageHelper;