wip fix
This commit is contained in:
parent
40f98e04ac
commit
b394129e73
|
|
@ -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,44 +25,58 @@ export const _ = {
|
||||||
.filter((e) => !!e)
|
.filter((e) => !!e)
|
||||||
.join("/");
|
.join("/");
|
||||||
|
|
||||||
if (!w) {
|
try {
|
||||||
const file = Bun.file(dir(`${g.datadir}/files/${rpath}`));
|
const filepath = dir(`${g.datadir}/files/${rpath}`);
|
||||||
return new Response(file);
|
const st = await stat(filepath);
|
||||||
} else {
|
if (st.isFile()) {
|
||||||
const original = Bun.file(dir(`${g.datadir}/files/${rpath}`));
|
if (
|
||||||
if (await original.exists()) {
|
!modified[filepath] ||
|
||||||
const p = parse(dir(`${g.datadir}/files/${rpath}`));
|
(modified[filepath] && modified[filepath] !== st.mtimeMs)
|
||||||
if (p.ext === ".svg") {
|
) {
|
||||||
return new Response(original);
|
modified[filepath] = st.mtimeMs;
|
||||||
}
|
|
||||||
|
|
||||||
let file_name = dir(`${g.datadir}/files/upload/thumb/${w}/${rpath}`);
|
|
||||||
let file = Bun.file(file_name);
|
|
||||||
if (!(await file.exists())) {
|
|
||||||
await dirAsync(dirname(file_name));
|
|
||||||
force = true;
|
force = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!w) {
|
||||||
|
const file = Bun.file(filepath);
|
||||||
|
return new Response(file);
|
||||||
|
} else {
|
||||||
|
const original = Bun.file(filepath);
|
||||||
|
|
||||||
if (format === "jpg" && !file_name.endsWith(".jpg")) {
|
const p = parse(filepath);
|
||||||
force = true;
|
if (p.ext === ".svg") {
|
||||||
}
|
return new Response(original);
|
||||||
|
|
||||||
if (force) {
|
|
||||||
const img = sharp(await original.arrayBuffer());
|
|
||||||
let out = img.resize({ width: w, fit: "inside" });
|
|
||||||
|
|
||||||
if (format === "jpg" && !file_name.endsWith(".jpg")) {
|
|
||||||
file_name = file_name + ".jpg";
|
|
||||||
out = out.toFormat("jpg");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await Bun.write(file_name, await out.toBuffer());
|
let file_name = dir(`${g.datadir}/files/upload/thumb/${w}/${rpath}`);
|
||||||
file = Bun.file(file_name);
|
let file = Bun.file(file_name);
|
||||||
}
|
if (!(await file.exists())) {
|
||||||
|
await dirAsync(dirname(file_name));
|
||||||
|
force = true;
|
||||||
|
}
|
||||||
|
|
||||||
return new Response(file);
|
if (format === "jpg" && !file_name.endsWith(".jpg")) {
|
||||||
|
force = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (force) {
|
||||||
|
const img = sharp(await original.arrayBuffer());
|
||||||
|
let out = img.resize({ width: w, fit: "inside" });
|
||||||
|
|
||||||
|
if (format === "jpg" && !file_name.endsWith(".jpg")) {
|
||||||
|
file_name = file_name + ".jpg";
|
||||||
|
out = out.toFormat("jpg");
|
||||||
|
}
|
||||||
|
|
||||||
|
await Bun.write(file_name, await out.toBuffer());
|
||||||
|
file = Bun.file(file_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Response(file);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} catch (e: any) {
|
||||||
|
return new Response("ERROR:" + e.message, { status: 404 });
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue