import { Console } from "hsconsole"; import UserRepo from "../repos/UserRepo"; import PasswordUtility from "../utilities/PasswordUtility"; import UserType from "../enums/UserType"; import User from "../entities/User"; import MediaRepo from "../repos/MediaRepo"; import { type MultipartFile } from "@fastify/multipart" import HashFS from "../objects/HashFS"; import Media from "../entities/Media"; import { randomBytes } from "crypto"; import DomainRepo from "../repos/DomainRepo"; export default abstract class UserService { public static async AuthenticateUser(username:string, password:string) { try { const user = await UserRepo.SelectByUsername(username); if (!user) { return null; } if (await PasswordUtility.ValidatePassword(user.PasswordHash, user.PasswordSalt, password)) { return user; } return null; } catch (e) { Console.printError(`EUS server service error:\n${e}`); throw e; } } public static async GetUser(id:number) { try { return await UserRepo.SelectById(id); } catch (e) { Console.printError(`EUS server service error:\n${e}`); throw e; } } public static async GetAll() { try { return await UserRepo.SelectAll(); } catch (e) { Console.printError(`EUS server service error:\n${e}`); throw e; } } public static async GetUserByUsername(username:string) { try { return await UserRepo.SelectByUsername(username); } catch (e) { Console.printError(`EUS server service error:\n${e}`); throw e; } } public static async CreateUser(currentUserId: number, username: string, email: string, password: string) { try { const existingCheck = await UserRepo.SelectByUsername(username); if (existingCheck) { return null; } const user = new User(); user.UserType = UserType.User; user.Username = username; user.EmailAddress = email; user.PasswordSalt = PasswordUtility.GenerateSalt(); user.PasswordHash = await PasswordUtility.HashPassword(user.PasswordSalt, password); user.ApiKey = randomBytes(64).toString("base64url"); user.UploadKey = randomBytes(64).toString("base64url"); user.CreatedByUserId = currentUserId; user.CreatedDatetime = new Date(); await UserRepo.InsertUpdate(user); return user; } catch (e) { Console.printError(`EUS server service error:\n${e}`); throw e; } } public static async GetRecentUploads(currentUserId: number) { try { return await MediaRepo.SelectRecentMedia(currentUserId, 10); } catch (e) { Console.printError(`EUS server service error:\n${e}`); throw e; } } public static async GetByUploadKey(uploadKey: string) { try { return await UserRepo.SelectByUploadKey(uploadKey); } catch (e) { Console.printError(`EUS server service error:\n${e}`); throw e; } } public static async UploadMedia(currentUserId: number, host: string, data: MultipartFile) { try { const fileInfo = await HashFS.GetHashFSInstance("images").AddFromStream(data.file); const domain = await DomainRepo.SelectByDomain(host); if (!domain) { return null; } let media = await MediaRepo.SelectByUserHash(currentUserId, fileInfo.fileHash); if (!media) { media = new Media(); media.CreatedByUserId = currentUserId; media.CreatedDatetime = new Date(); media.UserId = currentUserId; media.DomainId = domain.Id; // TODO: Make this come from the host. Only EUS's domain is supported for now. media.FileName = data.filename; media.MediaTag = randomBytes(12).toString("base64url"); media.MediaType = data.mimetype; media.Hash = fileInfo.fileHash; media.FileSize = fileInfo.fileSize; await MediaRepo.InsertUpdate(media); } return `${domain.HasHttps ? "https" : "http"}://${domain.Domain}/${media.MediaTag}`; } catch (e) { Console.printError(`EUS server service error:\n${e}`); throw e; } } }