// biome-ignore lint/correctness/noUnusedImports: Html is the JSX factory (tsconfig jsxFactory: Html.createElement) import { Html } from "@elysiajs/html"; import type { PropsWithChildren } from "@kitajs/html"; import { escapeHTML } from "bun"; import { fileTypeFromBuffer } from "file-type"; import hljs from "highlight.js"; import { config } from "./config"; import { humanFileSize, isValidUTF8 } from "./shared"; export const filetypes = ["none", "blob"].concat(hljs.listLanguages().sort()); function Template(props: PropsWithChildren<{ css: string }>) { return ( <> {""}
File not found
); } // Surfaces the server's configured abuse limits so users know the rules before // uploading. Each line only shows when the corresponding limit is actually set. function ServerLimits() { return (Reload page to try again
); } export function humanReadableTime(minutes: number) { // Guard on the total before it is broken into units below; an already-expired // (or sub-minute) duration has no meaningful breakdown. if (minutes <= 0) return "expired"; const years = Math.floor(minutes / 525600); minutes %= 525600; const days = Math.floor(minutes / 1440); minutes %= 1440; const hours = Math.floor(minutes / 60); minutes %= 60; let result = ""; if (years > 0) result += `${years} year${years > 1 ? "s" : ""} `; if (days > 0) result += `${days} day${days > 1 ? "s" : ""} `; if (hours > 0) result += `${hours} hour${hours > 1 ? "s" : ""} `; if (minutes > 0) result += `${minutes} minute${minutes > 1 ? "s" : ""} `; if (result === "") result = "0 minutes"; return result.trim(); } export async function ShowFile( filename: string, uuid: string, content: Uint8Array | null, filetype: string, delete_at: number | null, ) { //if content is null, this is for the js frontend let preview: JSX.Element; if (!content) { preview = <>Please wait for the file to load>; } else { preview = <>This file can't be previewed>; if (isValidUTF8(content) && filetype !== "blob") { if (filetype === "none") { preview = ({escapeHTML(new TextDecoder("utf-8").decode(content))}
);
} else {
preview = (
{
hljs.highlight(new TextDecoder("utf-8").decode(content), {
language: filetype,
}).value
}
);
}
} else {
// Point media previews at /raw/:uuid rather than inlining the whole
// file as a base64 data URI (which would balloon the HTML and server
// memory for large files). /raw serves a safe content-type and, for
// encrypted files, decrypts using the path-scoped password cookie.
const detected = await fileTypeFromBuffer(content);
const rawUrl = `/raw/${uuid}`;
if (detected) {
if (detected.mime.startsWith("audio/")) {
preview = ;
} else if (detected.mime.startsWith("video/")) {
preview = ;
} else if (detected.mime.startsWith("image/")) {
preview =