EUS/controllers/HomeController.ts

55 lines
1.5 KiB
TypeScript
Raw Normal View History

2025-01-08 08:19:11 +00:00
import DashboardViewModel from "../models/home/DashboardViewModel";
2025-01-05 14:22:18 +00:00
import Config from "../objects/Config";
import HashFS from "../objects/HashFS";
2025-01-06 06:53:13 +00:00
import UserService from "../services/UserService";
2025-01-01 22:03:59 +00:00
import Controller from "./Controller";
2025-01-05 14:22:18 +00:00
import { randomBytes } from "crypto";
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 = {
recentUploads: await UserService.GetRecentUploads(this.session.userId)
}
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-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 = "";
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
}