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