-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdomainh.mts
More file actions
357 lines (318 loc) · 11.6 KB
/
Copy pathdomainh.mts
File metadata and controls
357 lines (318 loc) · 11.6 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
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
import fsh from "./utils/fsh.js";
import promiseh from "./utils/promiseh.js";
import fs from "fs";
import { dateh } from "./site/dateh.mjs";
import { type Accessor, type Page, type Code, type Date, type Book, type CodeAndDate } from "./Accessor.mts";
import ziph from "./utils/ziph.js";
let currentDownloads: DownloadEntry[] = [];
let accessorh: Accessor;
type Status = typeof Statuses[keyof typeof Statuses];
type Folder = string;
const Statuses = {
COMPLETED: "completed",
IN_PROGRESS: "in progress",
STOPPED: "stopped",
ERROR: "error",
PARTIAL: "partial",
EMPTY: "empty"
} as const;
type CodeDateStatus = {
code: Code, date: Date, status: Status
}
type DownloadEntry = {
code: string;
date: string;
total: number;
current: number;
inProgress: boolean;
}
type NamedDownloadEntry = {
code: string;
date: string;
total: number;
current: number;
inProgress: boolean;
name: string;
}
type FileResult = {
data: string
type: string
}
type DownloadType = "full" | "cover" | "slice"
type ThumbnailResult = {status: "date" | "cover" | "default", thumbnail: string}
function findOrCreateDownload(code: Code, date: Date): DownloadEntry {
date = dateh.formatDate(date, "-");
let result = currentDownloads.find(x => x.code == code && x.date == date);
if (result == undefined) {
result = { code: code, date: date, total: 0, current: 0, inProgress: false };
currentDownloads.push(result);
}
return result;
}
function findDownload(code: Code, date: Date): DownloadEntry | undefined {
date = dateh.formatDate(date, "-");
let result = currentDownloads.find(x => x.code == code && x.date == date);
return result;
}
function getPages(code: Code, date: Date): Promise<Page[]> {
date = dateh.formatDate(date, "-");
return accessorh.getPages(code, date);
}
function isFull(code: Code): Promise<boolean> {
return accessorh.isFull(code);
}
function searchDownloadedPages(folder: Folder): Promise<Page[]> {
return fsh.getFiles(folder).then(files => {
let pages:Page[] = files.filter(file => (file.endsWith(".png") || file.endsWith(".jpg")) && !(file.endsWith("thumbnail.png")));
return Promise.resolve([...pages]);
});
}
function getStatus(codes: CodeAndDate[]): Promise<CodeDateStatus[]> {
return Promise.all(codes.map(r => unaryStatus(r.code, r.date))).then(res => {
let result: CodeDateStatus[]= [];
codes.forEach((v, i) => {
result[i] = {code: codes[i].code, date: codes[i].date, status: res[i].status};
});
return Promise.resolve(result);
});
}
function unaryStatus(code: Code, date: Date): Promise<{status: Status, current?: number, total?: number}> {
date = dateh.formatDate(date, "-");
let folder = `results/${code}/${date}`;
let outputFile = `${folder}/${code}_${date}.cbz`;
if (fsh.fileExists(outputFile)) {
return Promise.resolve({ status: "completed" });
}
let download = findDownload(code, date);
if (download != null) {
if (download.inProgress) {
return Promise.resolve({ status: "in progress", current: download.current, total: download.total });
} else {
return Promise.resolve({ status: "stopped", current: download.current, total: download.total });
}
}
return new Promise((resolve, reject) => {
searchDownloadedPages(folder).then(files => {
if (files.length > 0) { //previous partial
getPages(code, date).then(pages => {
resolve({ status: "partial", current: files.length, total: pages.length });
}).catch(e => {
resolve({ status: "error" });
});
} else {
resolve({ status: "empty" });
}
});
});
}
function read(code: Code, date: Date): Promise<FileResult> {
date = dateh.formatDate(date, "-");
let folder = `results/${code}/${date}`;
let outputFile = `${folder}/${code}_${date}.cbz`;
let outputFilePDF = `${folder}/${code}_${date}.pdf`;
let outputPartialFile = `${folder}/${code}_${date}-partial.cbz`;
return new Promise<string>((resolve, reject) => {
if (fsh.fileExists(outputFile)) {
resolve(outputFile);
} else if (fsh.fileExists(outputFilePDF)) {
resolve(outputFilePDF);
} else {
searchDownloadedPages(folder).then(files => {
if (files.length > 0) {
ziph.createZip(files, outputPartialFile);
resolve(outputPartialFile);
} else {
reject(new Error("empty"));
}
});
}
}).then(e => {
return new Promise((resolve, reject) => {
fs.readFile(e, "binary", (err, data) => {
if (err) reject(err);
else resolve({ type: e.endsWith(".cbz")? "cbz" : "pdf", data: data });
});
});
});
}
function stopDownload(code: Code, date: Date) {
date = dateh.formatDate(date, "-");
let dnld = findDownload(code, date);
if (dnld != null) {
dnld.inProgress = false;
}
return Promise.resolve(true);
}
let pagesQueue = promiseh.newQueue(() => Math.round(Math.random() * 2000) + 3000);
let imagesQueue = promiseh.newQueue(() => Math.round(Math.random() * 18000) + 15000);
let fullQueue = promiseh.newQueue(() => Math.round(Math.random() * 2000) + 3000);
function download(code: Code, date: Date, type: DownloadType): Promise<{status: Status}> {
date = dateh.formatDate(date, "-");
let folder = `results/${code}/${date}`;
let outputFileName = `${code}_${date}.cbz`;
let outputFileNamePDF = `${code}_${date}.pdf`;
let outputFile = `${folder}/${outputFileName}`;
let outputFilePDF = `${folder}/${outputFileName}`;
let outputPartialFile = `${folder}/${code}_${date}-partial.cbz`;
if (fsh.fileExists(outputFile) || fsh.fileExists(outputFilePDF)) {
return Promise.resolve({ status: "completed" });
}
console.log(currentDownloads);
let download = findOrCreateDownload(code, date);
if (download.inProgress) {
return Promise.resolve({ status: "in progress" });
}
download.inProgress = true;
isFull(code).then(isFull => {
if (!isFull) {
pagesQueue.push(() => {
return getPages(code, date).then(allPages => {
download.total = allPages.length;
let remainingPages = allPages.filter(r => {
let imageFile = `${folder}/${r}.png`;
return !fsh.fileExists(imageFile);
});
download.current = download.total - remainingPages.length;
if (type == "cover") {
remainingPages = remainingPages.slice(0, 1);
} else if (type == "slice") {
remainingPages = remainingPages.slice(0, 3);
}
imagesQueue.push(remainingPages.map((page, i) => () => {
console.log(`proceed image ${page}`);
let imageId = page;
if (download.inProgress == false) {
return Promise.resolve(false);
}
let imageFile = `${folder}/${imageId}.png`;
download.current++;
return accessorh.getImage(code, imageId).then(tt => {
return fsh.write(imageFile, tt);
}).then(e => {
console.log('The file has been saved! ' + imageFile);
return Promise.resolve(true);
}).then(ee => {
if (download.total == download.current) {
return searchDownloadedPages(folder).then(files => {
return ziph.createZip(files, outputFile);
}).then(e => {
download.inProgress = false;
return Promise.resolve(true);
});
} else if (i == remainingPages.length - 1) {
download.inProgress = false;
return Promise.resolve(true);
}
});
}), type == "cover");
});
});
} else {
fullQueue.push(() => {
console.log("Full download of " + code + " " + date);
let fullFolder = `${folder}`;
return accessorh.getFull(code, date).then(res => {
return fsh.mkdir(fullFolder).then(e => {
return ziph.unzipDomain(res, fullFolder, outputFileNamePDF, (total, progress) => {
download.total = total;
download.current = Math.floor(progress / 2);
});
});
}).then(e => {
console.log('The pages has been saved!');
return searchDownloadedPages(folder);
}).then(files => {
if (files.length == 0) {
return Promise.resolve({ status: "complete" });
}
return ziph.createZip(files, outputFile, (total, progress) => {
download.current = Math.floor(download.total / 2) + Math.floor(progress / 2);
});
}).then(e => {
console.log('The pdf has been saved!');
download.inProgress = false;
download.current = download.total;
return Promise.resolve({ status: "complete" });
});
});
}
});
return Promise.resolve({ status: "in progress" });
}
function getDownloads(code: Code | null): Promise<NamedDownloadEntry[]> {
let dwn = currentDownloads.filter(c => code == null || c.code == code);
return accessorh.getBooks().then(books => {
let result: NamedDownloadEntry[] = [];
dwn.forEach(d => {
let entry: NamedDownloadEntry = {code: d.code, date: d.date, total: d.total, current: d.current, inProgress: d.inProgress, name: d.code}
result.push(entry);
let book = books.find(b => b.code == d.code);
if (book) {
entry.name = book.name;
}
});
return Promise.resolve(result);
});
}
function getThumbnail(code: Code, date: Date): Promise<ThumbnailResult> {
return new Promise<ThumbnailResult>((resolve, reject) => {
if (date != null && date != "null" && code != null && code != "null") {
date = dateh.formatDate(date, "-");
let thumb = `results/${code}/${date}/thumbnail.png`;
if (fsh.fileExists(thumb)) {
fs.readFile(thumb, "binary", (err, data) => {
if (err) reject(err);
else resolve({ status: "date", thumbnail: data });
});
} else {
accessorh.getThumbnail(code, date).then(imageData => {
fsh.write(thumb, imageData, "binary");
thumb = `results/${code}/thumbnail.png`;
fsh.write(thumb, imageData, "binary");
resolve({ status: "date", thumbnail: imageData.toString() });
}).catch(e => {
console.log(e);
reject(e);
});
}
} else {
reject(new Error("thumbnail"));
}
}).catch(e => {
return new Promise<ThumbnailResult>((resolve, reject) => {
if (code != null && code != "null") {
let thumb = `results/${code}/thumbnail.png`;
if (fsh.fileExists(thumb)) {
fs.readFile(thumb, "binary", (err, data) => {
if (err) reject(err);
else resolve({ status: "cover", thumbnail: data });
});
} else {
reject(e);
}
} else {
reject(e);
}
});
}).catch(e => {
return new Promise<ThumbnailResult>((resolve, reject) => {
fs.readFile(`thumbnail.png`, "binary", (err, data) => {
if (err) reject(err);
else resolve({ status: "default", thumbnail: data });
});
});
})
}
var domainh = {
read: read,
download: download,
stopDownload: stopDownload,
getThumbnail: getThumbnail,
getDownloads: getDownloads,
getStatus: getStatus,
searchDownloadedPages: searchDownloadedPages
}
export default function (accessor: Accessor) {
accessorh = accessor;
return domainh;
};