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

Can change group logo

This commit is contained in:
Pierre HUBERT 2019-12-26 14:14:42 +01:00
parent 6be2cda9fb
commit 84a98506dc
4 changed files with 78 additions and 1 deletions

View File

@ -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
*

View File

@ -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)},
]

View File

@ -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;
}

View File

@ -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
*