mirror of
https://gitee.com/xiangheng/x_admin.git
synced 2025-10-16 05:00:43 +08:00
统一上传文件不区分图片、视频接口,素材中心私有化
This commit is contained in:
@@ -1,18 +1,16 @@
|
||||
package commonService
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"mime/multipart"
|
||||
"time"
|
||||
|
||||
"x_admin/plugin"
|
||||
"x_admin/schema/commonSchema"
|
||||
"x_admin/util"
|
||||
"x_admin/util/convert_util"
|
||||
)
|
||||
|
||||
type IUploadService interface {
|
||||
UploadImage(file *multipart.FileHeader, cid uint, aid uint) (res commonSchema.CommonUploadFileResp, e error)
|
||||
UploadVideo(file *multipart.FileHeader, cid uint, aid uint) (res commonSchema.CommonUploadFileResp, e error)
|
||||
}
|
||||
|
||||
var UploadService = NewUploadService()
|
||||
|
||||
// NewUploadService 初始化
|
||||
@@ -23,26 +21,34 @@ func NewUploadService() *uploadService {
|
||||
// uploadService 上传服务实现类
|
||||
type uploadService struct{}
|
||||
|
||||
// UploadImage 上传图片
|
||||
func (upSrv uploadService) UploadImage(file *multipart.FileHeader, cid uint, aid uint) (res commonSchema.CommonUploadFileResp, e error) {
|
||||
return upSrv.uploadFile(file, "image", 10, cid, aid)
|
||||
}
|
||||
|
||||
// UploadVideo 上传视频
|
||||
func (upSrv uploadService) UploadVideo(file *multipart.FileHeader, cid uint, aid uint) (res commonSchema.CommonUploadFileResp, e error) {
|
||||
return upSrv.uploadFile(file, "video", 20, cid, aid)
|
||||
}
|
||||
|
||||
// uploadFile 上传文件
|
||||
func (upSrv uploadService) uploadFile(file *multipart.FileHeader, folder string, fileType int, cid uint, aid uint) (res commonSchema.CommonUploadFileResp, e error) {
|
||||
// UploadFile 上传
|
||||
// cid 分类id
|
||||
// AdminId 用户id
|
||||
func (upSrv uploadService) UploadFile(file *multipart.FileHeader, cid uint, AdminId uint) (res commonSchema.CommonUploadFileResp, e error) {
|
||||
var upRes *plugin.UploadFile
|
||||
if upRes, e = plugin.StorageDriver.Upload(file, folder, fileType); e != nil {
|
||||
if upRes, e = plugin.StorageDriver.Upload(file); e != nil {
|
||||
return
|
||||
}
|
||||
var startTime = time.Now()
|
||||
|
||||
// 计算文件MD5
|
||||
md5, e := util.ToolsUtil.GetFileMD5(file)
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
var endTime = time.Now()
|
||||
var costTime = endTime.UnixNano() - startTime.UnixNano()
|
||||
// 毫秒
|
||||
costTime = costTime / 1000000
|
||||
|
||||
fmt.Printf("\n文件大小%d , md5时间: %d毫秒\n", file.Size, costTime)
|
||||
|
||||
var addReq commonSchema.CommonAlbumAddReq
|
||||
convert_util.Copy(&addReq, upRes)
|
||||
addReq.Aid = aid
|
||||
addReq.Cid = cid
|
||||
addReq.AdminId = AdminId //管理员
|
||||
addReq.Cid = cid // 分类id
|
||||
addReq.Hash = md5
|
||||
|
||||
var albumId uint
|
||||
if albumId, e = AlbumService.AlbumAdd(addReq); e != nil {
|
||||
return
|
||||
|
Reference in New Issue
Block a user