v1.0.0
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"sun-panel/api/api_v1/common/apiReturn"
|
||||
"sun-panel/api/api_v1/common/base"
|
||||
"sun-panel/global"
|
||||
"sun-panel/lib/cmn"
|
||||
"sun-panel/models"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type FileApi struct{}
|
||||
|
||||
func (a *FileApi) UploadImg(c *gin.Context) {
|
||||
userInfo, _ := base.GetCurrentUserInfo(c)
|
||||
configUpload := global.Config.GetValueString("base", "source_path")
|
||||
f, err := c.FormFile("imgfile")
|
||||
if err != nil {
|
||||
apiReturn.Error(c, "上传失败")
|
||||
return
|
||||
} else {
|
||||
fileExt := strings.ToLower(path.Ext(f.Filename))
|
||||
if fileExt != ".png" && fileExt != ".jpg" && fileExt != ".gif" && fileExt != ".jpeg" && fileExt != ".webp" && fileExt != ".svg" {
|
||||
apiReturn.Error(c, "上传失败!只允许png,jpg,gif,jpeg,svg文件")
|
||||
return
|
||||
}
|
||||
fileName := cmn.Md5(fmt.Sprintf("%s%s", f.Filename, time.Now().String()))
|
||||
fildDir := fmt.Sprintf("%s/%d/%d/%d/", configUpload, time.Now().Year(), time.Now().Month(), time.Now().Day())
|
||||
isExist, _ := cmn.PathExists(fildDir)
|
||||
if !isExist {
|
||||
os.MkdirAll(fildDir, os.ModePerm)
|
||||
}
|
||||
filepath := fmt.Sprintf("%s%s%s", fildDir, fileName, fileExt)
|
||||
c.SaveUploadedFile(f, filepath)
|
||||
|
||||
// 像数据库添加记录
|
||||
mFile := models.File{}
|
||||
mFile.AddFile(userInfo.ID, f.Filename, fileExt, filepath)
|
||||
apiReturn.SuccessData(c, gin.H{
|
||||
"imageUrl": filepath[1:],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (a *FileApi) UploadFiles(c *gin.Context) {
|
||||
userInfo, _ := base.GetCurrentUserInfo(c)
|
||||
configUpload := global.Config.GetValueString("base", "source_path")
|
||||
|
||||
form, err := c.MultipartForm()
|
||||
if err != nil {
|
||||
apiReturn.Error(c, "上传失败")
|
||||
return
|
||||
}
|
||||
files := form.File["files[]"]
|
||||
errFiles := []string{}
|
||||
succMap := map[string]string{}
|
||||
for _, f := range files {
|
||||
fileExt := strings.ToLower(path.Ext(f.Filename))
|
||||
fileName := cmn.Md5(fmt.Sprintf("%s%s", f.Filename, time.Now().String()))
|
||||
fildDir := fmt.Sprintf("%s/%d/%d/%d/", configUpload, time.Now().Year(), time.Now().Month(), time.Now().Day())
|
||||
isExist, _ := cmn.PathExists(fildDir)
|
||||
if !isExist {
|
||||
os.MkdirAll(fildDir, os.ModePerm)
|
||||
}
|
||||
filepath := fmt.Sprintf("%s%s%s", fildDir, fileName, fileExt)
|
||||
if c.SaveUploadedFile(f, filepath) != nil {
|
||||
errFiles = append(errFiles, f.Filename)
|
||||
} else {
|
||||
// 成功
|
||||
// 像数据库添加记录
|
||||
mFile := models.File{}
|
||||
mFile.AddFile(userInfo.ID, f.Filename, fileExt, filepath)
|
||||
succMap[f.Filename] = filepath[1:]
|
||||
}
|
||||
}
|
||||
|
||||
apiReturn.SuccessData(c, gin.H{
|
||||
"succMap": succMap,
|
||||
"errFiles": errFiles,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user