1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-23 05:49:23 +00:00
comunicapiv2/src/helpers/AccountImageHelper.ts

75 lines
1.9 KiB
TypeScript
Raw Normal View History

2019-11-23 15:10:51 +00:00
import { pathUserData } from "../utils/UserDataUtils";
import { existsSync, readFileSync } 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<AccountImage> {
const accountImageFileContent = this.GetFileAccountImage(userID);
const level = this.GetVisibilityLevel(userID);
return new AccountImage(userID, accountImageFileContent, 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);
}
}