fix: Add file type detection for downloaded thumbnails (Fix #8), fix extract hash info from url

This commit is contained in:
2024-12-25 10:06:18 +08:00
parent 0af29f80e7
commit fa9a9b5ef2
3 changed files with 59 additions and 4 deletions

View File

@@ -16,11 +16,12 @@ import {
asyncFilter,
promiseState,
PromiseStatus,
replaceExtname,
sleep,
sure_dir,
TimeoutError,
} from "../utils.ts";
import { join, resolve } from "@std/path";
import { basename, extname, join, resolve } from "@std/path";
import { exists } from "@std/fs/exists";
import { ProgressReadable } from "../utils/progress_readable.ts";
@@ -162,6 +163,11 @@ class DownloadManager {
}
this.#sendEvent();
}
set_details_name(index: number, name: string) {
const d = this.#progress.details.find((v) => v.index === index);
if (d) d.name = name;
this.#sendEvent();
}
set_details_started(index: number) {
const d = this.#progress.details.find((v) => v.index === index);
if (d) {
@@ -369,6 +375,20 @@ export async function download_task(
pr.addEventListener("progress", (e) => {
m.set_details_downloaded(i.index, e.detail);
});
if (!download_original) {
const ext = extname(path);
const u = new URL(re.url);
const uext = extname(u.pathname);
if (ext != uext) {
path = replaceExtname(path, uext);
if (f) {
f.path = path;
} else {
throw Error("Failed to get file.");
}
m.set_details_name(i.index, basename(path));
}
}
try {
const f = await Deno.open(path, {
create: true,