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") {
|
if (controllerName !== "api") {
|
||||||
HeaderUtility.AddHeaders(res);
|
HeaderUtility.AddBakedHeaders(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
const requestCtx = new RequestCtx(req, res, controllerName, methodName, session);
|
const requestCtx = new RequestCtx(req, res, controllerName, methodName, session);
|
||||||
|
|
|
@ -20,7 +20,7 @@ export default class HomeController extends Controller {
|
||||||
if (data && data.type === "file") {
|
if (data && data.type === "file") {
|
||||||
let uploadKey: string = "";
|
let uploadKey: string = "";
|
||||||
let host: string = "";
|
let host: string = "";
|
||||||
console.log(this.req.headers);
|
//console.log(this.req.headers);
|
||||||
if ("upload-key" in this.req.headers) {
|
if ("upload-key" in this.req.headers) {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
uploadKey = this.req.headers["upload-key"];
|
uploadKey = this.req.headers["upload-key"];
|
||||||
|
|
28
index.ts
28
index.ts
|
@ -3,7 +3,6 @@ import FastifyFormBody from "@fastify/formbody";
|
||||||
import FastifyMultipart from "@fastify/multipart";
|
import FastifyMultipart from "@fastify/multipart";
|
||||||
import FastifyCookie from "@fastify/cookie";
|
import FastifyCookie from "@fastify/cookie";
|
||||||
import FastifyView from "@fastify/view";
|
import FastifyView from "@fastify/view";
|
||||||
import FastifySend from "@fastify/send"
|
|
||||||
import FastifyStatic from "@fastify/static";
|
import FastifyStatic from "@fastify/static";
|
||||||
import Config from "./objects/Config";
|
import Config from "./objects/Config";
|
||||||
import EJS from "ejs";
|
import EJS from "ejs";
|
||||||
|
@ -20,6 +19,7 @@ import FunkyArray from "funky-array";
|
||||||
import MediaService from "./services/MediaService";
|
import MediaService from "./services/MediaService";
|
||||||
import Media from "./entities/Media";
|
import Media from "./entities/Media";
|
||||||
import HeaderUtility from "./utilities/HeaderUtility";
|
import HeaderUtility from "./utilities/HeaderUtility";
|
||||||
|
import { createReadStream } from "fs";
|
||||||
|
|
||||||
Console.customHeader(`EUS server started at ${new Date()}`);
|
Console.customHeader(`EUS server started at ${new Date()}`);
|
||||||
|
|
||||||
|
@ -57,12 +57,12 @@ fastify.addHook("preHandler", (req, res, done) => {
|
||||||
(async () => {
|
(async () => {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
req.startTime = Date.now();
|
req.startTime = Date.now();
|
||||||
HeaderUtility.AddHeaders(res);
|
|
||||||
|
|
||||||
// * Take usual controller path if this path is registered.
|
// * Take usual controller path if this path is registered.
|
||||||
if (Controller.RegisteredPaths.includes(req.url)) {
|
if (Controller.RegisteredPaths.includes(req.url)) {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
req.logType = cyan("CONTROLLER");
|
req.logType = cyan("CONTROLLER");
|
||||||
|
HeaderUtility.AddBakedHeaders(res);
|
||||||
return done();
|
return done();
|
||||||
} else {
|
} else {
|
||||||
const urlParts = req.url.split("/");
|
const urlParts = req.url.split("/");
|
||||||
|
@ -79,25 +79,23 @@ fastify.addHook("preHandler", (req, res, done) => {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
req.logType = cyan("IMAGE");
|
req.logType = cyan("IMAGE");
|
||||||
const fileStore = HashFS.GetHashFSInstance("images");
|
const fileStore = HashFS.GetHashFSInstance("images");
|
||||||
const { statusCode, headers, stream } = await FastifySend(req.raw, join(fileStore.path, fileStore.GetRelativePath(media.Hash)), {});
|
const readStream = createReadStream(join(fileStore.path, fileStore.GetRelativePath(media.Hash)));
|
||||||
headers["Content-Type"] = media.MediaType;
|
res.raw.writeHead(200, HeaderUtility.CombineHeaders({
|
||||||
if (statusCode === 200) {
|
"content-type": media.MediaType,
|
||||||
res.headers(headers);
|
"content-length": media.FileSize,
|
||||||
HeaderUtility.AddHeaders(res);
|
}));
|
||||||
stream.pipe(res.raw);
|
readStream.pipe(res.raw);
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
res.statusCode = statusCode;
|
|
||||||
return done();
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
HeaderUtility.AddBakedHeaders(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
req.logType = magenta("STATIC");
|
req.logType = magenta("STATIC");
|
||||||
}
|
}
|
||||||
|
|
||||||
done();
|
return done();
|
||||||
})();
|
})();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -105,6 +103,8 @@ fastify.addHook("onSend", (req, res, _payload, done) => {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
Console.printInfo(`[ ${req.logType} ] [ ${req.method.toUpperCase()} ] [ ${ConsoleUtility.StatusColor(res.statusCode)} ] [ ${blue(`${Date.now() - req.startTime}ms`)} ] > ${req.url}`);
|
Console.printInfo(`[ ${req.logType} ] [ ${req.method.toUpperCase()} ] [ ${ConsoleUtility.StatusColor(res.statusCode)} ] [ ${blue(`${Date.now() - req.startTime}ms`)} ] > ${req.url}`);
|
||||||
|
|
||||||
|
//console.log(res.getHeaders());
|
||||||
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,20 @@
|
||||||
import type { FastifyReply } from "fastify";
|
import type { FastifyReply } from "fastify";
|
||||||
|
|
||||||
export default abstract class HeaderUtility {
|
export default abstract class HeaderUtility {
|
||||||
public static AddHeaders(res: FastifyReply) {
|
public static BakedHeaders = {
|
||||||
res.header("x-powered-by", "EUS");
|
"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("rel", "cute");
|
||||||
res.header("Strict-Transport-Security", "max-age=31536000; includeSubDomains");
|
res.header("Strict-Transport-Security", "max-age=31536000; includeSubDomains");
|
||||||
res.header("X-XSS-Protection", "1; mode=block");
|
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("Referrer-Policy", "strict-origin-when-cross-origin");
|
||||||
res.header("Content-Security-Policy", "block-all-mixed-content;frame-ancestors 'self'");
|
res.header("Content-Security-Policy", "block-all-mixed-content;frame-ancestors 'self'");
|
||||||
res.header("X-Frame-Options", "SAMEORIGIN");
|
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