mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2025-06-20 08:35:17 +00:00
Start to process user account image
This commit is contained in:
75
src/helpers/AccountImageHelper.ts
Normal file
75
src/helpers/AccountImageHelper.ts
Normal file
@ -0,0 +1,75 @@
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
import { User, UserPageStatus } from "../entities/User";
|
||||
import { DatabaseHelper } from "./DatabaseHelper";
|
||||
import { AccountImageHelper } from "./AccountImageHelper";
|
||||
|
||||
/**
|
||||
* User helper
|
||||
@ -32,14 +33,15 @@ export class UserHelper {
|
||||
}
|
||||
|
||||
|
||||
private static DbToUser(row: any) : User {
|
||||
private static async DbToUser(row: any) : Promise<User> {
|
||||
return new User({
|
||||
id: row.ID,
|
||||
firstName: row.prenom,
|
||||
lastName: row.nom,
|
||||
timeCreate: new Date(row.date_creation).getTime()/1000,
|
||||
virtualDirectory: row.sous_repertoire,
|
||||
pageStatus: row.pageouverte == 1 ? UserPageStatus.OPEN : (row.public == 1 ? UserPageStatus.PUBLIC : UserPageStatus.PRIVATE)
|
||||
pageStatus: row.pageouverte == 1 ? UserPageStatus.OPEN : (row.public == 1 ? UserPageStatus.PUBLIC : UserPageStatus.PRIVATE),
|
||||
accountImage: await AccountImageHelper.Get(row.ID)
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user