From 1aa9ed485e8f72aa99f8ac3cc2792f9b6f112130 Mon Sep 17 00:00:00 2001 From: Junyan Qin <1010553892@qq.com> Date: Sun, 20 Oct 2024 11:12:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=BD=93=E7=A7=AF=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/config/config.go | 2 ++ backend/controller/postapi.go | 18 ++++++++++++++-- backend/migrate/main.go | 1 + backend/migrate/migrations/image_max_size.go | 22 ++++++++++++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 backend/migrate/migrations/image_max_size.go diff --git a/backend/config/config.go b/backend/config/config.go index 2d9b658..59b19f1 100644 --- a/backend/config/config.go +++ b/backend/config/config.go @@ -18,6 +18,8 @@ func SetInitValue() { viper.SetDefault("backend.host", "0.0.0.0") viper.SetDefault("backend.port", "8081") + viper.SetDefault("feature.image_max_size", 1024*1024*2) + // jwt viper.SetDefault("auth.jwt.secret", uuid.New().String()) viper.SetDefault("auth.jwt.expire", 3600*6) diff --git a/backend/controller/postapi.go b/backend/controller/postapi.go index 7f103e9..8059e0e 100644 --- a/backend/controller/postapi.go +++ b/backend/controller/postapi.go @@ -2,12 +2,14 @@ package controller import ( "bytes" + "fmt" "strconv" "github.com/RockChinQ/Campux/backend/database" "github.com/RockChinQ/Campux/backend/service" "github.com/RockChinQ/Campux/backend/util" "github.com/gin-gonic/gin" + "github.com/spf13/viper" ) type PostRouter struct { @@ -51,16 +53,28 @@ func (pr *PostRouter) UploadImage(c *gin.Context) { } // 取body的json里的图片数据 - file, _, err := c.Request.FormFile("image") + file, err := c.FormFile("image") if err != nil { pr.Fail(c, 1, err.Error()) return } + // 检查图片大小 + if file.Size > int64(viper.GetInt("feature.image_max_size")) { + pr.Fail(c, 1, fmt.Sprintf("图片文件过大 (%.2f MB > %.2f MB)", float64(file.Size)/1024/1024, float64(viper.GetInt("feature.image_max_size"))/1024/1024)) + return + } suffix := c.Request.FormValue("suffix") + fileReader, err := file.Open() + + if err != nil { + pr.Fail(c, 1, err.Error()) + return + } + // 上传图片 - key, err := pr.PostService.UploadImage(file, suffix) + key, err := pr.PostService.UploadImage(fileReader, suffix) if err != nil { pr.Fail(c, 1, err.Error()) diff --git a/backend/migrate/main.go b/backend/migrate/main.go index 45fcf07..71b7267 100644 --- a/backend/migrate/main.go +++ b/backend/migrate/main.go @@ -9,6 +9,7 @@ import ( var migrations = []Migration{ &ms.LocalStorageConfig{}, &ms.SQLiteConfig{}, + &ms.ImageMaxSize{}, } // Migration interface diff --git a/backend/migrate/migrations/image_max_size.go b/backend/migrate/migrations/image_max_size.go new file mode 100644 index 0000000..418897d --- /dev/null +++ b/backend/migrate/migrations/image_max_size.go @@ -0,0 +1,22 @@ +package migrations + +import ( + "github.com/RockChinQ/Campux/backend/config" + "github.com/spf13/viper" +) + +type ImageMaxSize struct{} + +func (i *ImageMaxSize) Name() string { + return "ImageMaxSize" +} + +func (i *ImageMaxSize) Check() bool { + return !viper.IsSet("feature.image_max_size") +} + +func (i *ImageMaxSize) Up() error { + viper.Set("feature.image_max_size", 1024*1024*2) + + return config.WriteConfig() +}