From 7ae67826e4f722ef367dd1ba275a1ab75dcc08e8 Mon Sep 17 00:00:00 2001 From: Havoc412 <2993167370@qq.com> Date: Thu, 24 Oct 2024 22:08:43 +0800 Subject: [PATCH] change upload file --- app/http/controller/web/upload_controller.go | 7 ++----- .../validator/common/upload_files/upload_files.go | 2 +- app/model/animal.go | 2 +- app/service/upload_file/upload_file.go | 11 ++++++----- test/time_test.go | 14 ++++++++++++++ 5 files changed, 24 insertions(+), 12 deletions(-) create mode 100644 test/time_test.go diff --git a/app/http/controller/web/upload_controller.go b/app/http/controller/web/upload_controller.go index 53a039a..c5bbd54 100644 --- a/app/http/controller/web/upload_controller.go +++ b/app/http/controller/web/upload_controller.go @@ -5,6 +5,7 @@ import ( "catface/app/global/variable" "catface/app/service/upload_file" "catface/app/utils/response" + "path/filepath" "github.com/gin-gonic/gin" ) @@ -18,12 +19,8 @@ type Upload struct { func (u *Upload) StartUpload(context *gin.Context) { // TODO 如果之后要存储到 Linux 服务器上特殊路径下,就需要修改这里。 dir_name := context.GetString(consts.ValidatorPrefix + "dir_name") - // 检查 dir_name 是否以 / 结尾,如果不是则补充一个 / - if len(dir_name) > 0 && dir_name[len(dir_name)-1] != '/' { - dir_name += "/" - } + savePath := filepath.Join(variable.BasePath, variable.ConfigYml.GetString("FileUploadSetting.UploadFileSavePath"), dir_name) - savePath := variable.BasePath + variable.ConfigYml.GetString("FileUploadSetting.UploadFileSavePath") + dir_name if r, finnalSavePath := upload_file.Upload(context, savePath); r == true { response.Success(context, consts.CurdStatusOkMsg, finnalSavePath) } else { diff --git a/app/http/validator/common/upload_files/upload_files.go b/app/http/validator/common/upload_files/upload_files.go index 20b63b6..cdb4411 100644 --- a/app/http/validator/common/upload_files/upload_files.go +++ b/app/http/validator/common/upload_files/upload_files.go @@ -27,7 +27,7 @@ func (u UpFiles) CheckParams(context *gin.Context) { // 该函数主要是将本结构体的字段(成员)按照 consts.ValidatorPrefix+ json标签对应的 键 => 值 形式绑定在上下文,便于下一步(控制器)可以直接通过 context.Get(键) 获取相关值 extraAddBindDataContext := data_transfer.DataAddContext(u, consts.ValidatorPrefix, context) if extraAddBindDataContext == nil { - response.ErrorSystem(context, "animialList表单验证器json化失败", "") + response.ErrorSystem(context, "upload Files 表单验证器json化失败", "") return } diff --git a/app/model/animal.go b/app/model/animal.go index 9da7d4d..a640608 100644 --- a/app/model/animal.go +++ b/app/model/animal.go @@ -29,7 +29,7 @@ type Animal struct { AvatarHeight uint16 `json:"avatar_height,omitempty"` // 为了方便前端在加载图像前的骨架图 & 瀑布流展示。 // INFO 暂时没用到 AvatarWidth uint16 `json:"avatar_width,omitempty"` // 为了方便前端在加载图像前的骨架图 & 瀑布流展示。 HeadImg string `gorm:"type:varchar(10)" json:"head_img,omitempty"` // Head 默认处理为正方形。 - Photos string `gorm:"type:varchar(100)" json:"photos,omitempty"` // 图片数组 + Photos string `gorm:"type:varchar(255)" json:"photos,omitempty"` // 图片数组 // TAG POI Latitude float64 `json:"latitude,omitempty"` // POI 位置相关 Longitude float64 `json:"longitude,omitempty"` // POI 位置相关 diff --git a/app/service/upload_file/upload_file.go b/app/service/upload_file/upload_file.go index f6c939f..f1572fb 100644 --- a/app/service/upload_file/upload_file.go +++ b/app/service/upload_file/upload_file.go @@ -8,6 +8,7 @@ import ( "fmt" "os" "path" + "path/filepath" "strings" "time" @@ -27,10 +28,10 @@ func Upload(context *gin.Context, savePath string) (r bool, finnalSavePath inter saveFileName := fmt.Sprintf("%d%s", sequence, file.Filename) saveFileName = md5_encrypt.MD5(saveFileName) + path.Ext(saveFileName) - if saveErr = context.SaveUploadedFile(file, newSavePath+saveFileName); saveErr == nil { + if saveErr = context.SaveUploadedFile(file, filepath.Join(newSavePath, saveFileName)); saveErr == nil { // 上传成功,返回资源的相对路径,这里请根据实际返回绝对路径或者相对路径 finnalSavePath = gin.H{ - "path": strings.ReplaceAll(newReturnPath+saveFileName, variable.BasePath, ""), + "path": strings.ReplaceAll(filepath.Join(newReturnPath, saveFileName), variable.BasePath, ""), } return true, finnalSavePath } @@ -44,9 +45,9 @@ func Upload(context *gin.Context, savePath string) (r bool, finnalSavePath inter // 文件上传可以设置按照 xxx年-xx月 格式存储 func generateYearMonthPath(savePathPre string) (string, string) { returnPath := variable.BasePath + variable.ConfigYml.GetString("FileUploadSetting.UploadFileReturnPath") - curYearMonth := time.Now().Format("2004_04") - newSavePathPre := savePathPre + curYearMonth - newReturnPathPre := returnPath + curYearMonth + curYearMonth := time.Now().In(time.Local).Format("2006_01") + newSavePathPre := filepath.Join(savePathPre, curYearMonth) + newReturnPathPre := filepath.Join(returnPath, curYearMonth) // 相关路径不存在,创建目录 if _, err := os.Stat(newSavePathPre); err != nil { if err = os.MkdirAll(newSavePathPre, os.ModePerm); err != nil { diff --git a/test/time_test.go b/test/time_test.go new file mode 100644 index 0000000..1db0401 --- /dev/null +++ b/test/time_test.go @@ -0,0 +1,14 @@ +package test + +import ( + "fmt" + "testing" + "time" +) + +func TestTimeNow(t *testing.T) { + now := time.Now() + curYearMonth := now.In(time.Local).Format("2006_01") + + fmt.Println(now, curYearMonth) +}