From b71963a4911c2c1433a4530032476d12678edac9 Mon Sep 17 00:00:00 2001 From: "Jack W." Date: Tue, 24 Oct 2023 16:50:14 -0400 Subject: [PATCH] refactor: :truck: Move preview to new file --- src/server/index.ts | 110 ++---------------------------------------- src/server/preview.ts | 109 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 107 deletions(-) create mode 100644 src/server/preview.ts diff --git a/src/server/index.ts b/src/server/index.ts index f023eaa..730145d 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -6,15 +6,15 @@ import bytes from "bytes" import ServeError from "./lib/errors" import Files from "./lib/files" -import * as auth from "./lib/auth" import * as Accounts from "./lib/accounts" import { getAccount } from "./lib/middleware" import APIRouter from "./routes/api" +import preview from "./preview" require("dotenv").config() -let pkg = require(`${process.cwd()}/package.json`) +const pkg = require(`${process.cwd()}/package.json`) let app = express() let config = require(`${process.cwd()}/config.json`) @@ -79,111 +79,7 @@ app.get("/", function (req, res) { // serve download page -app.get("/download/:fileId", getAccount, (req, res) => { - let acc = res.locals.acc as Accounts.Account - - if (files.getFilePointer(req.params.fileId)) { - let file = files.getFilePointer(req.params.fileId) - - if (file.visibility == "private" && acc?.id != file.owner) { - ServeError(res, 403, "you do not own this file") - return - } - - fs.readFile(process.cwd() + "/pages/download.html", (err, buf) => { - let fileOwner = file.owner - ? Accounts.getFromId(file.owner) - : undefined - if (err) { - res.sendStatus(500) - console.log(err) - return - } - res.send( - buf - .toString() - .replaceAll("$FileId", req.params.fileId) - .replaceAll("$Version", pkg.version) - .replaceAll( - "$FileSize", - file.sizeInBytes - ? bytes(file.sizeInBytes) - : "[File size unknown]" - ) - .replaceAll( - "$FileName", - file.filename - .replaceAll("&", "&") - .replaceAll("<", "<") - .replaceAll(">", ">") - ) - .replace( - "", - (file.mime.startsWith("image/") - ? `` - : file.mime.startsWith("video/") - ? ` - - - - ` + - // quick lazy fix as a fallback - // maybe i'll improve this later, but probably not. - ((file.sizeInBytes || 0) >= 26214400 - ? ` - - ` - : "") - : "") + - (fileOwner?.embed?.largeImage && - file.visibility != "anonymous" && - file.mime.startsWith("image/") - ? `` - : "") + - `\n` - ) - .replace( - "", - file.mime.startsWith("image/") - ? `
` - : file.mime.startsWith("video/") - ? `
` - : file.mime.startsWith("audio/") - ? `
` - : "" - ) - .replaceAll( - "$Uploader", - !file.owner || file.visibility == "anonymous" - ? "Anonymous" - : `@${fileOwner?.username || "Deleted User"}` - ) - ) - }) - } else { - ServeError(res, 404, "file not found") - } -}) +app.get("/download/:fileId", getAccount, preview(files)) /* routes should be in this order: diff --git a/src/server/preview.ts b/src/server/preview.ts new file mode 100644 index 0000000..af52cb2 --- /dev/null +++ b/src/server/preview.ts @@ -0,0 +1,109 @@ +import fs from "fs/promises" +import bytes from "bytes" +import ServeError from "./lib/errors" +import * as Accounts from "./lib/accounts" +import type { Handler } from "express" +import type Files from "./lib/files" +const pkg = require(`${process.cwd()}/package.json`) +export = (files: Files): Handler => + async (req, res) => { + let acc = res.locals.acc as Accounts.Account + const file = files.getFilePointer(req.params.fileId) + if (file) { + if (file.visibility == "private" && acc?.id != file.owner) { + ServeError(res, 403, "you do not own this file") + return + } + + const template = await fs + .readFile(process.cwd() + "/pages/download.html", "utf8") + .catch(() => { + throw res.sendStatus(500) + }) + let fileOwner = file.owner + ? Accounts.getFromId(file.owner) + : undefined + + res.send( + template + .replaceAll("$FileId", req.params.fileId) + .replaceAll("$Version", pkg.version) + .replaceAll( + "$FileSize", + file.sizeInBytes + ? bytes(file.sizeInBytes) + : "[File size unknown]" + ) + .replaceAll( + "$FileName", + file.filename + .replaceAll("&", "&") + .replaceAll("<", "<") + .replaceAll(">", ">") + ) + .replace( + "", + (file.mime.startsWith("image/") + ? `` + : file.mime.startsWith("video/") + ? ` + + + + ` + + // quick lazy fix as a fallback + // maybe i'll improve this later, but probably not. + ((file.sizeInBytes || 0) >= 26214400 + ? ` + + ` + : "") + : "") + + (fileOwner?.embed?.largeImage && + file.visibility != "anonymous" && + file.mime.startsWith("image/") + ? `` + : "") + + `\n` + ) + .replace( + "", + file.mime.startsWith("image/") + ? `
` + : file.mime.startsWith("video/") + ? `
` + : file.mime.startsWith("audio/") + ? `
` + : "" + ) + .replaceAll( + "$Uploader", + !file.owner || file.visibility == "anonymous" + ? "Anonymous" + : `@${fileOwner?.username || "Deleted User"}` + ) + ) + } else { + ServeError(res, 404, "file not found") + } + }