From ade9fe7c3e7cafa42a4b4a723f7df166c7e96bc0 Mon Sep 17 00:00:00 2001 From: alex Date: Sun, 19 Feb 2023 07:28:37 +0100 Subject: [PATCH] added file for definitions --- modules/image/image.go | 27 +++++---------------------- modules/structs/user.go | 9 --------- modules/utils/definitions.go | 22 ++++++++++++++++++++++ routers/api/v1/user/useravatar.go | 14 +++++++------- 4 files changed, 34 insertions(+), 38 deletions(-) create mode 100644 modules/utils/definitions.go diff --git a/modules/image/image.go b/modules/image/image.go index 5aff604..7833417 100644 --- a/modules/image/image.go +++ b/modules/image/image.go @@ -12,25 +12,8 @@ import ( "github.com/h2non/bimg" ) -// is specified in the form data of the frontend -const FormFileKey = "file" - -const MaxAvatarSize = 2 * 1024 * 1024 // 2 MB - -// filename len = 36 + dot . 1 + longestFileType 4 -// example: cf75ace7-da4c-434d-bf7d-0bdbcea5c57d.webp -const MaxFileNameLen = 41 - -var validFileTypes = []string{"jpeg", "jpg", "png", "webp"} - -// represents the query parameter size for the dynamic image resolution -// example ?size=1 -> 64x64 -var ImageSizes = []int{32, 64, 150, 256, 512, 1024} - -const DefaultImageSize = 4 - func FileTypeVerification(fileType string) bool { - for _, validFileType := range validFileTypes { + for _, validFileType := range utils.ValidFileTypes { if fileType == "image/"+validFileType { return true } @@ -132,7 +115,7 @@ func GetImage(userId string, fileName string, imageSize string) ([]byte, error) imgSize, err := strconv.Atoi(imageSize) if err != nil { - imgSize = DefaultImageSize + imgSize = utils.DefaultImageSize } buffer, err := bimg.Read(utils.GetUserStoragePath(userId) + fileName) @@ -143,11 +126,11 @@ func GetImage(userId string, fileName string, imageSize string) ([]byte, error) } // check that the value has not been manipulated and is greater than the largest index value in the list - if imgSize > len(ImageSizes)-1 { - imgSize = DefaultImageSize + if imgSize > len(utils.ImageSizes)-1 { + imgSize = utils.DefaultImageSize } - resolution := ImageSizes[imgSize] + resolution := utils.ImageSizes[imgSize] newImage, err := bimg.NewImage(buffer).ForceResize(resolution, resolution) diff --git a/modules/structs/user.go b/modules/structs/user.go index d43ffe2..ae9c2f0 100644 --- a/modules/structs/user.go +++ b/modules/structs/user.go @@ -1,14 +1,5 @@ package structs -const ( - LenUserId = 36 - LenXAuthorizationToken = 36 -) - -type UserPrivacy struct { - Test uint8 -} - // swagger:model UpdatedUserAvatarResponse type UpdatedUserAvatarResponse struct { AvatarUrl string diff --git a/modules/utils/definitions.go b/modules/utils/definitions.go new file mode 100644 index 0000000..32315c2 --- /dev/null +++ b/modules/utils/definitions.go @@ -0,0 +1,22 @@ +package utils + +const ( + // is specified in the form data of the frontend + FormFileKey = "file" + + MaxAvatarSize = 2 * 1024 * 1024 // 2 MB + + // filename len = 36 + dot . 1 + longestFileType 4 + // example: cf75ace7-da4c-434d-bf7d-0bdbcea5c57d.webp + MaxFileNameLen = 41 + + DefaultImageSize = 4 +) + +var ( + ValidFileTypes = []string{"jpeg", "jpg", "png", "webp"} + + // represents the query parameter size for the dynamic image resolution + // example ?size=1 -> 64x64 + ImageSizes = []int{32, 64, 150, 256, 512, 1024} +) diff --git a/routers/api/v1/user/useravatar.go b/routers/api/v1/user/useravatar.go index e42b442..ed84ca3 100644 --- a/routers/api/v1/user/useravatar.go +++ b/routers/api/v1/user/useravatar.go @@ -34,7 +34,7 @@ func GetAvatar(c *fiber.Ctx) error { userId := c.Params("userId") - if len(userId) != structs.LenUserId { + if len(userId) != cnjglobals.LenUserId { gocnjhelper.LogDebug("UserId is longer than allowed") return c.SendStatus(fiber.StatusUnprocessableEntity) } @@ -42,7 +42,7 @@ func GetAvatar(c *fiber.Ctx) error { fileName := c.Params("fileName") // check if the filename length has not been manipulated by the user - if len(fileName) > image.MaxFileNameLen { + if len(fileName) > utils.MaxFileNameLen { gocnjhelper.LogDebug("Filename is longer than allowed") return c.SendStatus(fiber.StatusUnprocessableEntity) } @@ -95,7 +95,7 @@ func UpdateAvatar(c *fiber.Ctx) error { xAuthorization := cnjglobals.GetAuhorizationToken(c) - if len(xAuthorization) != structs.LenXAuthorizationToken { + if len(xAuthorization) != cnjglobals.LenXAuthorizationHeader { return c.SendStatus(fiber.StatusUnauthorized) } @@ -108,14 +108,14 @@ func UpdateAvatar(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusUnauthorized) } - file, err := c.FormFile(image.FormFileKey) + file, err := c.FormFile(utils.FormFileKey) if err != nil { gocnjhelper.LogErrorf("Failed to get form file, err: %s", err) return c.SendStatus(fiber.StatusBadRequest) } - if file.Size > image.MaxAvatarSize { + if file.Size > utils.MaxAvatarSize { return c.SendStatus(fiber.StatusRequestEntityTooLarge) } @@ -133,8 +133,8 @@ func UpdateAvatar(c *fiber.Ctx) error { file, foundSession.UserId, utils.GetUserStoragePath(foundSession.UserId)+fileName, - image.ImageSizes[image.DefaultImageSize], - image.ImageSizes[image.DefaultImageSize], + utils.ImageSizes[utils.DefaultImageSize], + utils.ImageSizes[utils.DefaultImageSize], 40) if err != nil {