mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-08-28 05:12:32 +08:00
发布v2.11.5版本,更新内容请查看:https://github.com/bufanyun/hotgo/blob/v2.0/docs/guide-zh-CN/start-update-log.md
This commit is contained in:
@@ -50,6 +50,8 @@ func New(name ...string) UploadDrive {
|
||||
drive = &OssDrive{}
|
||||
case consts.UploadDriveQiNiu:
|
||||
drive = &QiNiuDrive{}
|
||||
case consts.UploadDriveMinio:
|
||||
drive = &MinioDrive{}
|
||||
default:
|
||||
panic(fmt.Sprintf("暂不支持的存储驱动:%v", driveType))
|
||||
}
|
||||
@@ -82,6 +84,11 @@ func DoUpload(ctx context.Context, typ string, file *ghttp.UploadFile) (result *
|
||||
err = gerror.Newf("图片大小不能超过%vMB", config.ImageSize)
|
||||
return
|
||||
}
|
||||
|
||||
if len(config.ImageType) > 0 && !validate.InSlice(strings.Split(config.ImageType, `,`), meta.Ext) {
|
||||
err = gerror.New("上传图片类型未经允许")
|
||||
return
|
||||
}
|
||||
case KindDoc:
|
||||
if !IsDocType(meta.Ext) {
|
||||
err = gerror.New("上传的文件不是文档")
|
||||
@@ -110,6 +117,11 @@ func DoUpload(ctx context.Context, typ string, file *ghttp.UploadFile) (result *
|
||||
err = gerror.Newf("文件大小不能超过%vMB", config.FileSize)
|
||||
return
|
||||
}
|
||||
|
||||
if len(config.FileType) > 0 && !validate.InSlice(strings.Split(config.FileType, `,`), meta.Ext) {
|
||||
err = gerror.New("上传文件类型未经允许")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
result, err = hasFile(ctx, meta.Md5)
|
||||
@@ -148,6 +160,8 @@ func LastUrl(ctx context.Context, fullPath, drive string) string {
|
||||
return config.OssBucketURL + "/" + fullPath
|
||||
case consts.UploadDriveQiNiu:
|
||||
return config.QiNiuDomain + "/" + fullPath
|
||||
case consts.UploadDriveMinio:
|
||||
return fmt.Sprintf("%s/%s/%s", config.MinioDomain, config.MinioBucket, fullPath)
|
||||
default:
|
||||
return fullPath
|
||||
}
|
||||
|
64
server/internal/library/storager/upload_minio.go
Normal file
64
server/internal/library/storager/upload_minio.go
Normal file
@@ -0,0 +1,64 @@
|
||||
// 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 (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
"github.com/minio/minio-go/v7/pkg/s3utils"
|
||||
"mime"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// MinioDrive minio对象存储驱动
|
||||
type MinioDrive struct {
|
||||
}
|
||||
|
||||
// Upload 上传到minio对象存储
|
||||
func (d *MinioDrive) Upload(ctx context.Context, file *ghttp.UploadFile) (fullPath string, err error) {
|
||||
if config.MinioPath == "" {
|
||||
err = gerror.New("minio存储驱动必须配置存储路径!")
|
||||
return
|
||||
}
|
||||
|
||||
client, err := minio.New(config.MinioEndpoint, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(config.MinioAccessKey, config.MinioSecretKey, ""),
|
||||
Secure: config.MinioUseSSL == 1,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if err = s3utils.CheckValidBucketName(config.MinioBucket); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
fullPath = GenFullPath(config.MinioPath, gfile.Ext(file.Filename))
|
||||
if err = s3utils.CheckValidObjectName(fullPath); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
reader, err := file.Open()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer reader.Close()
|
||||
|
||||
opts := minio.PutObjectOptions{
|
||||
ContentType: mime.TypeByExtension(filepath.Ext(file.Filename)),
|
||||
}
|
||||
if opts.ContentType == "" {
|
||||
opts.ContentType = "application/octet-stream"
|
||||
}
|
||||
|
||||
_, err = client.PutObject(ctx, config.MinioBucket, fullPath, reader, file.Size, opts)
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user