finish files route ?

This commit is contained in:
creations 2025-03-19 19:24:30 -04:00
parent 0b09b6eb3d
commit f39d1cdbf8
Signed by: creations
GPG key ID: 8F553AA4320FC711

View file

@ -1,158 +1,184 @@
// import { type ReservedSQL, sql } from "bun"; import { type ReservedSQL, sql, type SQLQuery } from "bun";
//
// import { isUUID } from "@/helpers/char"; import { isUUID } from "@/helpers/char";
// import { logger } from "@/helpers/logger"; import { logger } from "@/helpers/logger";
//
// function isValidSort(sortBy: string): boolean { function isValidSort(sortBy: string): boolean {
// const validSorts: string[] = [ const validSorts: string[] = [
// "size", "size",
// "created_at", "created_at",
// "expires_at", "expires_at",
// "views", "views",
// "name", "name",
// "original_name", "original_name",
// "mime_type", "mime_type",
// "extension", "extension",
// ]; ];
// return validSorts.includes(sortBy); return validSorts.includes(sortBy);
// } }
//
// function validSortOrder(sortOrder: string): string { function validSortOrder(sortOrder: string): string {
// const validSortOrder: { [key: string]: string } = { const validSortOrder: { [key: string]: string } = {
// asc: "ASC", asc: "ASC",
// desc: "DESC", desc: "DESC",
// ascending: "ASC", ascending: "ASC",
// descending: "DESC", descending: "DESC",
// }; };
//
// return validSortOrder[sortOrder.toLowerCase()] || "DESC"; return validSortOrder[sortOrder.toLowerCase()] || "DESC";
// } }
//
// const escapeLike: (value: string) => string = (value: string): string => const escapeLike: (value: string) => string = (value: string): string =>
// value.replace(/[%_\\]/g, "\\$&"); value.replace(/[%_\\]/g, "\\$&");
//
// const routeDef: RouteDef = { const routeDef: RouteDef = {
// method: "GET", method: "GET",
// accepts: "*/*", accepts: "*/*",
// returns: "application/json", returns: "application/json",
// }; };
//
// async function handler(request: ExtendedRequest): Promise<Response> { async function handler(request: ExtendedRequest): Promise<Response> {
// const { const {
// user: user_id, user: user_id,
// count = "25", count = "25",
// page = "0", page = "0",
// sort_by = "created_at", sort_by = "created_at",
// sort_order = "DESC", sort_order = "DESC",
// search_value, search_value,
// } = request.query as { } = request.query as {
// user: string; user: string;
// count: string; count: string;
// page: string; page: string;
// sort_by: string; sort_by: string;
// sort_order: string; sort_order: string;
// search_value: string; search_value: string;
// }; };
//
// if (!isValidSort(sort_by)) { if (!isValidSort(sort_by)) {
// return Response.json( return Response.json(
// { {
// success: false, success: false,
// code: 400, code: 400,
// error: "Invalid sort_by value", error: "Invalid sort_by value",
// }, },
// { status: 400 }, { status: 400 },
// ); );
// } }
//
// const userLookup: string | undefined = user_id || request.session?.id; const userLookup: string | undefined = user_id || request.session?.id;
//
// if (!userLookup) { if (!userLookup) {
// return Response.json( return Response.json(
// { {
// success: false, success: false,
// code: 400, code: 400,
// error: "Please provide a user ID or log in", error: "Please provide a user ID or log in",
// }, },
// { status: 400 }, { status: 400 },
// ); );
// } }
//
// const isId: boolean = isUUID(userLookup); const isId: boolean = isUUID(userLookup);
//
// if (!isId) { if (!isId) {
// return Response.json( return Response.json(
// { {
// success: false, success: false,
// code: 400, code: 400,
// error: "Invalid user ID", error: "Invalid user ID",
// }, },
// { status: 400 }, { status: 400 },
// ); );
// } }
//
// const isSelf: boolean = request.session?.id === userLookup; const isSelf: boolean = request.session?.id === userLookup;
// const isAdmin: boolean = request.session const isAdmin: boolean = request.session
// ? request.session.roles.includes("admin") ? request.session.roles.includes("admin")
// : false; : false;
//
// if (!isSelf && !isAdmin) { if (!isSelf && !isAdmin) {
// return Response.json( return Response.json(
// { {
// success: false, success: false,
// code: 403, code: 403,
// error: "Unauthorized", error: "Unauthorized",
// }, },
// { status: 403 }, { status: 403 },
// ); );
// } }
//
// const safeCount: number = Math.min(parseInt(count) || 25, 100); const safeCount: number = Math.min(parseInt(count) || 25, 100);
// const safePage: number = Math.max(parseInt(page) || 0, 0); const safePage: number = Math.max(parseInt(page) || 0, 0);
// const offset: number = safePage * safeCount; const offset: number = safePage * safeCount;
// let files: FileEntry[]; const sortColumn: string = sort_by || "created_at";
// const order: "ASC" | "DESC" = validSortOrder(sort_order) as "ASC" | "DESC";
// const reservation: ReservedSQL = await sql.reserve(); const safeSearchValue: string = escapeLike(search_value || "");
//
// // ! figure out why it wont accept DESC or ASC unless it's hardcoded let files: FileEntry[];
// try { let totalPages: number = 0;
// if (sort_by === "created_at" || sort_by === "expires_at") { let totalFiles: number = 0;
// } const reservation: ReservedSQL = await sql.reserve();
//
// if (!files.length) { try {
// return Response.json( // ! i really dont understand why bun wont accept reservation(order)`
// { function orderBy(
// success: true, field_name: string,
// code: 200, orderBy: "ASC" | "DESC",
// count: 0, ): SQLQuery {
// files: [], return reservation`ORDER BY ${reservation(field_name)} ${orderBy === "ASC" ? reservation`ASC` : reservation`DESC`}`;
// }, }
// { status: 200 },
// ); files = await reservation`
// } SELECT
// } catch (error) { * FROM files
// logger.error(["Error fetching files", error as Error]); WHERE owner = ${userLookup} AND
// return Response.json( (name ILIKE '%' || ${safeSearchValue} || '%' OR
// { original_name ILIKE '%' || ${safeSearchValue} || '%')
// success: false, ${orderBy(sortColumn, order)}
// code: 500, LIMIT ${safeCount} OFFSET ${offset};
// error: "Internal server error", `;
// },
// { status: 500 }, if (!files.length) {
// ); return Response.json(
// } finally { {
// reservation.release(); success: false,
// } code: 404,
// error: "No files found",
// return Response.json( },
// { { status: 404 },
// success: true, );
// code: 200, }
// count: files.length,
// files, [{ count: totalFiles }] = await reservation`
// }, SELECT COUNT(*)::int as count FROM files
// { status: 200 }, WHERE owner = ${userLookup};
// ); `;
// }
// totalPages = Math.ceil(totalFiles / safeCount);
// export { handler, routeDef }; } catch (error) {
logger.error(["Error fetching files", error as Error]);
return Response.json(
{
success: false,
code: 500,
error: "Internal server error",
},
{ status: 500 },
);
} finally {
reservation.release();
}
return Response.json(
{
success: true,
code: 200,
total_files: totalFiles,
total_pages: totalPages,
files,
},
{ status: 200 },
);
}
export { handler, routeDef };