This commit is contained in:
Nebel 2023-11-20 01:37:55 +09:00
parent de2644352d
commit 67fdc920e5
Signed by: nebel
GPG key ID: 79807D08C6EF6460
4 changed files with 55 additions and 0 deletions

View file

@ -1,3 +1,7 @@
import fs from "node:fs/promises";
import { createWriteStream } from "node:fs";
import stream from "node:stream/promises";
import { Zip, ZipPassThrough } from "fflate";
import { Database } from "./database";
export type Book = {
@ -35,5 +39,40 @@ export function createLibrary(db: Database) {
return books;
},
async archive(dir: string) {
const bookDirs = await fs.readdir(dir, { withFileTypes: true });
for (const bookDir of bookDirs) {
if (!bookDir.isDirectory()) {
continue;
}
const path = `${bookDir.path}/${bookDir.name}`;
const out = createWriteStream(`${path}.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 });
}
},
};
}

View file

@ -93,6 +93,15 @@ const options = {
const platform = createPlatform({ db, browser });
const dir = `${args.values["out-dir"]!}/${book.id}`;
await platform.download(dir, book);
await library.archive(args.values["out-dir"]!);
},
},
archive: {
type: "boolean",
async run() {
const db = await createDatabase(args.values.db!);
const library = createLibrary(db);
await library.archive(args.values["out-dir"]!);
},
},
help: {

6
package-lock.json generated
View file

@ -9,6 +9,7 @@
"version": "0.0.0",
"license": "AGPL-3.0",
"dependencies": {
"fflate": "^0.8.1",
"playwright": "^1.39.0",
"sqlite": "^5.1.1",
"sqlite3": "^5.1.6",
@ -661,6 +662,11 @@
"@esbuild/win32-x64": "0.18.20"
}
},
"node_modules/fflate": {
"version": "0.8.1",
"resolved": "https://registry.npmjs.org/fflate/-/fflate-0.8.1.tgz",
"integrity": "sha512-/exOvEuc+/iaUm105QIiOt4LpBdMTWsXxqR0HDF35vx3fmaKzw7354gTilCh5rkzEt8WYyG//ku3h3nRmd7CHQ=="
},
"node_modules/fs-minipass": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/fs-minipass/-/fs-minipass-2.1.0.tgz",

View file

@ -5,6 +5,7 @@
"type": "module",
"bin": "bin/run.js",
"dependencies": {
"fflate": "^0.8.1",
"playwright": "^1.39.0",
"sqlite": "^5.1.1",
"sqlite3": "^5.1.6",