From 84a98506dc98da0ce7e44f899cef69c00f7d7c12 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 26 Dec 2019 14:14:42 +0100 Subject: [PATCH] Can change group logo --- src/controllers/GroupsController.ts | 46 ++++++++++++++++++++++++++++- src/controllers/Routes.ts | 4 +++ src/entities/Group.ts | 7 +++++ src/helpers/GroupsHelper.ts | 22 ++++++++++++++ 4 files changed, 78 insertions(+), 1 deletion(-) diff --git a/src/controllers/GroupsController.ts b/src/controllers/GroupsController.ts index c945f03..8c3b066 100644 --- a/src/controllers/GroupsController.ts +++ b/src/controllers/GroupsController.ts @@ -1,5 +1,5 @@ import { RequestHandler } from "../entities/RequestHandler"; -import { GroupsHelper } from "../helpers/GroupsHelper"; +import { GroupsHelper, PATH_GROUPS_LOGOS } from "../helpers/GroupsHelper"; import { GroupsAccessLevel, GroupInfo, GroupVisibilityLevel, GroupPostsCreationLevel, GroupRegistrationLevel } from "../entities/Group"; import { GroupMembershipLevels } from "../entities/GroupMember"; import { time } from "../utils/DateUtils"; @@ -231,6 +231,50 @@ export class GroupsController { h.success("Requested virtual directory seems to be available!"); } + /** + * Upload a new group logo + * + * @param h Request handler + */ + public static async UploadLogo(h: RequestHandler) { + const groupID = await h.postGroupIDWithAccess("id", GroupsAccessLevel.ADMIN_ACCESS); + + if(!h.hasFile("logo")) + h.error(400, "An error occured while receiving logo !"); + + // Delete current logo (if any) + await GroupsHelper.DeleteLogo(groupID); + + // Save the new group logo + const targetFilePath = await h.savePostImage("logo", PATH_GROUPS_LOGOS, 500, 500); + + // Update the settings of the group + const settings = await GroupsHelper.GetInfo(groupID); + settings.logo = targetFilePath; + await GroupsHelper.SetSettings(settings); + + h.send({ + success: "Group logo has been successfully updated!", + url: settings.logoURL + }); + } + + /** + * Delete the current logo of a group + * + * @param h Request handler + */ + public static async DeleteLogo(h: RequestHandler) { + const groupID = await h.postGroupIDWithAccess("id", GroupsAccessLevel.ADMIN_ACCESS); + + await GroupsHelper.DeleteLogo(groupID); + + h.send({ + success: "Group logo has been successfully deleted!", + url: (await GroupsHelper.GetInfo(groupID)).logoURL + }) + } + /** * Turn a GroupInfo object into a valid API object * diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 91203d9..a73f897 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -100,4 +100,8 @@ export const Routes : Route[] = [ {path: "/groups/set_settings", cb: (h) => GroupsController.SetSettings(h)}, {path: "/groups/checkVirtualDirectory", cb: (h) => GroupsController.CheckVirtualDirectory(h)}, + + {path: "/groups/upload_logo", cb: (h) => GroupsController.UploadLogo(h)}, + + {path: "/groups/delete_logo", cb: (h) => GroupsController.DeleteLogo(h)}, ] \ No newline at end of file diff --git a/src/entities/Group.ts b/src/entities/Group.ts index 079aca6..42f7e90 100644 --- a/src/entities/Group.ts +++ b/src/entities/Group.ts @@ -96,6 +96,13 @@ export class GroupInfo implements GroupInfoConstructor { return pathUserData(this.logoPath, false); } + get logoSysPath() : string { + if(!this.hasLogo) + throw Error("The group has no logo!"); + + return pathUserData(this.logoPath, true); + } + get hasDescription() : boolean { return this.description && true; } diff --git a/src/helpers/GroupsHelper.ts b/src/helpers/GroupsHelper.ts index 169c41f..d330d8a 100644 --- a/src/helpers/GroupsHelper.ts +++ b/src/helpers/GroupsHelper.ts @@ -4,6 +4,7 @@ import { GroupMembershipLevels, GroupMember } from "../entities/GroupMember"; import { NewGroup } from "../entities/NewGroup"; import { time } from "../utils/DateUtils"; import { GroupSettings } from "../entities/GroupSettings"; +import { existsSync, unlinkSync } from "fs"; /** * Groups helper @@ -14,6 +15,8 @@ import { GroupSettings } from "../entities/GroupSettings"; const GROUPS_LIST_TABLE = "comunic_groups"; const GROUPS_MEMBERS_TABLE = "comunic_groups_members"; +export const PATH_GROUPS_LOGOS = "groups_logo"; + export class GroupsHelper { @@ -116,6 +119,25 @@ export class GroupsHelper { }); } + /** + * Delete the logo of a group + * + * @param groupID Target group ID + */ + public static async DeleteLogo(groupID: number) { + const currSettings = await this.GetInfo(groupID); + + if(!currSettings.hasLogo) + return; + + if(existsSync(currSettings.logoSysPath)) + unlinkSync(currSettings.logoSysPath); + + currSettings.logo = ""; + + await this.SetSettings(currSettings); + } + /** * Get the visibility level of a group *