-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetcher.js
81 lines (67 loc) · 1.99 KB
/
fetcher.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
/* jshint node:true */
/* jshint esversion:6 */
'use strict';
const express = require('express');
const app = express();
const AssetPoolClass = require("@brightsign/assetpool");
const AssetPoolFetcherClass = require("@brightsign/assetpoolfetcher");
const fs = require('fs');
const path = require('path');
const httpRequest = require('request');
let syncSpecUrl = "http://10.0.0.88:8081/current-sync.json";
function main() {
app.get('/fetch-files',
function(req, res /*, next*/ ) {
start()
.then(function() {
res.send('Asset fetch completed');
});
});
app.listen(9090, function() {
console.log('Example app listening on port 9090!');
});
}
function start() {
return new Promise(function(resolve, /*reject*/ ) {
console.log(`initializing download`);
const storage = '/storage/sd/';
const poolpathname = path.join(storage, "myassets");
console.log(`poolpathname: '${poolpathname}'.`);
if (!fs.existsSync(poolpathname)) {
fs.mkdirSync(poolpathname);
}
const assetpool = new AssetPoolClass(poolpathname);
const assetpoolfetcher = new AssetPoolFetcherClass(assetpool);
assetpoolfetcher.addEventListener("fileevent", function(data) {
console.log(`fileCompleteEvent: ${JSON.stringify(data.detail)}`);
});
getSyncSpec(syncSpecUrl)
.then(function(syncSpec) {
return assetpoolfetcher.start(syncSpec.files.download);
})
.then(function() {
console.log(`Download completed.`);
resolve();
})
.catch(function(err) {
console.log(`Download failed with '${err}'.`);
resolve();
});
});
}
function getSyncSpec(url) {
return new Promise(function(resolve, reject) {
httpRequest({
url: url
}, function(error, res, body) {
if (error) {
console.log(`http request error: '${error}'.`);
reject(error);
} else {
const result = JSON.parse(body);
resolve(result);
}
});
});
}
window.main = main;