diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index a09c827..77b05a4 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -99,6 +99,8 @@ export const Routes : Route[] = [ {path: "/settings/get_account_image", cb: (h) => SettingsController.GetAccountImageSettings(h)}, + {path: "/settings/upload_account_image", cb: (h) => SettingsController.UploadAccountImage(h)}, + // Friends controller {path: "/friends/getList", cb: (h) => FriendsController.GetList(h)}, diff --git a/src/controllers/SettingsController.ts b/src/controllers/SettingsController.ts index 4e63693..cc0e93b 100644 --- a/src/controllers/SettingsController.ts +++ b/src/controllers/SettingsController.ts @@ -11,6 +11,7 @@ import { removeHTMLNodes, checkURL, fixEncoding } from "../utils/StringUtils"; import { checkVirtualDirectoryAvailability, VirtualDirType } from "../utils/VirtualDirsUtils"; import { AccountHelper } from "../helpers/AccountHelper"; import { AccountImageVisibilityLevel } from "../entities/AccountImage"; +import { AccountImageHelper } from "../helpers/AccountImageHelper"; /** * API account image visibility levels @@ -215,4 +216,20 @@ export class SettingsController { 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(); + } } \ No newline at end of file diff --git a/src/entities/AccountImage.ts b/src/entities/AccountImage.ts index c7cd819..1bf6744 100644 --- a/src/entities/AccountImage.ts +++ b/src/entities/AccountImage.ts @@ -28,9 +28,19 @@ export class AccountImage { */ get url() : string { 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 { - return this.urlForFile(errorAccountImage); + return this.pathForFile(errorAccountImage); } - private static urlForFile(file : string) : string { - return pathUserData("avatars/" + file, false); + private static pathForFile(file : string, sysPath = false) : string { + return pathUserData("avatars/" + file, sysPath); } } \ No newline at end of file diff --git a/src/helpers/AccountImageHelper.ts b/src/helpers/AccountImageHelper.ts index ef59794..252e5f3 100644 --- a/src/helpers/AccountImageHelper.ts +++ b/src/helpers/AccountImageHelper.ts @@ -1,5 +1,5 @@ import { pathUserData } from "../utils/UserDataUtils"; -import { existsSync, readFileSync } from "fs"; +import { existsSync, readFileSync, unlinkSync, writeFileSync } from "fs"; import { AccountImage, AccountImageVisibilityLevel } from "../entities/AccountImage"; /** @@ -24,6 +24,22 @@ export class AccountImageHelper { 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 *