mirror of
https://github.com/quarkcloudio/quark-go.git
synced 2025-11-03 10:11:11 +08:00
101 lines
2.2 KiB
Go
101 lines
2.2 KiB
Go
package uploads
|
|
|
|
import (
|
|
"reflect"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/quarkcms/quark-go/pkg/app/model"
|
|
"github.com/quarkcms/quark-go/pkg/builder"
|
|
"github.com/quarkcms/quark-go/pkg/builder/template/adminupload"
|
|
"github.com/quarkcms/quark-go/pkg/msg"
|
|
"github.com/quarkcms/quark-go/pkg/storage"
|
|
)
|
|
|
|
type File struct {
|
|
adminupload.Template
|
|
}
|
|
|
|
// 初始化
|
|
func (p *File) Init() interface{} {
|
|
|
|
// 初始化模板
|
|
p.TemplateInit()
|
|
|
|
// 限制文件大小
|
|
p.LimitSize = 1024 * 1024 * 1024 * 2
|
|
|
|
// 限制文件类型
|
|
p.LimitType = []string{
|
|
"image/jpg",
|
|
"image/jpeg",
|
|
"image/png",
|
|
"image/gif",
|
|
}
|
|
|
|
// 设置文件上传路径
|
|
p.SavePath = "./website/storage/files/" + time.Now().Format("20060102") + "/"
|
|
|
|
return p
|
|
}
|
|
|
|
// 上传前回调
|
|
func (p *File) BeforeHandle(request *builder.Request, templateInstance interface{}, fileSystem *storage.FileSystem) (*storage.FileSystem, *storage.FileInfo, error) {
|
|
fileHash, err := fileSystem.GetFileHash()
|
|
if err != nil {
|
|
return fileSystem, nil, err
|
|
}
|
|
|
|
getFileInfo, _ := (&model.File{}).GetInfoByHash(fileHash)
|
|
if err != nil {
|
|
return fileSystem, nil, err
|
|
}
|
|
if getFileInfo.Id != 0 {
|
|
fileInfo := &storage.FileInfo{
|
|
Name: getFileInfo.Name,
|
|
Size: getFileInfo.Size,
|
|
Ext: getFileInfo.Ext,
|
|
Path: getFileInfo.Path,
|
|
Url: getFileInfo.Url,
|
|
Hash: getFileInfo.Hash,
|
|
}
|
|
|
|
return fileSystem, fileInfo, err
|
|
}
|
|
|
|
return fileSystem, nil, err
|
|
}
|
|
|
|
// 上传完成后回调
|
|
func (p *File) AfterHandle(request *builder.Request, templateInstance interface{}, result *storage.FileInfo) interface{} {
|
|
driver := reflect.
|
|
ValueOf(templateInstance).
|
|
Elem().
|
|
FieldByName("Driver").String()
|
|
|
|
// 重写url
|
|
if driver == storage.LocalDriver {
|
|
result.Url = strings.ReplaceAll(result.Url, "./website/", "/")
|
|
}
|
|
|
|
adminInfo, err := (&model.Admin{}).GetAuthUser(request.Token())
|
|
if err != nil {
|
|
return msg.Error(err.Error(), "")
|
|
}
|
|
|
|
// 插入数据库
|
|
(&model.File{}).InsertGetId(&model.File{
|
|
ObjType: "admin",
|
|
ObjId: adminInfo.Id,
|
|
Name: result.Name,
|
|
Size: result.Size,
|
|
Ext: result.Ext,
|
|
Path: result.Path,
|
|
Url: result.Url,
|
|
Hash: result.Hash,
|
|
Status: 1,
|
|
})
|
|
|
|
return msg.Success("上传成功", "", result)
|
|
}
|