140 lines
2.7 KiB
Go
140 lines
2.7 KiB
Go
package controller
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"io"
|
|
"math/rand"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"time"
|
|
"vote-admin-api/api/responses"
|
|
e "vote-admin-api/extend/Editor"
|
|
"vote-admin-api/extend/aliyun"
|
|
"vote-admin-api/utils"
|
|
)
|
|
|
|
type Editor struct{}
|
|
|
|
// GetEditorConfig 编辑器-获取配置
|
|
func (b *Editor) GetEditorConfig(c *gin.Context) {
|
|
action := c.Query("action")
|
|
if action == "" {
|
|
responses.FailWithMessage("缺少参数", c)
|
|
return
|
|
}
|
|
|
|
// 获取配置 config
|
|
if action == "config" {
|
|
config := e.GetConfig()
|
|
c.JSON(http.StatusOK, config)
|
|
return
|
|
}
|
|
|
|
// 图片列表 listImage
|
|
if action == "listImage" {
|
|
responses.Ok(c)
|
|
return
|
|
}
|
|
|
|
// 文件列表 listFile
|
|
if action == "listFile" {
|
|
responses.Ok(c)
|
|
return
|
|
}
|
|
|
|
responses.Ok(c)
|
|
return
|
|
}
|
|
|
|
// EditorUpload 编辑器-上传
|
|
func (b *Editor) EditorUpload(c *gin.Context) {
|
|
action := c.Query("action")
|
|
if action == "" {
|
|
responses.FailWithMessage("缺少参数", c)
|
|
return
|
|
}
|
|
|
|
file, err := c.FormFile("file")
|
|
if err != nil {
|
|
responses.FailWithMessage("文件错误", c)
|
|
return
|
|
}
|
|
|
|
f, err := file.Open()
|
|
if err != nil {
|
|
responses.FailWithMessage("文件错误", c)
|
|
return
|
|
}
|
|
defer func(f multipart.File) {
|
|
err := f.Close()
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
}(f)
|
|
|
|
// 读取文件内容到字节切片
|
|
fileBytes, err := io.ReadAll(f)
|
|
if err != nil {
|
|
responses.FailWithMessage("文件错误", c)
|
|
return
|
|
}
|
|
|
|
// 添加图片水印
|
|
fileBytes, err = utils.AddWatermarkToImage(fileBytes, "./resource/3061726102564.png")
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
fileType := "jpg"
|
|
if file.Filename != "" {
|
|
fileType = utils.GetExtension(file.Filename)
|
|
if fileType == "" {
|
|
fileType = "jpg"
|
|
}
|
|
}
|
|
|
|
now := time.Now()
|
|
dateTimeString := now.Format("20060102150405") // 当前时间字符串
|
|
rand.New(rand.NewSource(time.Now().UnixNano())) // 设置随机数
|
|
|
|
var ossPath string
|
|
|
|
// 上传图片 image
|
|
if action == "image" {
|
|
ossPath = "static/images/" + fmt.Sprintf("%d", rand.Intn(9000)+1000) + dateTimeString + "." + fileType
|
|
}
|
|
|
|
// 上传视频 video
|
|
if action == "video" {
|
|
ossPath = "static/video/" + fmt.Sprintf("%d", rand.Intn(9000)+1000) + dateTimeString + "." + fileType
|
|
}
|
|
|
|
// 上传文件 file
|
|
if action == "file" {
|
|
ossPath = "static/file/" + fmt.Sprintf("%d", rand.Intn(9000)+1000) + dateTimeString + "." + fileType
|
|
}
|
|
|
|
if ossPath == "" {
|
|
responses.FailWithMessage("上传失败", c)
|
|
return
|
|
}
|
|
|
|
// 上传oss
|
|
_, err = aliyun.PutObjectByte(ossPath, fileBytes)
|
|
if err != nil {
|
|
responses.FailWithMessage(err.Error(), c)
|
|
return
|
|
}
|
|
|
|
var g e.UploadDto
|
|
g.Url = utils.AddOssDomain("/" + ossPath)
|
|
g.State = "SUCCESS"
|
|
g.Title = dateTimeString + "." + fileType
|
|
g.Original = dateTimeString + "." + fileType
|
|
g.Type = fileType
|
|
|
|
c.JSON(http.StatusOK, g)
|
|
return
|
|
}
|