1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-21 21:09:22 +00:00

Make account image helper use the database instead of files storage

This commit is contained in:
Pierre HUBERT 2020-05-25 18:10:35 +02:00
parent cfb1bb4106
commit 26f6c12f35

View File

@ -1,6 +1,8 @@
import { pathUserData } from "../utils/UserDataUtils";
import { existsSync, readFileSync, unlinkSync, writeFileSync } from "fs";
import { AccountImage, AccountImageVisibilityLevel } from "../entities/AccountImage";
import { existsSync, unlinkSync } from "fs";
import { AccountImageVisibilityLevel } from "../entities/User";
import { DatabaseHelper } from "./DatabaseHelper";
import { USER_TABLE } from "./AccountHelper";
import { UserHelper } from "./UserHelper";
/**
* Account image helper
@ -10,20 +12,6 @@ import { AccountImage, AccountImageVisibilityLevel } from "../entities/AccountIm
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);
}
/**
* Set a new account image for a user
*
@ -33,11 +21,17 @@ export class AccountImageHelper {
public static async Set(userID: number, path: string) {
// First, delete any previous account image
const currInfo = await this.Get(userID);
if(currInfo.hasImage && existsSync(currInfo.sysPath))
unlinkSync(currInfo.sysPath);
await this.Delete(userID);
writeFileSync(this.GetPathMetadataFile(userID), path.replace("avatars/", ""));
await DatabaseHelper.UpdateRows({
table: USER_TABLE,
where: {
ID: userID
},
set: {
account_image_path: path
}
});
}
/**
@ -46,19 +40,21 @@ export class AccountImageHelper {
* @param userID Target user ID
*/
public static async Delete(userID: number) {
const currInfo = await this.Get(userID);
if(currInfo.hasImage) {
if(existsSync(currInfo.sysPath))
unlinkSync(currInfo.sysPath);
const user = await UserHelper.GetUserInfo(userID);
if(user.hasAccountImage) {
if(existsSync(user.accountImageSysPath))
unlinkSync(user.accountImageSysPath);
// Delete meta file
if(existsSync(this.GetPathMetadataFile(userID)))
unlinkSync(this.GetPathMetadataFile(userID));
// Delete visiblity file (if any)
const visibilityFile = this.GetPathVisibilityFile(userID);
if(existsSync(visibilityFile))
unlinkSync(visibilityFile);
// Update database
await DatabaseHelper.UpdateRows({
table: USER_TABLE,
where: {
ID: userID
},
set: {
account_image_path: ""
}
});
}
}
@ -69,68 +65,15 @@ export class AccountImageHelper {
* @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);
await DatabaseHelper.UpdateRows({
table: USER_TABLE,
where: {
ID: userID
},
set: {
account_image_visibility: 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);
}
}