Skip to content

Commit

Permalink
conf image h w
Browse files Browse the repository at this point in the history
  • Loading branch information
bookpanda committed Jul 18, 2024
1 parent e95be4c commit 64797e7
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 31 deletions.
5 changes: 4 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
APP_PORT=3001
APP_ENV=development
APP_MAX_FILE_SIZE_MB=10
APP_SERVICE_NAME=rpkm67-gateway

IMG_MAX_FILE_SIZE_MB=1
IMG_CROP_WIDTH=500
IMG_CROP_HEIGHT=500

SERVICE_AUTH=localhost:3002
SERVICE_BACKEND=localhost:3003
SERVICE_CHECKIN=localhost:3004
Expand Down
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func main() {

userClient := userProto.NewUserServiceClient(authConn)
userSvc := user.NewService(userClient, objSvc, logger)
userHdr := user.NewHandler(userSvc, conf.App.MaxFileSizeMb, constant.AllowedContentType, validate, logger)
userHdr := user.NewHandler(userSvc, &conf.Img, constant.AllowedContentType, validate, logger)

groupClient := groupProto.NewGroupServiceClient(backendConn)
groupSvc := group.NewService(groupClient, logger)
Expand Down
37 changes: 28 additions & 9 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ import (
)

type AppConfig struct {
Port string
Env string
Port string
Env string
ServiceName string
}

type ImageConfig struct {
MaxFileSizeMb int
ServiceName string
CropWidth int
CropHeight int
}

type ServiceConfig struct {
Expand All @@ -35,6 +40,7 @@ type TracerConfig struct {

type Config struct {
App AppConfig
Img ImageConfig
Svc ServiceConfig
Cors CorsConfig
Db DbConfig
Expand All @@ -49,16 +55,28 @@ func LoadConfig() (*Config, error) {
}
}

maxFileSizeMb, err := strconv.ParseInt(os.Getenv("APP_MAX_FILE_SIZE_MB"), 10, 64)
appConfig := AppConfig{
Port: os.Getenv("APP_PORT"),
Env: os.Getenv("APP_ENV"),
ServiceName: os.Getenv("APP_SERVICE_NAME"),
}

maxFileSizeMb, err := strconv.ParseInt(os.Getenv("IMG_MAX_FILE_SIZE_MB"), 10, 64)
if err != nil {
return nil, err
}

appConfig := AppConfig{
Port: os.Getenv("APP_PORT"),
Env: os.Getenv("APP_ENV"),
cropWidth, err := strconv.ParseInt(os.Getenv("IMG_CROP_WIDTH"), 10, 64)
if err != nil {
return nil, err
}
cropHeight, err := strconv.ParseInt(os.Getenv("IMG_CROP_HEIGHT"), 10, 64)
if err != nil {
return nil, err
}
imageConfig := ImageConfig{
MaxFileSizeMb: int(maxFileSizeMb),
ServiceName: os.Getenv("APP_SERVICE_NAME"),
CropWidth: int(cropWidth),
CropHeight: int(cropHeight),
}

serviceConfig := ServiceConfig{
Expand All @@ -82,6 +100,7 @@ func LoadConfig() (*Config, error) {

return &Config{
App: appConfig,
Img: imageConfig,
Svc: serviceConfig,
Cors: corsConfig,
Db: DbConfig,
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@ require (
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
go.opentelemetry.io/otel/metric v1.28.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
golang.org/x/image v0.18.0 // indirect
golang.org/x/sync v0.7.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 // indirect
)
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down Expand Up @@ -176,8 +174,6 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ=
golang.org/x/image v0.18.0/go.mod h1:4yyo5vMFQjVjUcVk4jEQcU9MGy/rulF5WvUILseCM2E=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
Expand Down
7 changes: 4 additions & 3 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/isd-sgcu/rpkm67-gateway/apperror"
"github.com/isd-sgcu/rpkm67-gateway/config"
"github.com/isd-sgcu/rpkm67-gateway/constant"
"github.com/isd-sgcu/rpkm67-gateway/internal/dto"
"github.com/isd-sgcu/rpkm67-gateway/internal/metrics"
Expand All @@ -23,7 +24,7 @@ type Ctx interface {
Param(key string) string
Query(key string) string
PostForm(key string) string
FormFile(key string, allowedContentType map[string]struct{}, maxFileSize int64) (*dto.DecomposedFile, error)
FormFile(key string, allowedContentType map[string]struct{}, conf *config.ImageConfig) (*dto.DecomposedFile, error)
GetString(key string) string
GetHeader(key string) string
GetTracer() trace.Tracer
Expand Down Expand Up @@ -118,13 +119,13 @@ func (c *contextImpl) PostForm(key string) string {
return c.Context.PostForm(key)
}

func (c *contextImpl) FormFile(key string, allowedContentType map[string]struct{}, maxFileSize int64) (*dto.DecomposedFile, error) {
func (c *contextImpl) FormFile(key string, allowedContentType map[string]struct{}, conf *config.ImageConfig) (*dto.DecomposedFile, error) {
file, err := c.Context.FormFile(key)
if err != nil {
return nil, err
}

data, err := ExtractFile(file, allowedContentType, maxFileSize)
data, err := ExtractFile(file, allowedContentType, conf)
if err != nil {
return nil, err
}
Expand Down
8 changes: 5 additions & 3 deletions internal/context/context.utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@ import (
"image/png"
"mime/multipart"
"strings"

"github.com/isd-sgcu/rpkm67-gateway/config"
)

func ExtractFile(file *multipart.FileHeader, allowedContent map[string]struct{}, maxSize int64) (data []byte, err error) {
func ExtractFile(file *multipart.FileHeader, allowedContent map[string]struct{}, conf *config.ImageConfig) (data []byte, err error) {
format := file.Header["Content-Type"][0]
if !isExisted(allowedContent, format) {
return nil, errors.New("Allowed content type is " + fmt.Sprint(strings.Join(mapToArr(allowedContent), ", ")))
}

resizedImage, err := resizeImage(file, 500, 500)
resizedImage, err := resizeImage(file, conf.CropWidth, conf.CropHeight)
if err != nil {
return nil, err
}

maxSizeMB := maxSize * 1024 * 1024
maxSizeMB := conf.MaxFileSizeMb * 1024 * 1024
fileBytes, err := compressImage(resizedImage, format, int(maxSizeMB))
if err != nil {
return nil, err
Expand Down
9 changes: 5 additions & 4 deletions internal/user/user.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"strings"

"github.com/isd-sgcu/rpkm67-gateway/config"
"github.com/isd-sgcu/rpkm67-gateway/internal/context"
"github.com/isd-sgcu/rpkm67-gateway/internal/dto"
"github.com/isd-sgcu/rpkm67-gateway/internal/validator"
Expand All @@ -18,16 +19,16 @@ type Handler interface {

type handlerImpl struct {
svc Service
maxFileSize int
conf *config.ImageConfig
allowedContentType map[string]struct{}
validate validator.DtoValidator
log *zap.Logger
}

func NewHandler(svc Service, maxFileSize int, allowedContentType map[string]struct{}, validate validator.DtoValidator, log *zap.Logger) Handler {
func NewHandler(svc Service, conf *config.ImageConfig, allowedContentType map[string]struct{}, validate validator.DtoValidator, log *zap.Logger) Handler {
return &handlerImpl{
svc: svc,
maxFileSize: maxFileSize,
conf: conf,
allowedContentType: allowedContentType,
validate: validate,
log: log,
Expand Down Expand Up @@ -141,7 +142,7 @@ func (h *handlerImpl) UpdatePicture(c context.Ctx) {
return
}

file, err := c.FormFile("picture", h.allowedContentType, int64(h.maxFileSize))
file, err := c.FormFile("picture", h.allowedContentType, h.conf)
if err != nil {
h.log.Named("UpdatePicture").Error("FormFile: failed to get file", zap.Error(err))
c.BadRequestError(err.Error())
Expand Down
9 changes: 5 additions & 4 deletions mocks/context/context.mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 64797e7

Please sign in to comment.