This commit is contained in:
Rizky 2024-02-22 13:43:31 +07:00
parent 1b77a5851d
commit fea458b827
7 changed files with 93 additions and 40 deletions

BIN
bun.lockb

Binary file not shown.

BIN
dockerzip

Binary file not shown.

View File

@ -16,15 +16,15 @@ export const _ = {
async api() { async api() {
const { req } = apiContext(this); const { req } = apiContext(this);
let rpath = decodeURIComponent(req.params._); let rpath = decodeURIComponent(req.params._);
let res = new Response("NOT FOUND", { status: 404 });
rpath = rpath rpath = rpath
.split("/") .split("/")
.map((e) => e.replace(/\.\./gi, "")) .map((e) => e.replace(/\.\./gi, ""))
.filter((e) => !!e) .filter((e) => !!e)
.join("/"); .join("/");
let res = new Response("NOT FOUND", { status: 404 });
if (Object.keys(req.query_parameters).length > 0) { if (Object.keys(req.query_parameters).length > 0) {
await dirAsync(dir(`${g.datadir}/files`)); await dirAsync(dir(`${g.datadir}/files`));
const base_dir = dir(`${g.datadir}/files/${rpath}`); const base_dir = dir(`${g.datadir}/files/${rpath}`);

55
pkgs/api/_img.ts Normal file
View File

@ -0,0 +1,55 @@
import { dirAsync } from "fs-jetpack";
import { apiContext } from "service-srv";
import { dir } from "utils/dir";
import { g } from "utils/global";
import { dirname, parse } from "path";
import sharp from "sharp";
export const _ = {
url: "/_img/**",
async api() {
const { req } = apiContext(this);
let res = new Response("NOT FOUND", { status: 404 });
const w = parseInt(req.query_parameters.w);
let force = typeof req.query_parameters.force === "string";
let rpath = decodeURIComponent(req.params._);
rpath = rpath
.split("/")
.map((e) => e.replace(/\.\./gi, ""))
.filter((e) => !!e)
.join("/");
if (!w) {
const file = Bun.file(dir(`${g.datadir}/files/${rpath}`));
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}`));
if (p.ext === ".svg") {
return new Response(original);
}
const file_name = dir(`${g.datadir}/files/thumb/${w}/${rpath}`);
let file = Bun.file(file_name);
if (!(await file.exists())) {
await dirAsync(dirname(file_name));
force = true;
}
if (force) {
const img = sharp(await original.arrayBuffer());
const out = await img.resize({ width: w, fit: "inside" }).toBuffer();
await Bun.write(file_name, out);
file = Bun.file(file_name);
}
return new Response(file);
}
}
return res;
},
};

View File

@ -1,41 +1,40 @@
import { g } from "utils/global"; import { apiContext } from "service-srv";
import { gzipAsync } from "utils/gzip";
export const _ = { export const _ = {
url: "/_proxy/*", url: "/_proxy/*",
async api(arg: { raw: true,
url: string; async api() {
method: "POST" | "GET"; const { req } = apiContext(this);
headers: any;
body: any;
}) {
const res = await fetch(
arg.url,
arg.body
? {
method: arg.method || "POST",
headers: arg.headers,
body: arg.body,
}
: {
headers: arg.headers,
}
);
let body: any = null; try {
const headers: any = {}; const url = new URL(decodeURIComponent(req.params["_"]));
res.headers.forEach((v, k) => { const body = await req.arrayBuffer();
const headers = {} as Record<string, string>;
req.headers.forEach((v, k) => {
if (k.startsWith("sec-")) return;
if (k.startsWith("connection")) return;
if (k.startsWith("dnt")) return;
if (k.startsWith("host")) return;
headers[k] = v; headers[k] = v;
}); });
body = await res.arrayBuffer(); return await fetch(url, {
method: req.method || "POST",
if (headers["content-encoding"] === "gzip") { headers,
body = await gzipAsync(new Uint8Array(body)); body,
} else { });
delete headers["content-encoding"]; } catch (e: any) {
console.error(e);
new Response(
JSON.stringify({
status: "failed",
reason: e.message,
}),
{
status: 403,
headers: { "content-type": "application/json" },
}
);
} }
return new Response(body, { headers });
}, },
}; };

View File

@ -16,6 +16,6 @@
"typescript": "^5.2.2", "typescript": "^5.2.2",
"unzipper": "^0.10.14", "unzipper": "^0.10.14",
"parse-multipart-data": "^1.5.0", "parse-multipart-data": "^1.5.0",
"fast-myers-diff": "^3.2.0" "sharp": "^0.33.2"
} }
} }

View File

@ -1,3 +1,4 @@
import { Prisma } from "../../app/db/db";
export type DBArg = { export type DBArg = {
db: string; db: string;
@ -15,9 +16,7 @@ export const execQuery = async (args: DBArg, prisma: any) => {
if (action === "query" && table.startsWith("$query")) { if (action === "query" && table.startsWith("$query")) {
try { try {
const q = params.shift(); const q = params.shift();
q.sql = true; return await tableInstance.bind(prisma)(Prisma.sql(q, ...params));
Object.freeze(q);
return await tableInstance.bind(prisma)(q, ...params);
} catch (e) { } catch (e) {
console.log(e); console.log(e);
return e; return e;