change upload file
This commit is contained in:
parent
f2303e206c
commit
7ae67826e4
@ -5,6 +5,7 @@ import (
|
|||||||
"catface/app/global/variable"
|
"catface/app/global/variable"
|
||||||
"catface/app/service/upload_file"
|
"catface/app/service/upload_file"
|
||||||
"catface/app/utils/response"
|
"catface/app/utils/response"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
@ -18,12 +19,8 @@ type Upload struct {
|
|||||||
func (u *Upload) StartUpload(context *gin.Context) {
|
func (u *Upload) StartUpload(context *gin.Context) {
|
||||||
// TODO 如果之后要存储到 Linux 服务器上特殊路径下,就需要修改这里。
|
// TODO 如果之后要存储到 Linux 服务器上特殊路径下,就需要修改这里。
|
||||||
dir_name := context.GetString(consts.ValidatorPrefix + "dir_name")
|
dir_name := context.GetString(consts.ValidatorPrefix + "dir_name")
|
||||||
// 检查 dir_name 是否以 / 结尾,如果不是则补充一个 /
|
savePath := filepath.Join(variable.BasePath, variable.ConfigYml.GetString("FileUploadSetting.UploadFileSavePath"), dir_name)
|
||||||
if len(dir_name) > 0 && dir_name[len(dir_name)-1] != '/' {
|
|
||||||
dir_name += "/"
|
|
||||||
}
|
|
||||||
|
|
||||||
savePath := variable.BasePath + variable.ConfigYml.GetString("FileUploadSetting.UploadFileSavePath") + dir_name
|
|
||||||
if r, finnalSavePath := upload_file.Upload(context, savePath); r == true {
|
if r, finnalSavePath := upload_file.Upload(context, savePath); r == true {
|
||||||
response.Success(context, consts.CurdStatusOkMsg, finnalSavePath)
|
response.Success(context, consts.CurdStatusOkMsg, finnalSavePath)
|
||||||
} else {
|
} else {
|
||||||
|
@ -27,7 +27,7 @@ func (u UpFiles) CheckParams(context *gin.Context) {
|
|||||||
// 该函数主要是将本结构体的字段(成员)按照 consts.ValidatorPrefix+ json标签对应的 键 => 值 形式绑定在上下文,便于下一步(控制器)可以直接通过 context.Get(键) 获取相关值
|
// 该函数主要是将本结构体的字段(成员)按照 consts.ValidatorPrefix+ json标签对应的 键 => 值 形式绑定在上下文,便于下一步(控制器)可以直接通过 context.Get(键) 获取相关值
|
||||||
extraAddBindDataContext := data_transfer.DataAddContext(u, consts.ValidatorPrefix, context)
|
extraAddBindDataContext := data_transfer.DataAddContext(u, consts.ValidatorPrefix, context)
|
||||||
if extraAddBindDataContext == nil {
|
if extraAddBindDataContext == nil {
|
||||||
response.ErrorSystem(context, "animialList表单验证器json化失败", "")
|
response.ErrorSystem(context, "upload Files 表单验证器json化失败", "")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ type Animal struct {
|
|||||||
AvatarHeight uint16 `json:"avatar_height,omitempty"` // 为了方便前端在加载图像前的骨架图 & 瀑布流展示。 // INFO 暂时没用到
|
AvatarHeight uint16 `json:"avatar_height,omitempty"` // 为了方便前端在加载图像前的骨架图 & 瀑布流展示。 // INFO 暂时没用到
|
||||||
AvatarWidth uint16 `json:"avatar_width,omitempty"` // 为了方便前端在加载图像前的骨架图 & 瀑布流展示。
|
AvatarWidth uint16 `json:"avatar_width,omitempty"` // 为了方便前端在加载图像前的骨架图 & 瀑布流展示。
|
||||||
HeadImg string `gorm:"type:varchar(10)" json:"head_img,omitempty"` // Head 默认处理为正方形。
|
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
|
// TAG POI
|
||||||
Latitude float64 `json:"latitude,omitempty"` // POI 位置相关
|
Latitude float64 `json:"latitude,omitempty"` // POI 位置相关
|
||||||
Longitude float64 `json:"longitude,omitempty"` // POI 位置相关
|
Longitude float64 `json:"longitude,omitempty"` // POI 位置相关
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"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 := fmt.Sprintf("%d%s", sequence, file.Filename)
|
||||||
saveFileName = md5_encrypt.MD5(saveFileName) + path.Ext(saveFileName)
|
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{
|
finnalSavePath = gin.H{
|
||||||
"path": strings.ReplaceAll(newReturnPath+saveFileName, variable.BasePath, ""),
|
"path": strings.ReplaceAll(filepath.Join(newReturnPath, saveFileName), variable.BasePath, ""),
|
||||||
}
|
}
|
||||||
return true, finnalSavePath
|
return true, finnalSavePath
|
||||||
}
|
}
|
||||||
@ -44,9 +45,9 @@ func Upload(context *gin.Context, savePath string) (r bool, finnalSavePath inter
|
|||||||
// 文件上传可以设置按照 xxx年-xx月 格式存储
|
// 文件上传可以设置按照 xxx年-xx月 格式存储
|
||||||
func generateYearMonthPath(savePathPre string) (string, string) {
|
func generateYearMonthPath(savePathPre string) (string, string) {
|
||||||
returnPath := variable.BasePath + variable.ConfigYml.GetString("FileUploadSetting.UploadFileReturnPath")
|
returnPath := variable.BasePath + variable.ConfigYml.GetString("FileUploadSetting.UploadFileReturnPath")
|
||||||
curYearMonth := time.Now().Format("2004_04")
|
curYearMonth := time.Now().In(time.Local).Format("2006_01")
|
||||||
newSavePathPre := savePathPre + curYearMonth
|
newSavePathPre := filepath.Join(savePathPre, curYearMonth)
|
||||||
newReturnPathPre := returnPath + curYearMonth
|
newReturnPathPre := filepath.Join(returnPath, curYearMonth)
|
||||||
// 相关路径不存在,创建目录
|
// 相关路径不存在,创建目录
|
||||||
if _, err := os.Stat(newSavePathPre); err != nil {
|
if _, err := os.Stat(newSavePathPre); err != nil {
|
||||||
if err = os.MkdirAll(newSavePathPre, os.ModePerm); err != nil {
|
if err = os.MkdirAll(newSavePathPre, os.ModePerm); err != nil {
|
||||||
|
14
test/time_test.go
Normal file
14
test/time_test.go
Normal file
@ -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)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user