From a0fcc376b7a1c6bc66a26b00cbf1ee9492d129f0 Mon Sep 17 00:00:00 2001 From: Holly Date: Sun, 26 Jan 2025 04:22:59 +0000 Subject: [PATCH] media requests work! --- controllers/Controller.ts | 2 +- controllers/HomeController.ts | 2 +- index.ts | 28 ++++++++++++++-------------- utilities/HeaderUtility.ts | 26 +++++++++++++++++++++++--- 4 files changed, 39 insertions(+), 19 deletions(-) diff --git a/controllers/Controller.ts b/controllers/Controller.ts index c6d398c..a6bd8b8 100644 --- a/controllers/Controller.ts +++ b/controllers/Controller.ts @@ -74,7 +74,7 @@ export default abstract class Controller { } if (controllerName !== "api") { - HeaderUtility.AddHeaders(res); + HeaderUtility.AddBakedHeaders(res); } const requestCtx = new RequestCtx(req, res, controllerName, methodName, session); diff --git a/controllers/HomeController.ts b/controllers/HomeController.ts index adeef43..cf24db2 100644 --- a/controllers/HomeController.ts +++ b/controllers/HomeController.ts @@ -20,7 +20,7 @@ export default class HomeController extends Controller { if (data && data.type === "file") { let uploadKey: string = ""; let host: string = ""; - console.log(this.req.headers); + //console.log(this.req.headers); if ("upload-key" in this.req.headers) { // @ts-ignore uploadKey = this.req.headers["upload-key"]; diff --git a/index.ts b/index.ts index f222af2..133cb62 100644 --- a/index.ts +++ b/index.ts @@ -3,7 +3,6 @@ import FastifyFormBody from "@fastify/formbody"; import FastifyMultipart from "@fastify/multipart"; import FastifyCookie from "@fastify/cookie"; import FastifyView from "@fastify/view"; -import FastifySend from "@fastify/send" import FastifyStatic from "@fastify/static"; import Config from "./objects/Config"; import EJS from "ejs"; @@ -20,6 +19,7 @@ import FunkyArray from "funky-array"; import MediaService from "./services/MediaService"; import Media from "./entities/Media"; import HeaderUtility from "./utilities/HeaderUtility"; +import { createReadStream } from "fs"; Console.customHeader(`EUS server started at ${new Date()}`); @@ -57,12 +57,12 @@ fastify.addHook("preHandler", (req, res, done) => { (async () => { // @ts-ignore req.startTime = Date.now(); - HeaderUtility.AddHeaders(res); // * Take usual controller path if this path is registered. if (Controller.RegisteredPaths.includes(req.url)) { // @ts-ignore req.logType = cyan("CONTROLLER"); + HeaderUtility.AddBakedHeaders(res); return done(); } else { const urlParts = req.url.split("/"); @@ -79,25 +79,23 @@ fastify.addHook("preHandler", (req, res, done) => { // @ts-ignore req.logType = cyan("IMAGE"); const fileStore = HashFS.GetHashFSInstance("images"); - const { statusCode, headers, stream } = await FastifySend(req.raw, join(fileStore.path, fileStore.GetRelativePath(media.Hash)), {}); - headers["Content-Type"] = media.MediaType; - if (statusCode === 200) { - res.headers(headers); - HeaderUtility.AddHeaders(res); - stream.pipe(res.raw); - return; - } - - res.statusCode = statusCode; - return done(); + const readStream = createReadStream(join(fileStore.path, fileStore.GetRelativePath(media.Hash))); + res.raw.writeHead(200, HeaderUtility.CombineHeaders({ + "content-type": media.MediaType, + "content-length": media.FileSize, + })); + readStream.pipe(res.raw); + return; } + } else { + HeaderUtility.AddBakedHeaders(res); } // @ts-ignore req.logType = magenta("STATIC"); } - done(); + return done(); })(); }); @@ -105,6 +103,8 @@ fastify.addHook("onSend", (req, res, _payload, done) => { // @ts-ignore Console.printInfo(`[ ${req.logType} ] [ ${req.method.toUpperCase()} ] [ ${ConsoleUtility.StatusColor(res.statusCode)} ] [ ${blue(`${Date.now() - req.startTime}ms`)} ] > ${req.url}`); + //console.log(res.getHeaders()); + done(); }); diff --git a/utilities/HeaderUtility.ts b/utilities/HeaderUtility.ts index 4851d48..490130e 100644 --- a/utilities/HeaderUtility.ts +++ b/utilities/HeaderUtility.ts @@ -1,8 +1,20 @@ import type { FastifyReply } from "fastify"; export default abstract class HeaderUtility { - public static AddHeaders(res: FastifyReply) { - res.header("x-powered-by", "EUS"); + public static BakedHeaders = { + "x-powered-by": "EUS", + "rel": "cute", + "Strict-Transport-Security": "max-age=31536000; includeSubDomains", + "X-XSS-Protection": "1; mode=block", + "Permissions-Policy": "microphone=(), geolocation=(), magnetometer=(), camera=(), payment=(), usb=(), accelerometer=(), gyroscope=()", + "Referrer-Policy": "strict-origin-when-cross-origin", + "Content-Security-Policy": "block-all-mixed-content;frame-ancestors 'self'", + "X-Frame-Options": "SAMEORIGIN", + "X-Content-Type-Options": "nosniff" + }; + + public static AddBakedHeaders(res: FastifyReply) { + /*res.header("x-powered-by", "EUS"); res.header("rel", "cute"); res.header("Strict-Transport-Security", "max-age=31536000; includeSubDomains"); res.header("X-XSS-Protection", "1; mode=block"); @@ -10,6 +22,14 @@ export default abstract class HeaderUtility { res.header("Referrer-Policy", "strict-origin-when-cross-origin"); res.header("Content-Security-Policy", "block-all-mixed-content;frame-ancestors 'self'"); res.header("X-Frame-Options", "SAMEORIGIN"); - res.header("X-Content-Type-Options", "nosniff"); + res.header("X-Content-Type-Options", "nosniff");*/ + res.headers(this.BakedHeaders); + } + + public static CombineHeaders(headers: any) { + // for (const header of Object.keys(headers)) { + // res.header(header, headers[header]); + // } + return { ...this.BakedHeaders, ...headers }; } } \ No newline at end of file