media requests work!
This commit is contained in:
parent
fd606996fd
commit
a0fcc376b7
4 changed files with 39 additions and 19 deletions
|
@ -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);
|
||||
|
|
|
@ -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"];
|
||||
|
|
26
index.ts
26
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);
|
||||
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;
|
||||
}
|
||||
|
||||
res.statusCode = statusCode;
|
||||
return done();
|
||||
}
|
||||
} 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();
|
||||
});
|
||||
|
||||
|
|
|
@ -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 };
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue