From b8d7c8313ff82b15d30ca29b84beca0499ff7995 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Sun, 28 May 2023 21:55:01 +0800 Subject: [PATCH] Fix gallery can not obtain two same pictures --- db.ts | 15 +++++++++++++-- tasks/download.ts | 13 +++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/db.ts b/db.ts index f0c2dec..17d454d 100644 --- a/db.ts +++ b/db.ts @@ -124,7 +124,7 @@ const PMETA_TABLE = `CREATE TABLE pmeta ( name TEXT, width INT, height INT, - PRIMARY KEY (gid, token) + PRIMARY KEY (gid, "index") );`; const TAG_TABLE = `CREATE TABLE tag ( id INTEGER PRIMARY KEY AUTOINCREMENT, @@ -156,7 +156,7 @@ export class EhDb { #lock_file: string | undefined; #dblock_file: string | undefined; #_tags: Map | undefined; - readonly version = new SemVer("1.0.0-3"); + readonly version = new SemVer("1.0.0-4"); constructor(base_path: string) { const db_path = join(base_path, "data.db"); sure_dir_sync(base_path); @@ -198,6 +198,7 @@ export class EhDb { const v = this.#read_version(); if (!v) return false; if (v.compare(this.version) === -1) { + let need_optimize = false; if (v.compare("1.0.0-1") === -1) { this.db.execute("ALTER TABLE tag ADD translated TEXT;"); this.db.execute("ALTER TABLE tag ADD intro TEXT;"); @@ -213,7 +214,17 @@ export class EhDb { if (v.compare("1.0.0-3") === -1) { this.db.execute("ALTER TABLE task ADD details TEXT;"); } + if (v.compare("1.0.0-4") === -1) { + this.db.execute("ALTER TABLE pmeta RENAME TO pmeta_origin;"); + this.db.execute(PMETA_TABLE); + this.db.execute( + 'INSERT INTO pmeta (gid, "index", token, name, width, height) SELECT gid, "index", token, name, width, height FROM pmeta_origin;', + ); + this.db.execute("DROP TABLE pmeta_origin;"); + need_optimize = true; + } this.#write_version(); + if (need_optimize) this.optimize(); } if ( ALL_TABLES.length !== this.#exist_table.size || diff --git a/tasks/download.ts b/tasks/download.ts index d2f662b..5ffdfbc 100644 --- a/tasks/download.ts +++ b/tasks/download.ts @@ -95,6 +95,19 @@ export async function download_task( (t.is_original || !cfg.download_original_img) && (await exists(t.path)) ) { + const p = db.get_pmeta_by_index(task.gid, i.index); + if (!p) { + const op = db.get_pmeta_by_token( + task.gid, + i.page_token, + ); + if (op) { + op.index = i.index; + op.name = i.name; + db.add_pmeta(op); + return; + } + } console.log("Already download page", i.index); return; }