blog/BlogAdminService/internal/controller/FileController.go

129 lines
3.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package controller
import (
"BlogAdmin/internal/service"
"BlogAdmin/third_party/SessionUtil"
"BlogAdmin/third_party/database"
"Common/consts"
"Common/model/AjaxResult"
"Common/model/admin"
"Common/model/vo"
"context"
"net/http"
"strings"
"time"
"github.com/google/uuid"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/mvc"
"github.com/kataras/iris/v12/sessions"
"gorm.io/gorm"
)
type FileController struct {
Ctx iris.Context
Session *sessions.Session
}
type uploadResponse struct {
Success int `json:"success"`
Message string `json:"message"`
Url string `json:"url"`
Location string `json:"location"`
}
func (ctrl *FileController) Get() {
ctrl.Ctx.View("/admin/file/index.html")
}
func (ctrl *FileController) GetList() {
page := ctrl.Ctx.URLParamIntDefault("page", 0)
itemsPerPage := ctrl.Ctx.URLParamIntDefault("itemsPerPage", 10)
pages := service.FileService.PageSysFiles(page, itemsPerPage)
ctrl.Ctx.JSON(pages)
}
func (ctrl *FileController) PostUpload() {
file, fileHeader, err := ctrl.Ctx.FormFile("editormd-image-file")
if err != nil {
file, fileHeader, err = ctrl.Ctx.FormFile("file")
if err != nil {
return
}
}
user := SessionUtil.GetUser(ctrl.Session)
fileId := uuid.NewString()
fileName := fileHeader.Filename
extLastIndex := strings.LastIndex(fileName, ".")
ext := fileName[extLastIndex:]
fileSize := fileHeader.Size
var bytes []byte = make([]byte, fileSize)
file.Read(bytes)
sysFile := admin.SysFile{
Id: fileId,
FileName: fileName,
FileSize: fileSize,
CreateBy: user.Username,
CreateTime: time.Now().UnixMilli(),
Data: bytes, State: "1",
Del: 0,
}
err = database.WGorm(func(tx *gorm.DB) error {
err2 := tx.Table("common_files").Create(sysFile).Error
return err2
// log.Println(sysFile)
// return nil
})
if err != nil {
// {
// success : 0 | 1, // 0 表示上传失败1 表示上传成功
// message : "提示的信息,上传成功或上传失败及错误信息等。",
// url : "图片地址" // 上传成功时才返回
// }
ctrl.Ctx.JSON(uploadResponse{Success: 0, Message: "上传失败"})
return
}
ctrl.Ctx.JSON(uploadResponse{
Success: 1,
Message: "上传成功",
Url: "/file/" + fileId + ext,
Location: "/file/" + fileId + ext,
})
}
func (ctrl *FileController) DelFile() {
id := ctrl.Ctx.Params().Get("id")
if id == "" {
ctrl.Ctx.JSON(AjaxResult.Error("ID为空"))
return
}
ctx := context.Background()
err := database.WGorm(func(tx *gorm.DB) error {
txErr := tx.Table("common_files").Delete(&vo.CommonFiles{Id: id}).Error
if txErr != nil {
return txErr
}
database.RedisTemplate.Del(ctx, consts.REDIS_FILE+id, consts.REDIS_FILE_BYTES+id).Err()
return txErr
})
if err != nil {
ctrl.Ctx.JSON(AjaxResult.Error("删除失败"))
}
ctrl.Ctx.JSON(AjaxResult.OkMsg("删除完成", nil))
}
func (ctrl *FileController) BeforeActivation(activation mvc.BeforeActivation) {
// log.Println("before")
activation.Handle(http.MethodDelete, "/del/{id}", "DelFile")
}
func (ctrl *FileController) AfterActivation(activation mvc.AfterActivation) {
// log.Println("after")
}