-
Notifications
You must be signed in to change notification settings - Fork 2
/
node_helper.js
438 lines (402 loc) · 15.2 KB
/
node_helper.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
"use strict";
const fs = require("fs");
const { writeFile, readFile } = require("fs/promises");
const path = require("path");
const moment = require("moment");
const OneDrivePhotos = require("./OneDrivePhotos.js");
const { Readable } = require("stream");
const { finished } = require("stream/promises");
const { RE2 } = require("re2-wasm");
const { Set } = require('immutable');
const NodeHelper = require("node_helper");
const Log = require("logger");
const { shuffle } = require("./shuffle.js");
const { error_to_string } = require("./error_to_string");
const ONE_DAY = 24 * 60 * 60 * 1000; // 1 day in milliseconds
/**
* @type {OneDrivePhotos}
*/
let OneDrivePhoto = null;
const NodeHeleprObject = {
start: function () {
this.scanInterval = 1000 * 60 * 55; // fixed. no longer needs to be fixed
this.config = {};
this.scanTimer = null;
/** @type {microsoftgraph.DriveItem} */
this.selecetedAlbums = [];
/** @type {OneDriveMediaItem[]} */
this.localPhotoList = [];
this.localPhotoPntr = 0;
this.lastLocalPhotoPntr = 0;
this.queue = null;
this.uploadAlbumId;
this.initializeTimer = null;
this.CACHE_ALBUMNS_PATH = path.resolve(this.path, "cache", "selecetedAlbumsCache.json");
this.CACHE_PHOTOLIST_PATH = path.resolve(this.path, "cache", "photoListCache.json");
this.CACHE_CONFIG = path.resolve(this.path, "cache", "config.json");
},
socketNotificationReceived: function (notification, payload) {
switch (notification) {
case "INIT":
this.initializeAfterLoading(payload);
break;
case "UPLOAD":
this.upload(payload);
break;
case "IMAGE_LOAD_FAIL":
{
const { url, event, source, lineno, colno, error } = payload;
Log.error("[ONEDRIVE] hidden.onerror", { event, source, lineno, colno });
if (error) {
Log.error("[ONEDRIVE] hidden.onerror error", error.message, error.name, error.stack);
}
Log.error("Image loading fails. Check your network.:", url);
this.prepAndSendChunk(Math.ceil((20 * 60 * 1000) / this.config.updateInterval)).then(); // 20min * 60s * 1000ms / updateinterval in ms
}
break;
case "IMAGE_LOADED":
{
const { id, index } = payload;
this.log_debug("Image loaded:", `${this.lastLocalPhotoPntr} + ${index}`, id);
}
break;
case "NEED_MORE_PICS":
{
Log.info("Used last pic in list");
this.prepAndSendChunk(Math.ceil((20 * 60 * 1000) / this.config.updateInterval)).then(); // 20min * 60s * 1000ms / updateinterval in ms
}
break;
case "MODULE_SUSPENDED_SKIP_UPDATE":
this.log_debug("Module is suspended so skip the UI update");
break;
default:
Log.error("Unknown notification received", notification);
}
},
log_debug: function (...args) {
if (this.debug) Log.info("[ONEDRIVE] [node_helper]", ...args);
},
upload: async function (path) {
if (!this.uploadAlbumId) {
Log.info("No uploadable album exists.");
return;
}
let uploadToken = await OneDrivePhoto.upload(path);
if (uploadToken) {
await OneDrivePhoto.create(uploadToken, this.uploadAlbumId);
Log.info("Upload completed.");
} else {
Log.error("Upload Fails.");
}
},
initializeAfterLoading: function (config) {
this.config = config;
this.debug = config.debug ? config.debug : false;
if (!this.config.scanInterval || this.config.scanInterval < 1000 * 60 * 10) this.config.scanInterval = 1000 * 60 * 10;
OneDrivePhoto = new OneDrivePhotos({
debug: this.debug,
config: config,
});
this.albumsFilters = [];
for (let album of config.albums) {
if (album.hasOwnProperty("source") && album.hasOwnProperty("flags")) {
this.albumsFilters.push(new RE2(album.source, album.flags + 'u'));
} else {
this.albumsFilters.push(album);
}
}
delete this.config.albums;
this.tryToIntitialize();
},
tryToIntitialize: async function () {
//set timer, in case if fails to retry in 1 min
clearTimeout(this.initializeTimer);
this.initializeTimer = setTimeout(
() => {
this.tryToIntitialize();
},
1 * 60 * 1000,
);
Log.info("Starting Initialization");
//load cached album list - if available
const cacheAlbumDt = new Date(await this.readCacheConfig("CACHE_ALBUMNS_PATH"));
const notExpiredCacheAlbum = cacheAlbumDt && (Date.now() - cacheAlbumDt.getTime() < ONE_DAY);
if (notExpiredCacheAlbum && fs.existsSync(this.CACHE_ALBUMNS_PATH)) {
Log.info("Loading cached albumns list");
try {
const data = await readFile(this.CACHE_ALBUMNS_PATH, "utf-8");
this.selecetedAlbums = JSON.parse(data.toString());
this.log_debug("successfully loaded selecetedAlbums");
this.sendSocketNotification("UPDATE_ALBUMS", this.selecetedAlbums); // for fast startup
} catch (err) {
Log.error("unable to load selecetedAlbums cache", err);
}
}
//load cached list - if available
const cachePhotoListDt = new Date(await this.readCacheConfig("CACHE_PHOTOLIST_PATH"));
const notExpiredCachePhotoList = cachePhotoListDt && (Date.now() - cachePhotoListDt.getTime() < ONE_DAY);
if (notExpiredCachePhotoList && fs.existsSync(this.CACHE_PHOTOLIST_PATH)) {
Log.info("Loading cached albumns list");
try {
const data = await readFile(this.CACHE_PHOTOLIST_PATH, "utf-8");
this.localPhotoList = JSON.parse(data.toString());
if (this.config.sort === "random") {
shuffle(this.localPhotoList);
}
this.log_debug("successfully loaded photo list cache of ", this.localPhotoList.length, " photos");
await this.prepAndSendChunk(5); // only 5 for extra fast startup
} catch (err) {
Log.error("unable to load photo list cache", err);
}
}
Log.info("Initialization complete!");
clearTimeout(this.initializeTimer);
Log.info("Start first scanning.");
this.startScanning();
},
prepAndSendChunk: async function (desiredChunk = 20) {
if (this.lastScanTime && (new Date() - this.lastScanTime) < 30000) {
return;
// await sleep(30000 - (new Date() - this.lastScanTime));
}
this.lastScanTime = new Date();
this.log_debug("prepAndSendChunk");
try {
//find which ones to refresh
if (this.localPhotoPntr < 0 || this.localPhotoPntr >= this.localPhotoList.length) {
this.localPhotoPntr = 0;
this.lastLocalPhotoPntr = 0;
}
let numItemsToRefresh = Math.min(desiredChunk, this.localPhotoList.length - this.localPhotoPntr, 20); //20 is api limit
this.log_debug("num to ref: ", numItemsToRefresh, ", DesChunk: ", desiredChunk, ", totalLength: ", this.localPhotoList.length, ", Pntr: ", this.localPhotoPntr);
const cachePath = this.path + "/cache/";
/**
* refresh them
* @type {OneDriveMediaItem[]}
*/
let list = [];
if (numItemsToRefresh > 0) {
list = await OneDrivePhoto.updateTheseMediaItems(this.localPhotoList.slice(this.localPhotoPntr, this.localPhotoPntr + numItemsToRefresh), cachePath);
}
if (list.length > 0) {
// update the localList
this.localPhotoList.splice(this.localPhotoPntr, list.length, ...list);
// send updated pics
this.sendSocketNotification("MORE_PICS", list);
// update pointer
this.lastLocalPhotoPntr = this.localPhotoPntr;
this.localPhotoPntr = this.localPhotoPntr + list.length;
this.log_debug("refreshed: ", list.length, ", totalLength: ", this.localPhotoList.length, ", Pntr: ", this.localPhotoPntr);
} else {
Log.error("couldn't send ", list.length, " pics");
}
} catch (err) {
Log.error("failed to refresh and send chunk: ");
Log.error(error_to_string(err));
}
},
/** @returns {microsoftgraph.DriveItem[]} */
getAlbums: async function () {
try {
let r = await OneDrivePhoto.getAlbums();
r.forEach(item => {
item.title = item.name;
});
return r;
} catch (err) {
Log.error(error_to_string(err));
}
},
startScanning: function () {
const fn = () => {
const nextScanDt = new Date(Date.now() + this.scanInterval);
this.scanJob().then(() => {
Log.info("Next scan will be at", nextScanDt);
});
};
// set up interval, then 1 fail won't stop future scans
this.scanTimer = setInterval(fn, this.scanInterval);
// call for first time
fn();
},
scanJob: async function () {
Log.info("Start Album scanning");
this.queue = null;
await this.getAlbumList();
try {
if (this.selecetedAlbums.length > 0) {
this.photos = await this.getImageList();
return true;
} else {
Log.warn("There is no album to get photos.");
return false;
}
} catch (err) {
Log.error(error_to_string(err));
}
},
getAlbumList: async function () {
Log.info("Getting album list");
/**
* @type {microsoftgraph.DriveItem[]}
*/
let albums = await this.getAlbums();
/**
* @type {microsoftgraph.DriveItem[]}
*/
let selecetedAlbums = [];
for (let ta of this.albumsFilters) {
const matches = albums.filter((a) => {
if (ta instanceof RE2) {
Log.debug(`RE2 match ${ta.source} -> '${a.title}' : ${ta.test(a.title)}`);
return ta.test(a.title);
}
else {
return ta === a.title;
}
});
if (matches.length === 0) {
Log.warn(`Can't find "${ta instanceof RE2 ? ta.source : ta}" in your album list.`);
}
else {
selecetedAlbums.push(...matches);
}
}
selecetedAlbums = Set(selecetedAlbums).toArray();
Log.info("Finish Album scanning. Properly scanned :", selecetedAlbums.length);
Log.info("Albums:", selecetedAlbums.map((a) => a.title).join(", "));
this.writeFileSafe(this.CACHE_ALBUMNS_PATH, JSON.stringify(selecetedAlbums, null, 4), "Album list cache");
this.saveCacheConfig("CACHE_ALBUMNS_PATH", new Date().toISOString());
for (let a of selecetedAlbums) {
let url = a.coverPhotoBaseUrl;
let fpath = path.join(this.path, "cache", a.id);
let file = fs.createWriteStream(fpath);
const response = await fetch(url);
await finished(Readable.fromWeb(response.body).pipe(file));
}
this.selecetedAlbums = selecetedAlbums;
Log.info("getAlbumList done");
this.sendSocketNotification("INITIALIZED", selecetedAlbums);
},
getImageList: async function () {
let condition = this.config.condition;
/**
* @param {OneDriveMediaItem} photo
*/
let photoCondition = (photo) => {
if (!photo.hasOwnProperty("mediaMetadata")) return false;
let data = photo.mediaMetadata;
if (data.hasOwnProperty("video")) return false;
if (!data.hasOwnProperty("photo")) return false;
let ct = moment(data.creationTime);
if (condition.fromDate && moment(condition.fromDate).isAfter(ct)) return false;
if (condition.toDate && moment(condition.toDate).isBefore(ct)) return false;
if (condition.minWidth && Number(condition.minWidth) > Number(data.width)) return false;
if (condition.minHeight && Number(condition.minHeight) > Number(data.height)) return false;
if (condition.maxWidth && Number(condition.maxWidth) < Number(data.width)) return false;
if (condition.maxHeight && Number(condition.maxHeight) < Number(data.height)) return false;
let whr = Number(data.width) / Number(data.height);
if (condition.minWHRatio && Number(condition.minWHRatio) > whr) return false;
if (condition.maxWHRatio && Number(condition.maxWHRatio) < whr) return false;
return true;
};
// let sort = (a, b) => {
// let at = moment(a.mediaMetadata.creationTime);
// let bt = moment(b.mediaMetadata.creationTime);
// if (at.isBefore(bt) && this.config.sort === "new") return 1;
// if (at.isAfter(bt) && this.config.sort === "old") return 1;
// return -1;
// };
/** @type {OneDriveMediaItem[]} */
let photos = [];
try {
for (let album of this.selecetedAlbums) {
this.log_debug(`Prepare to get photo list from '${album.title}'`);
let list = await OneDrivePhoto.getImageFromAlbum(album.id, photoCondition);
list.forEach(i => {
i._albumTitle = album.title;
});
this.log_debug(`Got ${list.length} photo(s) from '${album.title}'`);
photos = photos.concat(list);
}
if (photos.length > 0) {
if (this.config.sort === "new" || this.config.sort === "old") {
photos.sort((a, b) => {
let at = moment(a.mediaMetadata.creationTime);
let bt = moment(b.mediaMetadata.creationTime);
if (at.isBefore(bt) && this.config.sort === "new") return 1;
if (at.isAfter(bt) && this.config.sort === "old") return 1;
return -1;
});
} else {
shuffle(photos);
}
Log.info(`Total indexed photos: ${photos.length}`);
this.localPhotoList = [...photos];
this.localPhotoPntr = 0;
this.lastLocalPhotoPntr = 0;
this.prepAndSendChunk(50).then();
this.writeFileSafe(this.CACHE_PHOTOLIST_PATH, JSON.stringify(photos, null, 4), "Photo list cache");
this.saveCacheConfig("CACHE_PHOTOLIST_PATH", new Date().toISOString());
}
return photos;
} catch (err) {
Log.error(error_to_string(err));
}
},
stop: function () {
clearInterval(this.scanTimer);
},
readFileSafe: async function (filePath, fileDescription) {
try {
const data = await readFile(filePath, "utf-8");
return data.toString();
} catch (err) {
Log.error(`unable to read ${fileDescription}: ${filePath}`);
Log.error(error_to_string(err));
}
},
writeFileSafe: async function (filePath, data, fileDescription) {
try {
await writeFile(filePath, data);
this.log_debug(fileDescription + " saved");
} catch (err) {
Log.error(`unable to write ${fileDescription}: ${filePath}`);
Log.error(error_to_string(err));
}
},
readCacheConfig: async function (key) {
try {
let config = {};
if (fs.existsSync(this.CACHE_CONFIG)) {
const configStr = await this.readFileSafe(this.CACHE_CONFIG, "Cache Config");
config = JSON.parse(configStr);
}
if (Object(config).hasOwnProperty(key)) {
return config[key];
}
else {
return undefined;
}
} catch (err) {
Log.error(`unable to read Cache Config`);
Log.error(error_to_string(err));
}
},
saveCacheConfig: async function (key, value) {
try {
let config = {};
if (fs.existsSync(this.CACHE_CONFIG)) {
const configStr = await this.readFileSafe(this.CACHE_CONFIG, "Cache Config");
config = JSON.parse(configStr);
}
config[key] = value;
await this.writeFileSafe(this.CACHE_CONFIG, JSON.stringify(config, null, 4), "Cache Config");
this.log_debug("Cache Config saved");
} catch (err) {
Log.error(`unable to write Cache Config`);
Log.error(error_to_string(err));
}
},
};
module.exports = NodeHelper.create(NodeHeleprObject);