mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-08-28 05:12:32 +08:00
模块化上传驱动,使用泛型优化工具库降低冗余
This commit is contained in:
22
server/internal/library/storager/config.go
Normal file
22
server/internal/library/storager/config.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package storager
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"hotgo/internal/model"
|
||||
)
|
||||
|
||||
var config *model.UploadConfig
|
||||
|
||||
func SetConfig(c *model.UploadConfig) {
|
||||
config = c
|
||||
}
|
||||
|
||||
func GetConfig() *model.UploadConfig {
|
||||
return config
|
||||
}
|
||||
|
||||
func GetModel(ctx context.Context) *gdb.Model {
|
||||
return g.Model("sys_attachment").Ctx(ctx)
|
||||
}
|
160
server/internal/library/storager/mime.go
Normal file
160
server/internal/library/storager/mime.go
Normal file
@@ -0,0 +1,160 @@
|
||||
// Package storager
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package storager
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"io"
|
||||
"path"
|
||||
)
|
||||
|
||||
// 文件归属分类
|
||||
const (
|
||||
KindImg = "images" // 图片
|
||||
KindDoc = "document" // 文档
|
||||
KindAudio = "audio" // 音频
|
||||
KindVideo = "video" // 视频
|
||||
KindOther = "other" // 其他
|
||||
)
|
||||
|
||||
var (
|
||||
// 图片类型
|
||||
imgType = g.MapStrStr{
|
||||
"jpeg": "image/jpeg",
|
||||
"jpg": "image/jpeg",
|
||||
"png": "image/png",
|
||||
"gif": "image/gif",
|
||||
"webp": "image/webp",
|
||||
"cr2": "image/x-canon-cr2",
|
||||
"tif": "image/tiff",
|
||||
"bmp": "image/bmp",
|
||||
"heif": "image/heif",
|
||||
"jxr": "image/vnd.ms-photo",
|
||||
"psd": "image/vnd.adobe.photoshop",
|
||||
"ico": "image/vnd.microsoft.icon",
|
||||
"dwg": "image/vnd.dwg",
|
||||
}
|
||||
|
||||
// 文档类型
|
||||
docType = g.MapStrStr{
|
||||
"doc": "application/msword",
|
||||
"docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||
"xls": "application/vnd.ms-excel",
|
||||
"xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
"ppt": "application/vnd.ms-powerpoint",
|
||||
"pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
||||
}
|
||||
|
||||
// 音频类型
|
||||
audioType = g.MapStrStr{
|
||||
"mid": "audio/midi",
|
||||
"mp3": "audio/mpeg",
|
||||
"m4a": "audio/mp4",
|
||||
"ogg": "audio/ogg",
|
||||
"flac": "audio/x-flac",
|
||||
"wav": "audio/x-wav",
|
||||
"amr": "audio/amr",
|
||||
"aac": "audio/aac",
|
||||
"aiff": "audio/x-aiff",
|
||||
}
|
||||
|
||||
// 视频类型
|
||||
videoType = g.MapStrStr{
|
||||
"mp4": "video/mp4",
|
||||
"m4v": "video/x-m4v",
|
||||
"mkv": "video/x-matroska",
|
||||
"webm": "video/webm",
|
||||
"mov": "video/quicktime",
|
||||
"avi": "video/x-msvideo",
|
||||
"wmv": "video/x-ms-wmv",
|
||||
"mpg": "video/mpeg",
|
||||
"flv": "video/x-flv",
|
||||
"3gp": "video/3gpp",
|
||||
}
|
||||
)
|
||||
|
||||
// IsImgType 判断是否为图片
|
||||
func IsImgType(ext string) bool {
|
||||
_, ok := imgType[ext]
|
||||
return ok
|
||||
}
|
||||
|
||||
// IsDocType 判断是否为文档
|
||||
func IsDocType(ext string) bool {
|
||||
_, ok := docType[ext]
|
||||
return ok
|
||||
}
|
||||
|
||||
// IsAudioType 判断是否为音频
|
||||
func IsAudioType(ext string) bool {
|
||||
_, ok := audioType[ext]
|
||||
return ok
|
||||
}
|
||||
|
||||
// IsVideoType 判断是否为视频
|
||||
func IsVideoType(ext string) bool {
|
||||
_, ok := videoType[ext]
|
||||
return ok
|
||||
}
|
||||
|
||||
// GetImgType 获取图片类型
|
||||
func GetImgType(ext string) (string, error) {
|
||||
if mime, ok := imgType[ext]; ok {
|
||||
return mime, nil
|
||||
}
|
||||
return "", gerror.New("Invalid image type")
|
||||
}
|
||||
|
||||
// GetFileType 获取文件类型
|
||||
func GetFileType(ext string) (string, error) {
|
||||
if mime, ok := imgType[ext]; ok {
|
||||
return mime, nil
|
||||
}
|
||||
if mime, ok := docType[ext]; ok {
|
||||
return mime, nil
|
||||
}
|
||||
if mime, ok := audioType[ext]; ok {
|
||||
return mime, nil
|
||||
}
|
||||
if mime, ok := videoType[ext]; ok {
|
||||
return mime, nil
|
||||
}
|
||||
return "", gerror.Newf("Invalid file type:%v", ext)
|
||||
}
|
||||
|
||||
// GetFileKind 获取文件所属分类
|
||||
func GetFileKind(ext string) string {
|
||||
if _, ok := imgType[ext]; ok {
|
||||
return KindImg
|
||||
}
|
||||
if _, ok := docType[ext]; ok {
|
||||
return KindDoc
|
||||
}
|
||||
if _, ok := audioType[ext]; ok {
|
||||
return KindAudio
|
||||
}
|
||||
if _, ok := videoType[ext]; ok {
|
||||
return KindVideo
|
||||
}
|
||||
return KindOther
|
||||
}
|
||||
|
||||
// Ext 获取文件后缀
|
||||
func Ext(baseName string) string {
|
||||
return gstr.StrEx(path.Ext(baseName), ".")
|
||||
}
|
||||
|
||||
// UploadFileByte 获取上传文件的byte
|
||||
func UploadFileByte(file *ghttp.UploadFile) ([]byte, error) {
|
||||
open, err := file.Open()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return io.ReadAll(open)
|
||||
}
|
12
server/internal/library/storager/model.go
Normal file
12
server/internal/library/storager/model.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package storager
|
||||
|
||||
// FileMeta 文件元数据
|
||||
type FileMeta struct {
|
||||
Filename string // 文件名称
|
||||
Size int64 // 文件大小
|
||||
Kind string // 文件所属分类
|
||||
MetaType string // 文件类型
|
||||
NaiveType string // NaiveUI类型
|
||||
Ext string // 文件后缀名
|
||||
Md5 string // 文件hash
|
||||
}
|
228
server/internal/library/storager/upload.go
Normal file
228
server/internal/library/storager/upload.go
Normal file
@@ -0,0 +1,228 @@
|
||||
package storager
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/gogf/gf/v2/util/grand"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/library/contexts"
|
||||
"hotgo/internal/model/entity"
|
||||
"hotgo/utility/encrypt"
|
||||
"hotgo/utility/url"
|
||||
"hotgo/utility/validate"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// UploadDrive 存储驱动
|
||||
type UploadDrive interface {
|
||||
// Upload 上传
|
||||
Upload(ctx context.Context, file *ghttp.UploadFile) (fullPath string, err error)
|
||||
}
|
||||
|
||||
// New 初始化存储驱动
|
||||
func New(name ...string) UploadDrive {
|
||||
var (
|
||||
driveType = consts.UploadDriveLocal
|
||||
drive UploadDrive
|
||||
)
|
||||
|
||||
if len(name) > 0 && name[0] != "" {
|
||||
driveType = name[0]
|
||||
}
|
||||
|
||||
switch driveType {
|
||||
case consts.UploadDriveLocal:
|
||||
drive = &LocalDrive{}
|
||||
case consts.UploadDriveUCloud:
|
||||
drive = &UCloudDrive{}
|
||||
case consts.UploadDriveCos:
|
||||
drive = &CosDrive{}
|
||||
case consts.UploadDriveOss:
|
||||
drive = &OssDrive{}
|
||||
case consts.UploadDriveQiNiu:
|
||||
drive = &QiNiuDrive{}
|
||||
default:
|
||||
panic(fmt.Sprintf("暂不支持的存储驱动:%v", driveType))
|
||||
}
|
||||
return drive
|
||||
}
|
||||
|
||||
// DoUpload 上传入口
|
||||
func DoUpload(ctx context.Context, file *ghttp.UploadFile, typ int) (result *entity.SysAttachment, err error) {
|
||||
if file == nil {
|
||||
err = gerror.New("文件必须!")
|
||||
return
|
||||
}
|
||||
|
||||
meta, err := GetFileMeta(file)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if _, err = GetFileType(meta.Ext); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
switch typ {
|
||||
case consts.UploadTypeFile:
|
||||
if config.FileSize > 0 && meta.Size > config.FileSize*1024*1024 {
|
||||
err = gerror.Newf("文件大小不能超过%vMB", config.FileSize)
|
||||
return
|
||||
}
|
||||
case consts.UploadTypeImage:
|
||||
if !IsImgType(meta.Ext) {
|
||||
err = gerror.New("上传的文件不是图片")
|
||||
return
|
||||
}
|
||||
if config.ImageSize > 0 && meta.Size > config.ImageSize*1024*1024 {
|
||||
err = gerror.Newf("图片大小不能超过%vMB", config.ImageSize)
|
||||
return
|
||||
}
|
||||
case consts.UploadTypeDoc:
|
||||
if !IsDocType(meta.Ext) {
|
||||
err = gerror.New("上传的文件不是文档")
|
||||
return
|
||||
}
|
||||
case consts.UploadTypeAudio:
|
||||
if !IsAudioType(meta.Ext) {
|
||||
err = gerror.New("上传的文件不是音频")
|
||||
return
|
||||
}
|
||||
case consts.UploadTypeVideo:
|
||||
if !IsVideoType(meta.Ext) {
|
||||
err = gerror.New("上传的文件不是视频")
|
||||
return
|
||||
}
|
||||
default:
|
||||
err = gerror.Newf("无效的上传类型:%v", typ)
|
||||
return
|
||||
}
|
||||
|
||||
result, err = hasFile(ctx, meta.Md5)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if result != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 上传到驱动
|
||||
fullPath, err := New(config.Drive).Upload(ctx, file)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// 写入附件记录
|
||||
return write(ctx, meta, fullPath)
|
||||
}
|
||||
|
||||
// LastUrl 根据驱动获取最终文件访问地址
|
||||
func LastUrl(ctx context.Context, fullPath, drive string) string {
|
||||
if validate.IsURL(fullPath) {
|
||||
return fullPath
|
||||
}
|
||||
|
||||
switch drive {
|
||||
case consts.UploadDriveLocal:
|
||||
return url.GetAddr(ctx) + "/" + fullPath
|
||||
case consts.UploadDriveUCloud:
|
||||
return config.UCloudEndpoint + "/" + fullPath
|
||||
case consts.UploadDriveCos:
|
||||
return config.CosBucketURL + "/" + fullPath
|
||||
case consts.UploadDriveOss:
|
||||
return config.OssBucketURL + "/" + fullPath
|
||||
case consts.UploadDriveQiNiu:
|
||||
return config.QiNiuDomain + "/" + fullPath
|
||||
default:
|
||||
return fullPath
|
||||
}
|
||||
}
|
||||
|
||||
// GetFileMeta 获取上传文件元数据
|
||||
func GetFileMeta(file *ghttp.UploadFile) (meta *FileMeta, err error) {
|
||||
meta = new(FileMeta)
|
||||
meta.Filename = file.Filename
|
||||
meta.Size = file.Size
|
||||
meta.Ext = Ext(file.Filename)
|
||||
meta.Kind = GetFileKind(meta.Ext)
|
||||
meta.MetaType, err = GetFileType(meta.Ext)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 兼容naiveUI
|
||||
naiveType := "text/plain"
|
||||
if IsImgType(Ext(file.Filename)) {
|
||||
naiveType = ""
|
||||
}
|
||||
meta.NaiveType = naiveType
|
||||
|
||||
// 文件hash
|
||||
b, err := UploadFileByte(file)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
meta.Md5 = encrypt.Md5ToString(gconv.String(encrypt.Hash32(b)))
|
||||
return
|
||||
}
|
||||
|
||||
// GenFullPath 根据目录和文件类型生成一个绝对地址
|
||||
func GenFullPath(basePath, ext string) string {
|
||||
fileName := strconv.FormatInt(gtime.TimestampNano(), 36) + grand.S(6)
|
||||
fileName = fileName + ext
|
||||
return basePath + gtime.Date() + "/" + strings.ToLower(fileName)
|
||||
}
|
||||
|
||||
// write 写入附件记录
|
||||
func write(ctx context.Context, meta *FileMeta, fullPath string) (models *entity.SysAttachment, err error) {
|
||||
models = &entity.SysAttachment{
|
||||
Id: 0,
|
||||
AppId: contexts.GetModule(ctx),
|
||||
MemberId: contexts.GetUserId(ctx),
|
||||
Drive: config.Drive,
|
||||
Size: meta.Size,
|
||||
Path: fullPath,
|
||||
FileUrl: fullPath,
|
||||
Name: meta.Filename,
|
||||
Kind: meta.Kind,
|
||||
MetaType: meta.MetaType,
|
||||
NaiveType: meta.NaiveType,
|
||||
Ext: meta.Ext,
|
||||
Md5: meta.Md5,
|
||||
Status: consts.StatusEnabled,
|
||||
}
|
||||
|
||||
id, err := GetModel(ctx).Data(models).InsertAndGetId()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
models.Id = id
|
||||
return
|
||||
}
|
||||
|
||||
// hasFile 检查附件是否存在
|
||||
func hasFile(ctx context.Context, md5 string) (res *entity.SysAttachment, err error) {
|
||||
if err = GetModel(ctx).Where("md5", md5).Scan(&res); err != nil {
|
||||
err = gerror.Wrap(err, "检查文件hash时出现错误")
|
||||
return
|
||||
}
|
||||
|
||||
if res == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 只有在上传时才会检查md5值,如果附件存在则更新最后上传时间,保证上传列表更新显示在最前面
|
||||
if res.Id > 0 {
|
||||
_, _ = GetModel(ctx).WherePri(res.Id).Data(g.Map{
|
||||
"status": consts.StatusEnabled,
|
||||
"updated_at": gtime.Now(),
|
||||
}).Update()
|
||||
}
|
||||
return
|
||||
}
|
42
server/internal/library/storager/upload_cos.go
Normal file
42
server/internal/library/storager/upload_cos.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package storager
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// CosDrive 腾讯云cos驱动
|
||||
type CosDrive struct {
|
||||
}
|
||||
|
||||
// Upload 上传到腾讯云cos对象存储
|
||||
func (d *CosDrive) Upload(ctx context.Context, file *ghttp.UploadFile) (fullPath string, err error) {
|
||||
if config.CosPath == "" {
|
||||
err = gerror.New("COS存储驱动必须配置存储路径!")
|
||||
return
|
||||
}
|
||||
|
||||
// 流式上传本地小文件
|
||||
f2, err := file.Open()
|
||||
defer func() { _ = f2.Close() }()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
URL, _ := url.Parse(config.CosBucketURL)
|
||||
client := cos.NewClient(&cos.BaseURL{BucketURL: URL}, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
SecretID: config.CosSecretId,
|
||||
SecretKey: config.CosSecretKey,
|
||||
},
|
||||
})
|
||||
|
||||
fullPath = GenFullPath(config.UCloudPath, gfile.Ext(file.Filename))
|
||||
_, err = client.Object.Put(ctx, fullPath, f2, nil)
|
||||
return
|
||||
}
|
42
server/internal/library/storager/upload_local.go
Normal file
42
server/internal/library/storager/upload_local.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package storager
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// LocalDrive 本地驱动
|
||||
type LocalDrive struct {
|
||||
}
|
||||
|
||||
// Upload 上传到本地
|
||||
func (d *LocalDrive) Upload(ctx context.Context, file *ghttp.UploadFile) (fullPath string, err error) {
|
||||
var (
|
||||
sp = g.Cfg().MustGet(ctx, "server.serverRoot")
|
||||
nowDate = gtime.Date()
|
||||
)
|
||||
|
||||
if sp.IsEmpty() {
|
||||
err = gerror.New("本地上传驱动必须配置静态路径!")
|
||||
return
|
||||
}
|
||||
|
||||
if config.LocalPath == "" {
|
||||
err = gerror.New("本地上传驱动必须配置本地存储路径!")
|
||||
return
|
||||
}
|
||||
|
||||
// 包含静态文件夹的路径
|
||||
fullDirPath := strings.Trim(sp.String(), "/") + "/" + config.LocalPath + nowDate
|
||||
fileName, err := file.Save(fullDirPath, true)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// 不含静态文件夹的路径
|
||||
fullPath = config.LocalPath + nowDate + "/" + fileName
|
||||
return
|
||||
}
|
42
server/internal/library/storager/upload_oss.go
Normal file
42
server/internal/library/storager/upload_oss.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package storager
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/aliyun/aliyun-oss-go-sdk/oss"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
)
|
||||
|
||||
// OssDrive 阿里云oss驱动
|
||||
type OssDrive struct {
|
||||
}
|
||||
|
||||
// Upload 上传到阿里云oss
|
||||
func (d *OssDrive) Upload(ctx context.Context, file *ghttp.UploadFile) (fullPath string, err error) {
|
||||
if config.OssPath == "" {
|
||||
err = gerror.New("OSS存储驱动必须配置存储路径!")
|
||||
return
|
||||
}
|
||||
|
||||
// 流式上传本地小文件
|
||||
f2, err := file.Open()
|
||||
defer func() { _ = f2.Close() }()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
client, err := oss.New(config.OssEndpoint, config.OssSecretId, config.OssSecretKey)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
bucket, err := client.Bucket(config.OssBucket)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
fullPath = GenFullPath(config.UCloudPath, gfile.Ext(file.Filename))
|
||||
err = bucket.PutObject(fullPath, f2)
|
||||
return
|
||||
}
|
52
server/internal/library/storager/upload_qiniu.go
Normal file
52
server/internal/library/storager/upload_qiniu.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package storager
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/qiniu/go-sdk/v7/auth/qbox"
|
||||
"github.com/qiniu/go-sdk/v7/storage"
|
||||
)
|
||||
|
||||
// QiNiuDrive 七牛云对象存储驱动
|
||||
type QiNiuDrive struct {
|
||||
}
|
||||
|
||||
// Upload 上传到七牛云对象存储
|
||||
func (d *QiNiuDrive) Upload(ctx context.Context, file *ghttp.UploadFile) (fullPath string, err error) {
|
||||
if config.QiNiuPath == "" {
|
||||
err = gerror.New("七牛云存储驱动必须配置存储路径!")
|
||||
return
|
||||
}
|
||||
|
||||
// 流式上传本地小文件
|
||||
f2, err := file.Open()
|
||||
defer func() { _ = f2.Close() }()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
putPolicy := storage.PutPolicy{
|
||||
Scope: config.QiNiuBucket,
|
||||
}
|
||||
token := putPolicy.UploadToken(qbox.NewMac(config.QiNiuAccessKey, config.QiNiuSecretKey))
|
||||
|
||||
cfg := storage.Config{}
|
||||
|
||||
// 是否使用https域名
|
||||
cfg.UseHTTPS = true
|
||||
|
||||
// 上传是否使用CDN上传加速
|
||||
cfg.UseCdnDomains = false
|
||||
|
||||
// 空间对应的机房
|
||||
cfg.Region, err = storage.GetRegion(config.QiNiuAccessKey, config.QiNiuBucket)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
fullPath = GenFullPath(config.UCloudPath, gfile.Ext(file.Filename))
|
||||
err = storage.NewFormUploader(&cfg).Put(ctx, &storage.PutRet{}, token, fullPath, f2, file.Size, &storage.PutExtra{})
|
||||
return
|
||||
}
|
45
server/internal/library/storager/upload_ucloud.go
Normal file
45
server/internal/library/storager/upload_ucloud.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package storager
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
upload "github.com/ufilesdk-dev/ufile-gosdk"
|
||||
)
|
||||
|
||||
// UCloudDrive UCloud对象存储驱动
|
||||
type UCloudDrive struct {
|
||||
}
|
||||
|
||||
// Upload 上传到UCloud对象存储
|
||||
func (d *UCloudDrive) Upload(ctx context.Context, file *ghttp.UploadFile) (fullPath string, err error) {
|
||||
if config.UCloudPath == "" {
|
||||
err = gerror.New("UCloud存储驱动必须配置存储路径!")
|
||||
return
|
||||
}
|
||||
|
||||
client, err := upload.NewFileRequest(&upload.Config{
|
||||
PublicKey: config.UCloudPublicKey,
|
||||
PrivateKey: config.UCloudPrivateKey,
|
||||
BucketHost: config.UCloudBucketHost,
|
||||
BucketName: config.UCloudBucketName,
|
||||
FileHost: config.UCloudFileHost,
|
||||
Endpoint: config.UCloudEndpoint,
|
||||
VerifyUploadMD5: false,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 流式上传本地小文件
|
||||
f2, err := file.Open()
|
||||
defer func() { _ = f2.Close() }()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
fullPath = GenFullPath(config.UCloudPath, gfile.Ext(file.Filename))
|
||||
err = client.IOPut(f2, fullPath, "")
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user