EUS/controllers/HomeController.ts

59 lines
1.7 KiB
TypeScript
Raw Normal View History

2025-01-01 22:03:59 +00:00
import Controller from "./Controller";
2025-01-08 14:50:10 +00:00
import type DashboardViewModel from "../models/home/DashboardViewModel";
import UserService from "../services/UserService";
2025-01-26 10:34:56 +00:00
import DomainService from "../services/DomainService";
import ArrayUtility from "../utilities/ArrayUtility";
2025-01-01 22:03:59 +00:00
export default class HomeController extends Controller {
2025-01-08 08:19:11 +00:00
public async Index_Get_AllowAnonymous() {
2025-01-06 06:53:13 +00:00
if (this.session) {
2025-01-08 08:19:11 +00:00
const dashboardViewModel: DashboardViewModel = {
2025-01-26 10:34:56 +00:00
recentUploads: await UserService.GetRecentUploads(this.session.userId),
domains: ArrayUtility.ToIdKeyedDict(await DomainService.LoadDomains())
2025-01-08 08:19:11 +00:00
}
return this.view("dashboard",dashboardViewModel);
2025-01-06 06:53:13 +00:00
}
2025-01-01 22:03:59 +00:00
return this.view();
}
2025-01-03 03:11:00 +00:00
2025-01-27 00:10:56 +00:00
public async ImageList_Get() {
return this.view();
}
2025-01-05 14:22:18 +00:00
public async Upload_Post_AllowAnonymous() {
const data = await this.req.file();
if (data && data.type === "file") {
let uploadKey: string = "";
2025-01-06 06:53:13 +00:00
let host: string = "";
2025-01-26 04:22:59 +00:00
//console.log(this.req.headers);
2025-01-05 14:22:18 +00:00
if ("upload-key" in this.req.headers) {
// @ts-ignore
uploadKey = this.req.headers["upload-key"];
2025-01-06 06:53:13 +00:00
} else {
return this.unauthorised("Upload key invalid or missing.");
2025-01-05 14:22:18 +00:00
}
2025-01-06 06:53:13 +00:00
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);
2025-01-05 14:22:18 +00:00
}
2025-01-03 03:11:00 +00:00
2025-01-05 14:22:18 +00:00
return this.badRequest();
2025-01-03 03:11:00 +00:00
}
2025-01-01 22:03:59 +00:00
}