diff --git a/bun.lockb b/bun.lockb index 93d7318..1afd8f6 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/controllers/AccountController.ts b/controllers/AccountController.ts index cd6cee4..61c7ca2 100644 --- a/controllers/AccountController.ts +++ b/controllers/AccountController.ts @@ -1,12 +1,19 @@ import type LoginViewModel from "../models/account/LoginViewModel"; import type RegisterViewModel from "../models/account/RegisterViewModel"; +import type DashboardViewModel from "../models/home/DashboardViewModel"; import Config from "../objects/Config"; import Session from "../objects/Session"; +import DomainService from "../services/DomainService"; import UserService from "../services/UserService"; +import ArrayUtility from "../utilities/ArrayUtility"; import Controller from "./Controller"; export default class AccountController extends Controller { public async Login_Get_AllowAnonymous() { + if (this.session) { + return this.redirectToAction("dashboard"); + } + return this.view(); } @@ -25,10 +32,14 @@ export default class AccountController extends Controller { Session.AssignUserSession(this.res, user); - return this.redirectToAction("index", "home"); + return this.redirectToAction("dashboard"); } public async Register_Get_AllowAnonymous() { + if (this.session) { + return this.redirectToAction("dashboard"); + } + return this.view(); } @@ -68,9 +79,34 @@ export default class AccountController extends Controller { return this.redirectToAction("index", "home"); } + public async Index_Get_AllowAnonymous() { + if (this.session) { + return this.redirectToAction("dashboard"); + } + + return this.redirectToAction("login"); + } + + public async Dashboard_Get() { + if (this.session) { + const dashboardViewModel: DashboardViewModel = { + recentUploads: await UserService.GetRecentUploads(this.session.userId), + domains: ArrayUtility.ToIdKeyedDict(await DomainService.LoadDomains()) + } + + return this.view(dashboardViewModel); + } + + return this.redirect("/"); + } + + public async ImageList_Get() { + return this.view(); + } + public async Logout_Get_AllowAnonymous() { Session.Clear(this.req.cookies, this.res); - return this.redirectToAction("index", "home"); + return this.redirect("/"); } } \ No newline at end of file diff --git a/controllers/Controller.ts b/controllers/Controller.ts index a6bd8b8..1b6e8bf 100644 --- a/controllers/Controller.ts +++ b/controllers/Controller.ts @@ -143,6 +143,7 @@ export default abstract class Controller { // Methods view(view?:string | Object, model?: Object) { view; model; } redirectToAction(action:string, controller?:string) { action; controller; } + redirect(url:string) { url } ok(message?:string) { message } badRequest(message?:string) { message } unauthorised(message?:string) { message } diff --git a/controllers/HomeController.ts b/controllers/HomeController.ts index a8e5e29..2757aca 100644 --- a/controllers/HomeController.ts +++ b/controllers/HomeController.ts @@ -1,59 +1,7 @@ import Controller from "./Controller"; -import type DashboardViewModel from "../models/home/DashboardViewModel"; -import UserService from "../services/UserService"; -import DomainService from "../services/DomainService"; -import ArrayUtility from "../utilities/ArrayUtility"; export default class HomeController extends Controller { - public async Index_Get_AllowAnonymous() { - if (this.session) { - const dashboardViewModel: DashboardViewModel = { - recentUploads: await UserService.GetRecentUploads(this.session.userId), - domains: ArrayUtility.ToIdKeyedDict(await DomainService.LoadDomains()) - } - - return this.view("dashboard",dashboardViewModel); - } - + public Index_Get_AllowAnonymous() { return this.view(); } - - public async ImageList_Get() { - return this.view(); - } - - public async Upload_Post_AllowAnonymous() { - const data = await this.req.file(); - if (data && data.type === "file") { - let uploadKey: string = ""; - let host: string = ""; - //console.log(this.req.headers); - if ("upload-key" in this.req.headers) { - // @ts-ignore - uploadKey = this.req.headers["upload-key"]; - } else { - return this.unauthorised("Upload key invalid or missing."); - } - if ("host" in this.req.headers) { - // @ts-ignore - host = this.req.headers["host"]; - } else { - return this.badRequest("Host header missing?!"); - } - - const user = await UserService.GetByUploadKey(uploadKey); - if (!user) { - return this.unauthorised("Upload key invalid or missing."); - } - - const fileUrl = await UserService.UploadMedia(user.Id, host, data); - if (!fileUrl) { - return this.badRequest("This domain is not registered to your EUS account."); - } - - return this.ok(fileUrl); - } - - return this.badRequest(); - } } \ No newline at end of file diff --git a/controllers/UploadController.ts b/controllers/UploadController.ts new file mode 100644 index 0000000..3413fe2 --- /dev/null +++ b/controllers/UploadController.ts @@ -0,0 +1,39 @@ +import UserService from "../services/UserService"; +import Controller from "./Controller"; + +export default class UploadController extends Controller { + public async Index_Post_AllowAnonymous() { + const data = await this.req.file(); + if (data && data.type === "file") { + let uploadKey: string = ""; + let host: string = ""; + //console.log(this.req.headers); + if ("upload-key" in this.req.headers) { + // @ts-ignore + uploadKey = this.req.headers["upload-key"]; + } else { + return this.unauthorised("Upload key invalid or missing."); + } + if ("host" in this.req.headers) { + // @ts-ignore + host = this.req.headers["host"]; + } else { + return this.badRequest("Host header missing?!"); + } + + const user = await UserService.GetByUploadKey(uploadKey); + if (!user) { + return this.unauthorised("Upload key invalid or missing."); + } + + const fileUrl = await UserService.UploadMedia(user.Id, host, data); + if (!fileUrl) { + return this.badRequest("This domain is not registered to your EUS account."); + } + + return this.ok(fileUrl); + } + + return this.badRequest(); + } +} \ No newline at end of file diff --git a/index.ts b/index.ts index 614e2be..b035276 100644 --- a/index.ts +++ b/index.ts @@ -1,3 +1,8 @@ +if (!process.versions.bun) { + console.log("EUS is only designed to run on Bun, sorry!"); + process.exit(1); +} + import Fastify, { type FastifyReply, type FastifyRequest } from "fastify"; import FastifyFormBody from "@fastify/formbody"; import FastifyMultipart from "@fastify/multipart"; @@ -20,6 +25,8 @@ import MediaService from "./services/MediaService"; import Media from "./entities/Media"; import HeaderUtility from "./utilities/HeaderUtility"; import { createReadStream } from "fs"; +import ApiController from "./controllers/ApiController"; +import UploadController from "./controllers/UploadController"; Console.customHeader(`EUS server started at ${new Date()}`); @@ -134,7 +141,11 @@ if (Config.database.enabled) { if (Config.controllers.enabled && Config.database.enabled) { Controller.FastifyInstance = fastify; new AccountController(); - new HomeController(); + new ApiController(); + if (Config.controllers["home-enabled"]) { + new HomeController(); + } + new UploadController(); } else { Console.printInfo(`[ ${red("CONTROLLER")} ] Controllers are disabled${Config.controllers.enabled && !Config.database.enabled ? " because the database is disabled but required by the controllers." : "."} Server will operate in static mode only.`); } diff --git a/objects/Config.ts b/objects/Config.ts index 8de722f..5f53d50 100644 --- a/objects/Config.ts +++ b/objects/Config.ts @@ -31,7 +31,8 @@ interface ISession { } interface IControllers { - enabled: boolean + enabled: boolean, + "home-enabled": boolean } interface ISignup { diff --git a/objects/RequestCtx.ts b/objects/RequestCtx.ts index 11fd2b5..38f30ca 100644 --- a/objects/RequestCtx.ts +++ b/objects/RequestCtx.ts @@ -49,6 +49,10 @@ export default class RequestCtx { } } + redirect(url:string) { + return this.res.redirect(url); + } + ok(message?:string) { return this.res.status(200).send(message ?? ""); } diff --git a/package.json b/package.json index dd3ae5a..71ee5b2 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "fastify": "^5.2.1", "funky-array": "^1.0.0", "hsconsole": "^1.1.0", - "mysql2": "^3.12.0" + "mysql2": "^3.12.0", + "watcher": "^2.3.1" } } diff --git a/views/home/dashboard.ejs b/views/account/dashboard.ejs similarity index 100% rename from views/home/dashboard.ejs rename to views/account/dashboard.ejs diff --git a/views/home/imagelist.ejs b/views/account/imagelist.ejs similarity index 100% rename from views/home/imagelist.ejs rename to views/account/imagelist.ejs