diff --git a/constant/user.constant.go b/constant/user.constant.go index 7a9fcd8..3ce9a28 100644 --- a/constant/user.constant.go +++ b/constant/user.constant.go @@ -4,6 +4,4 @@ var AllowedContentType = map[string]struct{}{ "image/jpeg": {}, "image/jpg": {}, "image/png": {}, - "image/gif": {}, - "image/webp": {}, } diff --git a/go.sum b/go.sum index 848e811..3e4b128 100644 --- a/go.sum +++ b/go.sum @@ -61,12 +61,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 h1:bkypFPDjIYGfCYD5mRBvpqxfYX1YCS1PXdKYWi8FsN0= github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0/go.mod h1:P+Lt/0by1T8bfcF3z737NnSbmxQAppXMRziHUxPOC8k= -github.com/isd-sgcu/rpkm67-go-proto v0.4.6 h1:yUBzUY3ftBfnI2x/MT+MsynBOlV5pOR143c9OTrVGuU= -github.com/isd-sgcu/rpkm67-go-proto v0.4.6/go.mod h1:w+UCeQnJ3wBuJ7Tyf8LiBiPZVb1KlecjMNCB7kBeL7M= github.com/isd-sgcu/rpkm67-go-proto v0.4.8 h1:tU6nCv4A34guBoDwkZvUzzs6z43NBzgLsSbGmX5QRYI= github.com/isd-sgcu/rpkm67-go-proto v0.4.8/go.mod h1:w+UCeQnJ3wBuJ7Tyf8LiBiPZVb1KlecjMNCB7kBeL7M= -github.com/isd-sgcu/rpkm67-model v0.0.6 h1:pYlqOmeXGQIfHdOhyAta4kXkqnoLc4X3KWcAjPrAuds= -github.com/isd-sgcu/rpkm67-model v0.0.6/go.mod h1:dxgLSkrFpbQOXsrzqgepZoEOyZUIG2LBGtm5gsuBbVc= github.com/isd-sgcu/rpkm67-model v0.0.7 h1:3b8gf1Ocg+Ky4xocKtCqVCB3rFDg90IgEXRwNmHt0OE= github.com/isd-sgcu/rpkm67-model v0.0.7/go.mod h1:dxgLSkrFpbQOXsrzqgepZoEOyZUIG2LBGtm5gsuBbVc= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= diff --git a/internal/context/context.utils.go b/internal/context/context.utils.go index 0ab6ded..540b6c9 100644 --- a/internal/context/context.utils.go +++ b/internal/context/context.utils.go @@ -6,6 +6,8 @@ import ( "fmt" "image" "image/jpeg" + + "image/png" "mime/multipart" "strings" ) @@ -21,8 +23,6 @@ func ExtractFile(file *multipart.FileHeader, allowedContent map[string]struct{}, return nil, err } - println("File size: ", len(fileBytes)) - return fileBytes, nil } @@ -40,6 +40,7 @@ func mapToArr(m map[string]struct{}) []string { } func compressImage(fileHeader *multipart.FileHeader, maxSizeMB int) ([]byte, error) { + fileExt := fileHeader.Header["Content-Type"][0] file, err := fileHeader.Open() if err != nil { return nil, err @@ -56,11 +57,20 @@ func compressImage(fileHeader *multipart.FileHeader, maxSizeMB int) ([]byte, err for { buf := new(bytes.Buffer) - if err := jpeg.Encode(buf, img, &jpeg.Options{Quality: quality}); err != nil { + + switch fileExt { + case "image/jpeg", "image/jpg": + err = jpeg.Encode(buf, img, &jpeg.Options{Quality: quality}) + case "image/png": + err = png.Encode(buf, img) + default: + return nil, fmt.Errorf("unsupported file type: %v", fileExt) + } + if err != nil { return nil, err } - compressed = buf.Bytes() + compressed = buf.Bytes() if len(compressed) <= maxSizeMB { break } @@ -74,3 +84,29 @@ func compressImage(fileHeader *multipart.FileHeader, maxSizeMB int) ([]byte, err return compressed, nil } + +// func resizeImage(img image.Image, width, height int) *image.RGBA { +// bounds := img.Bounds() + +// if width == 0 && height == 0 { +// return nil +// } + +// if width == 0 { +// width = bounds.Dx() * height / bounds.Dy() +// } +// if height == 0 { +// height = bounds.Dy() * width / bounds.Dx() +// } + +// if width > 500 || height > 500 { +// scaleFactor := float64(500) / math.Max(float64(width), float64(height)) +// width = int(float64(width) * scaleFactor) +// height = int(float64(height) * scaleFactor) +// } + +// newImg := image.NewRGBA(image.Rect(0, 0, width, height)) +// draw.CatmullRom.Scale(newImg, newImg.Bounds(), img, bounds, draw.Over, nil) + +// return newImg +// }