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

Can replace account image

This commit is contained in:
Pierre HUBERT 2020-03-22 15:00:08 +01:00
parent d02eb7afce
commit 34b0a29e77
4 changed files with 51 additions and 6 deletions

View File

@ -99,6 +99,8 @@ export const Routes : Route[] = [
{path: "/settings/get_account_image", cb: (h) => SettingsController.GetAccountImageSettings(h)}, {path: "/settings/get_account_image", cb: (h) => SettingsController.GetAccountImageSettings(h)},
{path: "/settings/upload_account_image", cb: (h) => SettingsController.UploadAccountImage(h)},
// Friends controller // Friends controller
{path: "/friends/getList", cb: (h) => FriendsController.GetList(h)}, {path: "/friends/getList", cb: (h) => FriendsController.GetList(h)},

View File

@ -11,6 +11,7 @@ import { removeHTMLNodes, checkURL, fixEncoding } from "../utils/StringUtils";
import { checkVirtualDirectoryAvailability, VirtualDirType } from "../utils/VirtualDirsUtils"; import { checkVirtualDirectoryAvailability, VirtualDirType } from "../utils/VirtualDirsUtils";
import { AccountHelper } from "../helpers/AccountHelper"; import { AccountHelper } from "../helpers/AccountHelper";
import { AccountImageVisibilityLevel } from "../entities/AccountImage"; import { AccountImageVisibilityLevel } from "../entities/AccountImage";
import { AccountImageHelper } from "../helpers/AccountImageHelper";
/** /**
* API account image visibility levels * API account image visibility levels
@ -215,4 +216,20 @@ export class SettingsController {
visibility: ACCOUNT_IMAGE_VISIBLITY_LEVELS[imageInfo.level] visibility: ACCOUNT_IMAGE_VISIBLITY_LEVELS[imageInfo.level]
}); });
} }
/**
* Upload new account image
*
* @param h Request handler
*/
public static async UploadAccountImage(h: RequestHandler) {
if(!h.hasFile("picture"))
h.error(400, "An error occured while receiving the image !")
const uri = await h.savePostImage("picture", "avatars", 800, 800);
await AccountImageHelper.Set(h.getUserId(), uri);
h.success();
}
} }

View File

@ -28,9 +28,19 @@ export class AccountImage {
*/ */
get url() : string { get url() : string {
if(this.path.length < 1) if(this.path.length < 1)
return AccountImage.urlForFile(defaultAccountImage); return AccountImage.pathForFile(defaultAccountImage);
return AccountImage.urlForFile(this.path); return AccountImage.pathForFile(this.path);
}
/**
* Get account image sys path
*/
get sysPath() : string {
if(this.path.length < 1)
throw new Error("This user has no account image!");
return AccountImage.pathForFile(this.path, true);
} }
/** /**
@ -42,10 +52,10 @@ export class AccountImage {
} }
static get errorURL() : string { static get errorURL() : string {
return this.urlForFile(errorAccountImage); return this.pathForFile(errorAccountImage);
} }
private static urlForFile(file : string) : string { private static pathForFile(file : string, sysPath = false) : string {
return pathUserData("avatars/" + file, false); return pathUserData("avatars/" + file, sysPath);
} }
} }

View File

@ -1,5 +1,5 @@
import { pathUserData } from "../utils/UserDataUtils"; import { pathUserData } from "../utils/UserDataUtils";
import { existsSync, readFileSync } from "fs"; import { existsSync, readFileSync, unlinkSync, writeFileSync } from "fs";
import { AccountImage, AccountImageVisibilityLevel } from "../entities/AccountImage"; import { AccountImage, AccountImageVisibilityLevel } from "../entities/AccountImage";
/** /**
@ -24,6 +24,22 @@ export class AccountImageHelper {
return new AccountImage(userID, accountImageFileContent, level); 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/", ""));
}
/** /**
* Get the content of the file associated to the user account image, if any * Get the content of the file associated to the user account image, if any
* *