From 53dd6c2b1f0e0571e8e5e204a75d512b5dfb2096 Mon Sep 17 00:00:00 2001 From: Kohei Watanabe Date: Thu, 23 Nov 2023 10:24:08 +0900 Subject: [PATCH] archive --- library.ts | 71 +++++++++++++++++++++++++++--------------------------- main.ts | 2 +- 2 files changed, 36 insertions(+), 37 deletions(-) diff --git a/library.ts b/library.ts index 2e70eed..44d2acb 100644 --- a/library.ts +++ b/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 }); }, }; } diff --git a/main.ts b/main.ts index 1dd8591..d225350 100644 --- a/main.ts +++ b/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(); }, },