move actions to a more suitable location

This commit is contained in:
Holly Stubbs 2025-02-01 02:51:22 +00:00
parent f2a0bfdcea
commit 1871abee00
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
11 changed files with 99 additions and 58 deletions

BIN
bun.lockb

Binary file not shown.

View file

@ -1,12 +1,19 @@
import type LoginViewModel from "../models/account/LoginViewModel"; import type LoginViewModel from "../models/account/LoginViewModel";
import type RegisterViewModel from "../models/account/RegisterViewModel"; import type RegisterViewModel from "../models/account/RegisterViewModel";
import type DashboardViewModel from "../models/home/DashboardViewModel";
import Config from "../objects/Config"; import Config from "../objects/Config";
import Session from "../objects/Session"; import Session from "../objects/Session";
import DomainService from "../services/DomainService";
import UserService from "../services/UserService"; import UserService from "../services/UserService";
import ArrayUtility from "../utilities/ArrayUtility";
import Controller from "./Controller"; import Controller from "./Controller";
export default class AccountController extends Controller { export default class AccountController extends Controller {
public async Login_Get_AllowAnonymous() { public async Login_Get_AllowAnonymous() {
if (this.session) {
return this.redirectToAction("dashboard");
}
return this.view(); return this.view();
} }
@ -25,10 +32,14 @@ export default class AccountController extends Controller {
Session.AssignUserSession(this.res, user); Session.AssignUserSession(this.res, user);
return this.redirectToAction("index", "home"); return this.redirectToAction("dashboard");
} }
public async Register_Get_AllowAnonymous() { public async Register_Get_AllowAnonymous() {
if (this.session) {
return this.redirectToAction("dashboard");
}
return this.view(); return this.view();
} }
@ -68,9 +79,34 @@ export default class AccountController extends Controller {
return this.redirectToAction("index", "home"); 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() { public async Logout_Get_AllowAnonymous() {
Session.Clear(this.req.cookies, this.res); Session.Clear(this.req.cookies, this.res);
return this.redirectToAction("index", "home"); return this.redirect("/");
} }
} }

View file

@ -143,6 +143,7 @@ export default abstract class Controller {
// Methods // Methods
view(view?:string | Object, model?: Object) { view; model; } view(view?:string | Object, model?: Object) { view; model; }
redirectToAction(action:string, controller?:string) { action; controller; } redirectToAction(action:string, controller?:string) { action; controller; }
redirect(url:string) { url }
ok(message?:string) { message } ok(message?:string) { message }
badRequest(message?:string) { message } badRequest(message?:string) { message }
unauthorised(message?:string) { message } unauthorised(message?:string) { message }

View file

@ -1,59 +1,7 @@
import Controller from "./Controller"; 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 { export default class HomeController extends Controller {
public async Index_Get_AllowAnonymous() { public 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);
}
return this.view(); 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();
}
} }

View file

@ -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();
}
}

View file

@ -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 Fastify, { type FastifyReply, type FastifyRequest } from "fastify";
import FastifyFormBody from "@fastify/formbody"; import FastifyFormBody from "@fastify/formbody";
import FastifyMultipart from "@fastify/multipart"; import FastifyMultipart from "@fastify/multipart";
@ -20,6 +25,8 @@ 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"; import { createReadStream } from "fs";
import ApiController from "./controllers/ApiController";
import UploadController from "./controllers/UploadController";
Console.customHeader(`EUS server started at ${new Date()}`); Console.customHeader(`EUS server started at ${new Date()}`);
@ -134,7 +141,11 @@ if (Config.database.enabled) {
if (Config.controllers.enabled && Config.database.enabled) { if (Config.controllers.enabled && Config.database.enabled) {
Controller.FastifyInstance = fastify; Controller.FastifyInstance = fastify;
new AccountController(); new AccountController();
new ApiController();
if (Config.controllers["home-enabled"]) {
new HomeController(); new HomeController();
}
new UploadController();
} else { } 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.`); 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.`);
} }

View file

@ -31,7 +31,8 @@ interface ISession {
} }
interface IControllers { interface IControllers {
enabled: boolean enabled: boolean,
"home-enabled": boolean
} }
interface ISignup { interface ISignup {

View file

@ -49,6 +49,10 @@ export default class RequestCtx {
} }
} }
redirect(url:string) {
return this.res.redirect(url);
}
ok(message?:string) { ok(message?:string) {
return this.res.status(200).send(message ?? ""); return this.res.status(200).send(message ?? "");
} }

View file

@ -36,6 +36,7 @@
"fastify": "^5.2.1", "fastify": "^5.2.1",
"funky-array": "^1.0.0", "funky-array": "^1.0.0",
"hsconsole": "^1.1.0", "hsconsole": "^1.1.0",
"mysql2": "^3.12.0" "mysql2": "^3.12.0",
"watcher": "^2.3.1"
} }
} }