113 lines
2.8 KiB
Go
113 lines
2.8 KiB
Go
package adm_controller
|
||
|
||
import (
|
||
"Blog/internal/client"
|
||
"Blog/internal/consts"
|
||
"Blog/internal/model"
|
||
"Blog/internal/model/result"
|
||
"Blog/internal/repository"
|
||
"Blog/internal/service"
|
||
"Blog/internal/utils"
|
||
"context"
|
||
"net/http"
|
||
"strings"
|
||
|
||
"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.PageFiles(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 := utils.SessionUtil.GetUser(ctrl.Session)
|
||
// fileId := uuid.NewString()
|
||
fileName := fileHeader.Filename
|
||
extLastIndex := strings.LastIndex(fileName, ".")
|
||
ext := fileName[extLastIndex:]
|
||
fileSize := fileHeader.Size
|
||
var data []byte = make([]byte, fileSize)
|
||
file.Read(data)
|
||
uploadFile := service.FileService.SaveFile(fileName, data, user.Username)
|
||
|
||
// {
|
||
// success : 0 | 1, // 0 表示上传失败,1 表示上传成功
|
||
// message : "提示的信息,上传成功或上传失败及错误信息等。",
|
||
// url : "图片地址" // 上传成功时才返回
|
||
// }
|
||
if uploadFile == nil {
|
||
ctrl.Ctx.JSON(uploadResponse{Success: 0, Message: "上传失败"})
|
||
return
|
||
}
|
||
|
||
fileId := uploadFile.Id
|
||
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(result.Error("ID为空"))
|
||
return
|
||
}
|
||
ctx := context.Background()
|
||
err := repository.FileRepository.WGorm(func(tx *gorm.DB) error {
|
||
txErr := tx.Table(consts.TABLE_UPLOAD_FILE).Delete(&model.UploadFile{Id: id}).Error
|
||
if txErr != nil {
|
||
return txErr
|
||
}
|
||
client.RedisClient.Del(ctx, consts.REDIS_FILE+id, consts.REDIS_FILE_BYTES+id).Err()
|
||
|
||
return txErr
|
||
})
|
||
|
||
if err != nil {
|
||
ctrl.Ctx.JSON(result.Error("删除失败"))
|
||
}
|
||
ctrl.Ctx.JSON(result.OkMsg("删除完成", nil))
|
||
}
|
||
|
||
func (ctrl *FileController) BeforeActivation(activation mvc.BeforeActivation) {
|
||
// logrus.Info("before")
|
||
activation.Handle(http.MethodDelete, "/del/{id}", "DelFile")
|
||
}
|
||
|
||
func (ctrl *FileController) AfterActivation(activation mvc.AfterActivation) {
|
||
// logrus.Info("after")
|
||
}
|