First Commit
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
import BunSQLite from "@moduletrace/bun-sqlite";
|
||||
import type { BUN_SQLITE_WGUI_MEDIA, BunSQLiteTables } from "@/db/types/db";
|
||||
import grabDirNames from "@/src/utils/grab-dir-names";
|
||||
import { existsSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import userAuth from "../../backend/auth/user-auth";
|
||||
|
||||
const { DATA_DIR } = grabDirNames();
|
||||
|
||||
type Params = {
|
||||
req: Request;
|
||||
};
|
||||
|
||||
export default async function handleMediaServer({
|
||||
req,
|
||||
}: Params): Promise<Response> {
|
||||
const url = new URL(req.url);
|
||||
|
||||
const [root, privacy, media_id, thumbnail] = url.pathname
|
||||
.split("/")
|
||||
.filter((p) => p.match(/./));
|
||||
|
||||
if (!privacy) {
|
||||
throw new Error(`No media privacy params passed`);
|
||||
}
|
||||
|
||||
let media: BUN_SQLITE_WGUI_MEDIA | undefined = undefined;
|
||||
|
||||
if (privacy == "private") {
|
||||
const { user, user_types } = await userAuth({ req });
|
||||
|
||||
if (!user?.logged_in_status) {
|
||||
throw new Error(`Not logged in`);
|
||||
}
|
||||
|
||||
const media_res = await BunSQLite.select<
|
||||
BUN_SQLITE_WGUI_MEDIA,
|
||||
(typeof BunSQLiteTables)[number]
|
||||
>({
|
||||
table: "media",
|
||||
query: {
|
||||
query: {
|
||||
user_id: {
|
||||
value: user.id,
|
||||
},
|
||||
},
|
||||
},
|
||||
targetId: media_id,
|
||||
});
|
||||
|
||||
media = media_res.singleRes || undefined;
|
||||
} else {
|
||||
const media_res = await BunSQLite.select<
|
||||
BUN_SQLITE_WGUI_MEDIA,
|
||||
(typeof BunSQLiteTables)[number]
|
||||
>({
|
||||
table: "media",
|
||||
targetId: media_id,
|
||||
});
|
||||
|
||||
media = media_res.singleRes || undefined;
|
||||
}
|
||||
|
||||
if (!media?.id) {
|
||||
throw new Error(`Media record not found!`);
|
||||
}
|
||||
|
||||
const media_location = thumbnail
|
||||
? media.media_thumbnail_write_path
|
||||
: media.media_write_path;
|
||||
|
||||
if (!media_location) {
|
||||
throw new Error(`No media location found!`);
|
||||
}
|
||||
|
||||
const full_media_location = path.join(DATA_DIR, media_location);
|
||||
|
||||
if (!existsSync(full_media_location)) {
|
||||
throw new Error(`Media does not exist!`);
|
||||
}
|
||||
|
||||
return new Response(Bun.file(full_media_location));
|
||||
}
|
||||
Reference in New Issue
Block a user