242 lines
5.5 KiB
Go
242 lines
5.5 KiB
Go
package controller
|
||
|
||
import (
|
||
"fmt"
|
||
"github.com/gin-gonic/gin"
|
||
"kefu/common"
|
||
"kefu/lib"
|
||
"kefu/models"
|
||
"kefu/tools"
|
||
"kefu/types"
|
||
"log"
|
||
"os"
|
||
"path"
|
||
"strconv"
|
||
"strings"
|
||
"time"
|
||
)
|
||
|
||
// 上传附件返回后缀
|
||
func UploadFileV2(c *gin.Context) {
|
||
SendAttachment, err := strconv.ParseBool(models.FindConfig("SendAttachment"))
|
||
if !SendAttachment || err != nil {
|
||
c.JSON(200, gin.H{
|
||
"code": types.ApiCode.UPLOAD_FORBIDDEN,
|
||
"msg": types.ApiCode.GetMessage(types.ApiCode.UPLOAD_FORBIDDEN),
|
||
})
|
||
return
|
||
}
|
||
f, err := c.FormFile("realfile")
|
||
if err != nil {
|
||
c.JSON(200, gin.H{
|
||
"code": types.ApiCode.FAILED,
|
||
"msg": types.ApiCode.GetMessage(types.ApiCode.FAILED),
|
||
"result": err.Error(),
|
||
})
|
||
return
|
||
}
|
||
//限制上传大小
|
||
maxSize := 20 * 1024 * 1024
|
||
uploadMaxSize := models.FindConfig("UploadMaxSize")
|
||
if uploadMaxSize != "" {
|
||
uploadMaxSizeInt, _ := strconv.Atoi(uploadMaxSize)
|
||
maxSize = uploadMaxSizeInt * 1024 * 1024
|
||
}
|
||
if f.Size >= int64(maxSize) {
|
||
c.JSON(200, gin.H{
|
||
"code": types.ApiCode.UPLOAD_OVERSIZE,
|
||
"msg": types.ApiCode.GetMessage(types.ApiCode.UPLOAD_OVERSIZE),
|
||
})
|
||
return
|
||
}
|
||
|
||
fileName := tools.Md5(fmt.Sprintf("%s%s", f.Filename, time.Now().String()))
|
||
fildDir := fmt.Sprintf("%s%d%s/", common.Upload, time.Now().Year(), time.Now().Month().String())
|
||
isExist, _ := tools.IsFileExist(fildDir)
|
||
if !isExist {
|
||
os.Mkdir(fildDir, os.ModePerm)
|
||
}
|
||
|
||
fileExt := strings.ToLower(path.Ext(f.Filename))
|
||
filepath := fmt.Sprintf("%s%s%s", fildDir, fileName, fileExt)
|
||
c.SaveUploadedFile(f, filepath)
|
||
path := "/" + filepath
|
||
//上传到阿里云oss
|
||
oss, err := lib.NewOssLib()
|
||
if err == nil {
|
||
dstUrl, err := oss.Upload(filepath, filepath)
|
||
if err == nil {
|
||
path = dstUrl
|
||
}
|
||
}
|
||
c.JSON(200, gin.H{
|
||
"code": 200,
|
||
"msg": "上传成功!",
|
||
"result": gin.H{
|
||
"path": path,
|
||
"ext": fileExt,
|
||
"size": f.Size,
|
||
"name": f.Filename,
|
||
},
|
||
})
|
||
}
|
||
func UploadAvator(c *gin.Context) {
|
||
f, err := c.FormFile("imgfile")
|
||
if err != nil {
|
||
c.JSON(200, gin.H{
|
||
"code": 400,
|
||
"msg": "上传失败!" + err.Error(),
|
||
})
|
||
return
|
||
} else {
|
||
|
||
fileExt := strings.ToLower(path.Ext(f.Filename))
|
||
if fileExt != ".png" && fileExt != ".jpg" && fileExt != ".gif" && fileExt != ".jpeg" {
|
||
c.JSON(200, gin.H{
|
||
"code": 400,
|
||
"msg": "上传失败!只允许png,jpg,gif,jpeg文件",
|
||
})
|
||
return
|
||
}
|
||
|
||
fileName := tools.Md5(fmt.Sprintf("%s%s", f.Filename, time.Now().String()))
|
||
|
||
fildDir := fmt.Sprintf("%savator/%d%s/", common.Upload, time.Now().Year(), time.Now().Month().String())
|
||
isExist, _ := tools.IsFileExist(fildDir)
|
||
if !isExist {
|
||
err := os.MkdirAll(fildDir, os.ModePerm)
|
||
if err != nil {
|
||
c.JSON(200, gin.H{
|
||
"code": 400,
|
||
"msg": "上传失败!" + err.Error(),
|
||
})
|
||
return
|
||
}
|
||
}
|
||
filepath := fmt.Sprintf("%s%s%s", fildDir, fileName, fileExt)
|
||
c.SaveUploadedFile(f, filepath)
|
||
|
||
path := "/" + filepath
|
||
//上传到阿里云oss
|
||
oss, err := lib.NewOssLib()
|
||
if err == nil {
|
||
dstUrl, err := oss.Upload(filepath, filepath)
|
||
if err == nil {
|
||
path = dstUrl
|
||
}
|
||
}
|
||
if err != nil {
|
||
log.Println("上传oss:", err)
|
||
}
|
||
c.JSON(200, gin.H{
|
||
"code": 200,
|
||
"msg": "上传成功!",
|
||
"result": gin.H{
|
||
"path": path,
|
||
},
|
||
})
|
||
}
|
||
}
|
||
|
||
func UploadEditorImg(c *gin.Context) {
|
||
f, err := c.FormFile("imgfile")
|
||
if err != nil {
|
||
c.JSON(200, gin.H{
|
||
"errno": 400,
|
||
"msg": "上传失败!",
|
||
})
|
||
return
|
||
} else {
|
||
|
||
fileExt := strings.ToLower(path.Ext(f.Filename))
|
||
if fileExt != ".png" && fileExt != ".jpg" && fileExt != ".gif" && fileExt != ".jpeg" {
|
||
c.JSON(200, gin.H{
|
||
"errno": 400,
|
||
"msg": "上传失败!只允许png,jpg,gif,jpeg文件",
|
||
})
|
||
return
|
||
}
|
||
|
||
fileName := tools.Md5(fmt.Sprintf("%s%s", f.Filename, time.Now().String()))
|
||
fildDir := fmt.Sprintf("%s%d%s/", common.Upload, time.Now().Year(), time.Now().Month().String())
|
||
isExist, _ := tools.IsFileExist(fildDir)
|
||
if !isExist {
|
||
os.Mkdir(fildDir, os.ModePerm)
|
||
}
|
||
filepath := fmt.Sprintf("%s%s%s", fildDir, fileName, fileExt)
|
||
c.SaveUploadedFile(f, filepath)
|
||
|
||
path := "/" + filepath
|
||
//上传到阿里云oss
|
||
oss, err := lib.NewOssLib()
|
||
if err == nil {
|
||
dstUrl, err := oss.Upload(filepath, filepath)
|
||
if err == nil {
|
||
path = dstUrl
|
||
}
|
||
}
|
||
c.JSON(200, gin.H{
|
||
"errno": 0,
|
||
"msg": "上传成功!",
|
||
"data": gin.H{
|
||
"url": path,
|
||
"alt": "",
|
||
"href": "",
|
||
},
|
||
})
|
||
}
|
||
}
|
||
func UploadAudioV2(c *gin.Context) {
|
||
//SendAttachment, err := strconv.ParseBool(models.FindConfig("SendAttachment"))
|
||
//if !SendAttachment || err != nil {
|
||
// c.JSON(200, gin.H{
|
||
// "code": 400,
|
||
// "msg": "禁止上传附件!",
|
||
// })
|
||
// return
|
||
//}
|
||
f, err := c.FormFile("realfile")
|
||
if err != nil {
|
||
c.JSON(200, gin.H{
|
||
"code": 400,
|
||
"msg": "上传失败!",
|
||
})
|
||
return
|
||
} else {
|
||
|
||
fileExt := ".mp3"
|
||
if f.Size >= 20*1024*1024 {
|
||
c.JSON(200, gin.H{
|
||
"code": 400,
|
||
"msg": "上传失败!不允许超过20M",
|
||
})
|
||
return
|
||
}
|
||
|
||
fileName := tools.Md5(fmt.Sprintf("%s%s", f.Filename, time.Now().String()))
|
||
fildDir := fmt.Sprintf("%s%d%s/", common.Upload, time.Now().Year(), time.Now().Month().String())
|
||
isExist, _ := tools.IsFileExist(fildDir)
|
||
if !isExist {
|
||
os.Mkdir(fildDir, os.ModePerm)
|
||
}
|
||
filepath := fmt.Sprintf("%s%s%s", fildDir, fileName, fileExt)
|
||
c.SaveUploadedFile(f, filepath)
|
||
path := "/" + filepath
|
||
//上传到阿里云oss
|
||
oss, err := lib.NewOssLib()
|
||
if err == nil {
|
||
dstUrl, err := oss.Upload(filepath, filepath)
|
||
if err == nil {
|
||
path = dstUrl
|
||
}
|
||
}
|
||
c.JSON(200, gin.H{
|
||
"code": 200,
|
||
"msg": "上传成功!",
|
||
"result": gin.H{
|
||
"path": path,
|
||
},
|
||
})
|
||
}
|
||
}
|