增加上传图片
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
"knowledge/api/responses"
|
||||
e "knowledge/extend/Editor"
|
||||
"knowledge/extend/aliyun"
|
||||
"knowledge/utils"
|
||||
"math/rand"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
@@ -13,4 +13,5 @@ type Api struct {
|
||||
Share // 分享
|
||||
BaseToken // 基础数据-飞花令
|
||||
BaseTokenItem // 基础数据-飞花令-明细
|
||||
Editor // 配置-编辑器
|
||||
}
|
||||
|
||||
@@ -95,6 +95,17 @@ func publicRouter(r *gin.Engine, api controller.Api) {
|
||||
|
||||
// 登陆
|
||||
adminGroup.POST("/login", api.AdminUser.Login)
|
||||
|
||||
// 配置
|
||||
systemGroup := adminGroup.Group("/system")
|
||||
{
|
||||
// 编辑器配置
|
||||
editorGroup := systemGroup.Group("/editor")
|
||||
{
|
||||
// 编辑器-获取配置
|
||||
editorGroup.GET("", api.Editor.GetEditorConfig)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,6 +232,17 @@ func privateRouter(r *gin.Engine, api controller.Api) {
|
||||
// 获取统计数据
|
||||
staticGroup.GET("", api.Static.GetStatic)
|
||||
}
|
||||
|
||||
// 配置
|
||||
systemGroup := adminGroup.Group("/system")
|
||||
{
|
||||
// 编辑器配置
|
||||
editorGroup := systemGroup.Group("/editor")
|
||||
{
|
||||
// 编辑器-上传
|
||||
editorGroup.POST("", api.Editor.EditorUpload)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取分享数据
|
||||
|
||||
Reference in New Issue
Block a user