-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp-controller.js
More file actions
289 lines (221 loc) · 8.8 KB
/
Copy pathapp-controller.js
File metadata and controls
289 lines (221 loc) · 8.8 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
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
const fs = require('fs');
const path = require('path');
const lo = require('lodash');
const moment = require('moment');
const config = require('./config.json');
const util = require('./util');
const { log } = require('./log');
const __referenceStoredItem = {
mediaItem: {},
appData: {
mediaItemGet: {
at: Date.now()
},
probe: {
at: Date.now(),
contentLength: 123
},
download: {
at: Date.now(),
contentLength: 123
}
}
};
/**
MediaItem is a Google Photos API data structure containing item metadata.
StoredItem is MediaItem + app metadata { mediaItem, appData }
*/
class AppController {
constructor(photoDb, albumDb, googlePhotos, downloadPath) {
this.photoDb = photoDb;
this.albumDb = albumDb;
this.googlePhotos = googlePhotos;
this.downloadPath = downloadPath;
}
onAlbums(albums) {
log.info(this, 'onAlbums');
const maxTitleStringLen = albums.reduce((prev, curr) => Math.max(prev, curr.title.length), 0);
albums.forEach(album => {
album.items = [];
this.albumDb.set(album.id, album);
log.info(this, album.title.padEnd(maxTitleStringLen), album.id)
});
}
onRefreshAlbum(album, items) {
log.info(this, 'onRefreshAlbum');
album.items = items;
let albums = [album];
this.onAlbums(albums);
}
onMediaItemsDownloaded(mediaItems) {
log.info(this, 'onMediaItemsDownloaded', mediaItems.length);
const storedItems = mediaItems.map(mediaItem => {
let storedItem = this.photoDb.get(mediaItem.id);
if (storedItem == null) {
storedItem = { mediaItem, appData: {} };
} else {
storedItem.mediaItem = mediaItem;
}
return this.photoDb.set(mediaItem.id, storedItem);
});
log.info(this, 'onMediaItemsDownloaded total media items', this.photoDb.getAll().length);
this._fixFilenamesForDuplicates();
return storedItems;
}
_fixFilenamesForDuplicates() {
const duplicates = this._getStoredItemsWithDuplicateFilenames();
log.info(this, 'fixFilenamesForDuplicates found duplicates', Object.keys(duplicates).length);
for (let filename in duplicates) {
duplicates[filename].storedItems.forEach((storedItem, idx) => {
storedItem.altFilename = `${idx}_${filename}`;
this.photoDb.set(storedItem.mediaItem.id, storedItem);
});
}
}
_getStoredItemsWithDuplicateFilenames() {
const counts = {
// 'filename': { count: 0, ids: [] }
};
this._getAllMediaItems()
.filter(storedItem => !storedItem.altFilename)
.map(storedItem => {
const lowercaseFilename = storedItem.mediaItem.filename.toLowerCase();
const count = counts[lowercaseFilename] || { count: 0, storedItems: [] };
count.count++;
count.storedItems.push(storedItem);
counts[lowercaseFilename] = count;
});
const duplicates = {};
Object.keys(counts)
.filter(filename => counts[filename].count > 1)
.forEach(filename => duplicates[filename] = counts[filename]);
return duplicates;
}
_getAllMediaItems() {
return this.photoDb.getByFilter(value => value.mediaItem);
}
findMediaItemIdsToProbe(renewIfOlderThanDays, numberOfItems) {
log.verbose(this, 'findMediaItemIdsToProbe', renewIfOlderThanDays, numberOfItems);
const storedItemsToProbe = this.photoDb.getByFilter(
this._createProbeFilterFn(renewIfOlderThanDays)
).slice(0, numberOfItems);
log.verbose(this, 'storedItemsToProbe', storedItemsToProbe.length);
return storedItemsToProbe.map(item => item.mediaItem.id);
}
_createProbeFilterFn(renewIfOlderThanDays) {
return value => {
let probeDataIsOld = true;
if (value.appData.probe) {
const diff = util.diffBetweenTwoDates(value.appData.probe.at, Date.now(), 'days');
probeDataIsOld = diff >= renewIfOlderThanDays;
}
return probeDataIsOld;
};
}
onProbedMediaItems(contentLengthMap) {
log.info(this, 'onProbedMediaItems', Object.keys(contentLengthMap).length);
const keys = Object.keys(contentLengthMap);
const storedItems = keys.map(key => {
const storedItem = this.photoDb.get(key);
const probeData = storedItem.appData.probe || { at: 0, contentLength: 0 };
probeData.at = Date.now();
probeData.contentLength = Number(contentLengthMap[key]);
storedItem.appData.probe = probeData;
this.photoDb.set(storedItem.mediaItem.id, storedItem);
return storedItem;
});
const forDownload = this._chooseFilesForDownload(storedItems, contentLengthMap);
log.verbose(this, 'onProbedMediaItems for download', forDownload.length);
// resets download metadata of updated items so they can be redownloaded
forDownload
.map(storedItem => {
storedItem.appData.download = null;
return storedItem;
})
.map(storedItem => this.photoDb.set(storedItem.mediaItem.id, storedItem));
}
_chooseFilesForDownload(storedItems) {
return storedItems.filter(storedItem => {
if (storedItem.appData.probe && storedItem.appData.download) {
const pcl = storedItem.appData.probe.contentLength;
const dcl = storedItem.appData.download.contentLength;
return pcl !== dcl;
}
return false;
});
}
async renewMediaItems(mediaItemIds) {
log.info(this, 'renewMediaItems', mediaItemIds.length);
const mediaItems = await this.googlePhotos.batchGet(mediaItemIds);
return this.onMediaItemsDownloaded(mediaItems);
}
findMediaItemsToDownload(numberOfItems) {
log.verbose(this, 'findMediaItemsToDownload', numberOfItems);
const storedItemsToDownload = this.photoDb.getByFilter(
this._createDownloadFilterFn(), numberOfItems
).slice(0, numberOfItems);
log.verbose(this, 'findMediaItemsToDownload stored items', storedItemsToDownload.length);
return storedItemsToDownload.map(storedItem => storedItem.mediaItem.id);
}
_createDownloadFilterFn() {
return value => {
let isFileExists = false;
let isFileSizeSame = false;
if (value.mediaItem) {
const filename = this._getStoredItemFilename(value);
const filepath = path.join(this.downloadPath, filename);
isFileExists = fs.existsSync(filepath);
if (isFileExists) {
const contentLength = lo.get(value.appData, 'download.contentLength');
const stat = fs.statSync(filepath);
isFileSizeSame = stat.size === contentLength;
}
}
return !(isFileExists && isFileSizeSame);
};
}
getStoredItemsByFilenameMap(filenamesOnDisk) {
const filenamesSet = new Set(filenamesOnDisk);
const map = {};
this.photoDb.getAll().forEach(storedItem => {
const filename = this._getStoredItemFilename(storedItem);
if (filenamesSet.has(filename)) {
map[filename] = {
filename,
storedItem
};
}
});
return map;
}
_getStoredItemFilename(storedItem) {
return storedItem.altFilename || storedItem.mediaItem.filename;
}
onFilesDownloaded(files) {
// file = { where: String, mediaItem }
return files.map(({ valid, where, mediaItem }) => {
if (!valid) {
return;
}
const stat = fs.statSync(where);
const download = {
at: Date.now(),
contentLength: stat.size
};
const date = moment(mediaItem.mediaMetadata.creationTime).toDate();
fs.utimesSync(where, date, date);
const storedItem = this.photoDb.get(mediaItem.id);
storedItem.appData.download = download;
return this.photoDb.set(mediaItem.id, storedItem);
});
}
hasStoredItemDiscrepancy(storedItem) {
if (storedItem.appData.probe && storedItem.appData.download) {
const pcl = storedItem.appData.probe.contentLength;
const dcl = storedItem.appData.download.contentLength;
return pcl !== dcl;
}
return true;
}
}
module.exports = AppController;