From 1dd7c21bb6a985a3ffbca4d325dea65622b70508 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Sun, 21 May 2023 13:58:13 +0800 Subject: [PATCH] Update --- config.example.jsonc | 4 +++- config.ts | 3 +++ db.ts | 6 ++++-- deno.jsonc | 3 ++- main.ts | 5 +++-- task_manager.ts | 2 +- utils.ts | 8 +++++++- 7 files changed, 23 insertions(+), 8 deletions(-) diff --git a/config.example.jsonc b/config.example.jsonc index ed7ec6e..41bcd45 100644 --- a/config.example.jsonc +++ b/config.example.jsonc @@ -8,5 +8,7 @@ // Maximum task count "max_task_count": 1, // Fetch page data from Multi-Page Viewer - "mpv": false + "mpv": false, + // The folder that to store database. + "db_path": "./downloads" } diff --git a/config.ts b/config.ts index 1085189..32224fc 100644 --- a/config.ts +++ b/config.ts @@ -44,6 +44,9 @@ export class Config { get cookies() { return this._return_string("cookies"); } + get db_path() { + return this._return_string("db_path"); + } get ua() { return this._return_string("ua"); } diff --git a/db.ts b/db.ts index 79b1110..22ecc0e 100644 --- a/db.ts +++ b/db.ts @@ -3,7 +3,7 @@ import { SemVer } from "std/semver/mod.ts"; import { join } from "std/path/mod.ts"; import { SqliteError } from "sqlite/mod.ts"; import { Status } from "sqlite/src/constants.ts"; -import { sleep } from "./utils.ts"; +import { sleep, sure_dir_sync } from "./utils.ts"; import { Task, TaskType } from "./task.ts"; type SqliteMaster = { @@ -157,7 +157,9 @@ export class EhDb { #_tags: Map | undefined; readonly version = new SemVer("1.0.0-1"); constructor(base_path: string) { - this.db = new DB(join(base_path, "data.db")); + const db_path = join(base_path, "data.db"); + sure_dir_sync(base_path); + this.db = new DB(db_path); this.db.execute("PRAGMA main.locking_mode=EXCLUSIVE;"); if (!this.#check_database()) this.#create_table(); if (this.#flock_enabled) { diff --git a/deno.jsonc b/deno.jsonc index 39519f6..9083ef9 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -8,7 +8,8 @@ "dev": "deno run --watch main.ts", "test": "deno test --allow-read=./config.json,./test --allow-net --allow-write=./test --allow-run=tasklist.exe --unstable", "run": "deno run --allow-read=./config.json,./downloads --allow-write=./downloads --allow-run=tasklist.exe --allow-net --unstable", - "compile": "deno compile --allow-read=./config.json,./downloads --allow-write=./downloads --allow-run=tasklist.exe --allow-net --unstable" + "compile": "deno compile --allow-read=./config.json,./downloads --allow-write=./downloads --allow-run=tasklist.exe --allow-net --unstable", + "compile_full": "deno compile --allow-read --allow-write --allow-run=tasklist.exe --allow-net --unstable" }, "fmt": { "indentWidth": 4, diff --git a/main.ts b/main.ts index ffb3a8a..181022c 100644 --- a/main.ts +++ b/main.ts @@ -81,16 +81,17 @@ async function run() { } } function optimize() { - const db = new EhDb(settings.base); + const db = new EhDb(settings.db_path || settings.base); db.optimize(); db.close(); } async function update_tag_translation() { - const db = new EhDb(settings.base); + const db = new EhDb(settings.db_path || settings.base); const f = await load_eht_file( args._.length > 1 ? args._[1].toString() : undefined, ); update_database_tag(db, f); + db.close(); } async function main() { await sure_dir(settings.base); diff --git a/task_manager.ts b/task_manager.ts index 1fcff23..1773643 100644 --- a/task_manager.ts +++ b/task_manager.ts @@ -20,7 +20,7 @@ export class TaskManager { constructor(cfg: Config) { this.cfg = cfg; this.client = new Client(cfg); - this.db = new EhDb(cfg.base); + this.db = new EhDb(cfg.db_path || cfg.base); this.running_tasks = new Map(); this.max_task_count = cfg.max_task_count; add_exit_handler(this); diff --git a/utils.ts b/utils.ts index 334650e..b018eea 100644 --- a/utils.ts +++ b/utils.ts @@ -1,4 +1,4 @@ -import { exists } from "std/fs/exists.ts"; +import { exists, existsSync } from "std/fs/exists.ts"; import { initParser } from "deno_dom/deno-dom-wasm-noinit.ts"; export function sleep(time: number): Promise { @@ -15,6 +15,12 @@ export async function sure_dir(f = "./test") { } } +export function sure_dir_sync(f = "./test") { + if (!existsSync(f)) { + Deno.mkdirSync(f, { "recursive": true }); + } +} + export enum PromiseStatus { Pending, Fulfilled,