This commit is contained in:
Rizky 2024-03-17 14:09:31 +07:00
parent 40f98e04ac
commit b394129e73
1 changed files with 47 additions and 30 deletions

View File

@ -1,10 +1,13 @@
import { dirAsync } from "fs-jetpack"; import { dirAsync } from "fs-jetpack";
import { apiContext } from "service-srv"; import { apiContext } from "service-srv";
import { stat } from "fs/promises";
import { dir } from "utils/dir"; import { dir } from "utils/dir";
import { g } from "utils/global"; import { g } from "utils/global";
import { dirname, parse } from "path"; import { dirname, parse } from "path";
import sharp from "sharp"; import sharp from "sharp";
const modified = {} as Record<string, number>;
export const _ = { export const _ = {
url: "/_img/**", url: "/_img/**",
async api() { async api() {
@ -22,13 +25,25 @@ export const _ = {
.filter((e) => !!e) .filter((e) => !!e)
.join("/"); .join("/");
try {
const filepath = dir(`${g.datadir}/files/${rpath}`);
const st = await stat(filepath);
if (st.isFile()) {
if (
!modified[filepath] ||
(modified[filepath] && modified[filepath] !== st.mtimeMs)
) {
modified[filepath] = st.mtimeMs;
force = true;
}
if (!w) { if (!w) {
const file = Bun.file(dir(`${g.datadir}/files/${rpath}`)); const file = Bun.file(filepath);
return new Response(file); return new Response(file);
} else { } else {
const original = Bun.file(dir(`${g.datadir}/files/${rpath}`)); const original = Bun.file(filepath);
if (await original.exists()) {
const p = parse(dir(`${g.datadir}/files/${rpath}`)); const p = parse(filepath);
if (p.ext === ".svg") { if (p.ext === ".svg") {
return new Response(original); return new Response(original);
} }
@ -40,7 +55,6 @@ export const _ = {
force = true; force = true;
} }
if (format === "jpg" && !file_name.endsWith(".jpg")) { if (format === "jpg" && !file_name.endsWith(".jpg")) {
force = true; force = true;
} }
@ -61,6 +75,9 @@ export const _ = {
return new Response(file); return new Response(file);
} }
} }
} catch (e: any) {
return new Response("ERROR:" + e.message, { status: 404 });
}
return res; return res;
}, },