move actions to a more suitable location
This commit is contained in:
parent
f2a0bfdcea
commit
1871abee00
11 changed files with 99 additions and 58 deletions
BIN
bun.lockb
BIN
bun.lockb
Binary file not shown.
|
@ -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("/");
|
||||
}
|
||||
}
|
|
@ -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 }
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
39
controllers/UploadController.ts
Normal file
39
controllers/UploadController.ts
Normal 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();
|
||||
}
|
||||
}
|
13
index.ts
13
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.`);
|
||||
}
|
||||
|
|
|
@ -31,7 +31,8 @@ interface ISession {
|
|||
}
|
||||
|
||||
interface IControllers {
|
||||
enabled: boolean
|
||||
enabled: boolean,
|
||||
"home-enabled": boolean
|
||||
}
|
||||
|
||||
interface ISignup {
|
||||
|
|
|
@ -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 ?? "");
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue