1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-22 05:19: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, unlinkSync } from "fs";
import { existsSync, readFileSync, unlinkSync, writeFileSync } from "fs"; import { AccountImageVisibilityLevel } from "../entities/User";
import { AccountImage, AccountImageVisibilityLevel } from "../entities/AccountImage"; import { DatabaseHelper } from "./DatabaseHelper";
import { USER_TABLE } from "./AccountHelper";
import { UserHelper } from "./UserHelper";
/** /**
* Account image helper * Account image helper
@ -10,20 +12,6 @@ import { AccountImage, AccountImageVisibilityLevel } from "../entities/AccountIm
export class AccountImageHelper { 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 * Set a new account image for a user
* *
@ -33,11 +21,17 @@ export class AccountImageHelper {
public static async Set(userID: number, path: string) { public static async Set(userID: number, path: string) {
// First, delete any previous account image // First, delete any previous account image
const currInfo = await this.Get(userID); await this.Delete(userID);
if(currInfo.hasImage && existsSync(currInfo.sysPath))
unlinkSync(currInfo.sysPath);
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 * @param userID Target user ID
*/ */
public static async Delete(userID: number) { public static async Delete(userID: number) {
const currInfo = await this.Get(userID); const user = await UserHelper.GetUserInfo(userID);
if(currInfo.hasImage) { if(user.hasAccountImage) {
if(existsSync(currInfo.sysPath)) if(existsSync(user.accountImageSysPath))
unlinkSync(currInfo.sysPath); unlinkSync(user.accountImageSysPath);
// Delete meta file // Update database
if(existsSync(this.GetPathMetadataFile(userID))) await DatabaseHelper.UpdateRows({
unlinkSync(this.GetPathMetadataFile(userID)); table: USER_TABLE,
where: {
// Delete visiblity file (if any) ID: userID
const visibilityFile = this.GetPathVisibilityFile(userID); },
if(existsSync(visibilityFile)) set: {
unlinkSync(visibilityFile); account_image_path: ""
}
});
} }
} }
@ -69,68 +65,15 @@ export class AccountImageHelper {
* @param level New level for account image * @param level New level for account image
*/ */
public static async SetVisibilityLevel(userID: number, level: AccountImageVisibilityLevel) { public static async SetVisibilityLevel(userID: number, level: AccountImageVisibilityLevel) {
const file = this.GetPathVisibilityFile(userID); await DatabaseHelper.UpdateRows({
table: USER_TABLE,
// If the visiblity is set to everyone, we do not where: {
// need to have a visibility file ID: userID
if(level == AccountImageVisibilityLevel.EVERYONE) { },
set: {
if(existsSync(file)) account_image_visibility: level
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);
}
} }