archive
This commit is contained in:
parent
c5ac70d317
commit
53dd6c2b1f
2 changed files with 36 additions and 37 deletions
71
library.ts
71
library.ts
|
@ -72,44 +72,43 @@ on conflict(reader_url)
|
|||
|
||||
return books;
|
||||
},
|
||||
async archive(dir: string) {
|
||||
const bookDirs = await fs.readdir(dir, { withFileTypes: true });
|
||||
async archive(path: string, outDir: string, book: Book) {
|
||||
const bookDir = await fs.stat(path);
|
||||
|
||||
for (const bookDir of bookDirs) {
|
||||
if (!bookDir.isDirectory()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const path = `${bookDir.path}/${bookDir.name}`;
|
||||
const book = await this.get(bookDir.name);
|
||||
const title = book
|
||||
? `${book.authors.join("、")}「${book.title}」`.replace(/[/]/g, "%2F")
|
||||
: bookDir.name;
|
||||
const out = createWriteStream(`${dir}/${title}.cbz`);
|
||||
const zip = new Zip(function cb(err, data, final) {
|
||||
if (err) {
|
||||
out.destroy(err);
|
||||
return;
|
||||
}
|
||||
|
||||
out[final ? "end" : "write"](data);
|
||||
});
|
||||
|
||||
const files = await fs.readdir(path);
|
||||
|
||||
for (const file of files) {
|
||||
const data = new ZipPassThrough(file);
|
||||
zip.add(data);
|
||||
|
||||
const buffer = await fs.readFile(`${path}/${file}`);
|
||||
data.push(buffer, true);
|
||||
}
|
||||
|
||||
zip.end();
|
||||
|
||||
await stream.finished(out);
|
||||
await fs.rm(path, { recursive: true });
|
||||
if (!bookDir.isDirectory()) {
|
||||
throw new Error(`Not found: ${path}`);
|
||||
}
|
||||
|
||||
const title = `${book.authors.join("、")}「${book.title}」`.replace(
|
||||
/[/]/g,
|
||||
"%2F",
|
||||
);
|
||||
|
||||
const out = createWriteStream(`${outDir}/${title}.cbz`);
|
||||
|
||||
const zip = new Zip(function cb(err, data, final) {
|
||||
if (err) {
|
||||
out.destroy(err);
|
||||
return;
|
||||
}
|
||||
|
||||
out[final ? "end" : "write"](data);
|
||||
});
|
||||
|
||||
const files = await fs.readdir(path);
|
||||
|
||||
for (const file of files) {
|
||||
const data = new ZipPassThrough(file);
|
||||
zip.add(data);
|
||||
|
||||
const buffer = await fs.readFile(`${path}/${file}`);
|
||||
data.push(buffer, true);
|
||||
}
|
||||
|
||||
zip.end();
|
||||
|
||||
await stream.finished(out);
|
||||
await fs.rm(path, { recursive: true });
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
2
main.ts
2
main.ts
|
@ -126,9 +126,9 @@ const options = {
|
|||
for (const book of books) {
|
||||
const dir = `${args.values["out-dir"]!}/${book.id}`;
|
||||
await platform.download(dir, book);
|
||||
await library.archive(dir, args.values["out-dir"]!, book);
|
||||
}
|
||||
|
||||
await library.archive(args.values["out-dir"]!);
|
||||
await browser.close();
|
||||
},
|
||||
},
|
||||
|
|
Loading…
Add table
Reference in a new issue