import { pathUserData } from "../utils/UserDataUtils"; import { existsSync, readFileSync, unlinkSync, writeFileSync } from "fs"; import { AccountImage, AccountImageVisibilityLevel } from "../entities/AccountImage"; /** * Account image helper * * @author Pierre HUBERT */ export class AccountImageHelper { /** * Get information about the account image of a specific user ID * * @param userID Target user ID * @return The path to user image */ public static async Get(userID : number) : Promise { const accountImageFileContent = this.GetFileAccountImage(userID); const level = this.GetVisibilityLevel(userID); return new AccountImage(userID, accountImageFileContent, level); } /** * Set a new account image for a user * * @param userID Target user ID * @param path The path for the account image */ public static async Set(userID: number, path: string) { // First, delete any previous account image const currInfo = await this.Get(userID); if(currInfo.hasImage) unlinkSync(currInfo.sysPath); writeFileSync(this.GetPathMetadataFile(userID), path.replace("avatars/", "")); } /** * Delete (remove) a user account image * * @param userID Target user ID */ public static async Delete(userID: number) { const currInfo = await this.Get(userID); if(currInfo.hasImage) { unlinkSync(currInfo.sysPath); // Delete meta file unlinkSync(this.GetPathMetadataFile(userID)); // Delete visiblity file (if any) const visibilityFile = this.GetPathVisibilityFile(userID); if(existsSync(visibilityFile)) unlinkSync(visibilityFile); } } /** * Change account image visiblity level * * @param userID Target user ID * @param level New level for account image */ public static async SetVisibilityLevel(userID: number, level: AccountImageVisibilityLevel) { const file = this.GetPathVisibilityFile(userID); // If the visiblity is set to everyone, we do not // need to have a visibility file if(level == AccountImageVisibilityLevel.EVERYONE) { if(existsSync(file)) unlinkSync(file); return; } else writeFileSync(file, level); } /** * Get the content of the file associated to the user account image, if any * * @param userID Target user ID * @returns The content of the file */ private static GetFileAccountImage(userID: number) : string { const fileName = this.GetPathMetadataFile(userID); if(!existsSync(fileName)) return ""; return readFileSync(fileName, {encoding: "utf-8"}); } /** * Get the visibility leve of a user account image * * @param userID Target user ID */ private static GetVisibilityLevel(userID: number) : AccountImageVisibilityLevel { const filePath = this.GetPathVisibilityFile(userID); if(!existsSync(filePath)) return AccountImageVisibilityLevel.EVERYONE; return Number.parseInt(readFileSync(filePath, {encoding: "utf-8"})); } /** * Get the path to the file that contains the path to user account image * * @param userID Target user ID */ private static GetPathMetadataFile(userID: number) { return pathUserData("avatars/adresse_avatars/" + userID.toString() + ".txt", true); } /** * Get the path to visibility file * * @param userID Target user ID */ private static GetPathVisibilityFile(userID: number) { return pathUserData("avatars/adresse_avatars/limit_view_" + userID + ".txt", true); } }