Bug fix: A gallery may contains two image which have same name

This commit is contained in:
2023-06-10 21:11:17 +08:00
parent a5488dcd35
commit eb146e826d
3 changed files with 36 additions and 2 deletions

View File

@@ -5,6 +5,7 @@ import { EhDb } from "../db.ts";
import { Task, TaskDownloadProgess, TaskType } from "../task.ts"; import { Task, TaskDownloadProgess, TaskType } from "../task.ts";
import { TaskManager } from "../task_manager.ts"; import { TaskManager } from "../task_manager.ts";
import { import {
add_suffix_to_path,
asyncFilter, asyncFilter,
promiseState, promiseState,
PromiseStatus, PromiseStatus,
@@ -117,6 +118,13 @@ export async function download_task(
if (cfg.mpv) { if (cfg.mpv) {
const mpv = await client.fetchMPVPage(task.gid, task.token); const mpv = await client.fetchMPVPage(task.gid, task.token);
m.set_total_page(mpv.pagecount); m.set_total_page(mpv.pagecount);
const names = mpv.imagelist.reduce(
(acc: Record<string, number>, cur) => {
const curr = cur.name;
return acc[curr] ? ++acc[curr] : acc[curr] = 1, acc;
},
{},
);
for (const i of mpv.imagelist) { for (const i of mpv.imagelist) {
if (abort.aborted) break; if (abort.aborted) break;
await m.add_new_task(async () => { await m.add_new_task(async () => {
@@ -167,7 +175,11 @@ export async function download_task(
const download_original = cfg.download_original_img && const download_original = cfg.download_original_img &&
!i.is_original; !i.is_original;
if (download_original) console.log(i.index, i.data.o); if (download_original) console.log(i.index, i.data.o);
const path = resolve(join(base_path, i.name)); let path = resolve(join(base_path, i.name));
if (names[i.name] > 1) {
path = add_suffix_to_path(path, i.page_token);
console.log("Changed path to", path);
}
function download_img() { function download_img() {
return new Promise<void>((resolve, reject) => { return new Promise<void>((resolve, reject) => {
async function download() { async function download() {

View File

@@ -152,3 +152,15 @@ export function configureZipJs() {
configure({ useWebWorkers: false }); configure({ useWebWorkers: false });
zipjs_configured = true; zipjs_configured = true;
} }
export function add_suffix_to_path(path: string, suffix?: string) {
if (suffix === undefined) {
suffix = Math.round(Math.random() * 100000).toString();
}
const ext = extname(path);
if (ext) {
return `${path.slice(0, path.length - ext.length)}-${suffix}${ext}`;
} else {
return `${path}-${suffix}`;
}
}

View File

@@ -1,6 +1,7 @@
import { assertEquals } from "std/testing/asserts.ts"; import { assert, assertEquals } from "std/testing/asserts.ts";
import { check_running } from "./pid_check.ts"; import { check_running } from "./pid_check.ts";
import { import {
add_suffix_to_path,
asyncFilter, asyncFilter,
asyncForEach, asyncForEach,
filterFilename, filterFilename,
@@ -69,3 +70,12 @@ Deno.test("filterFilename_test", () => {
assertEquals(filterFilename("d|?ad.ts"), "d__ad.ts"); assertEquals(filterFilename("d|?ad.ts"), "d__ad.ts");
} }
}); });
Deno.test("add_suffix_to_path_test", () => {
assertEquals(add_suffix_to_path("test.ts", "ok"), "test-ok.ts");
assertEquals(add_suffix_to_path("test", "ok"), "test-ok");
const t = add_suffix_to_path("test.ts");
console.log(t);
assert(t.startsWith("test-"));
assert(t.endsWith(".ts"));
});