added file for definitions
parent
a48a902823
commit
ade9fe7c3e
|
@ -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)
|
||||
|
||||
|
|
|
@ -1,14 +1,5 @@
|
|||
package structs
|
||||
|
||||
const (
|
||||
LenUserId = 36
|
||||
LenXAuthorizationToken = 36
|
||||
)
|
||||
|
||||
type UserPrivacy struct {
|
||||
Test uint8
|
||||
}
|
||||
|
||||
// swagger:model UpdatedUserAvatarResponse
|
||||
type UpdatedUserAvatarResponse struct {
|
||||
AvatarUrl string
|
||||
|
|
|
@ -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}
|
||||
)
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue