* feat(mcp): 新增gva_review工具并优化字典和代码生成逻辑 * fix: 调整mcp整体逻辑 * chore: 更新.gitignore,添加对本地配置文件的忽略 * feat(logo): 新增Logo组件并在多个页面中替换原有logo实现 * fix: 修复菜单 Logo 部分删除文本后显示异常的问题 * fix:添加字典列表搜索,支持中英文搜索.添加字典详情搜索 * style: 优化部分视觉样式 * feat: 增强错误预览组件的暗黑模式支持 * feat: 优化请求错误消息获取逻辑,增加状态文本优先级 * feat: 添加前端登录验证码静态验证逻辑 * feat: 添加开发环境启动脚本 * feat: 更新 SvgIcon 组件,支持本地图标和 Iconify 图标、移除未使用的 unocss 依赖 * fix:字典支持 tree 结构 * feat: 优化动态路由注册方式 * feat: 添加配置控制标签页keep-alive功能 * feat: 添加全局错误处理机制,捕获 Vue 和 JS 错误 * refactor: 移除API和菜单创建结果中的权限分配提醒,优化输出信息 * feat: 更新 reset.scss,优化全局样式重置,增强兼容性和可读性 * refactor(字典详情): 优化字典详情查询逻辑,移除预加载改为按需加载 * refactor(路由管理): 优化路由添加逻辑,增强路径处理和顶级路由注册 * refactor(系统配置): 将auto-migrate修改为disable-auto-migrate,保证用户升级的兼容性 * feat(utils): 优化字典数据递归查找功能并替换select为tree-select * fix(deps): 修复在字段类型为file生成搜索条件无法运行的bug * fix: 修复header的tools中icon不展示的问题 --------- Co-authored-by: piexlMax(奇淼 <qimiaojiangjizhao@gmail.com> Co-authored-by: Azir-11 <2075125282@qq.com> Co-authored-by: bypanghu <bypanghu@163.com> Co-authored-by: feitianbubu <feitianbubu@qq.com> Co-authored-by: 青菜白玉汤 <79054161+Azir-11@users.noreply.github.com> Co-authored-by: krank <emosick@qq.com>
107 lines
3.4 KiB
Go
107 lines
3.4 KiB
Go
package upload
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"mime"
|
|
"mime/multipart"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
|
"github.com/flipped-aurora/gin-vue-admin/server/utils"
|
|
"github.com/minio/minio-go/v7"
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
var MinioClient *Minio // 优化性能,但是不支持动态配置
|
|
|
|
type Minio struct {
|
|
Client *minio.Client
|
|
bucket string
|
|
}
|
|
|
|
func GetMinio(endpoint, accessKeyID, secretAccessKey, bucketName string, useSSL bool) (*Minio, error) {
|
|
if MinioClient != nil {
|
|
return MinioClient, nil
|
|
}
|
|
// Initialize minio client object.
|
|
minioClient, err := minio.New(endpoint, &minio.Options{
|
|
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
|
|
Secure: useSSL, // Set to true if using https
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// 尝试创建bucket
|
|
err = minioClient.MakeBucket(context.Background(), bucketName, minio.MakeBucketOptions{})
|
|
if err != nil {
|
|
// Check to see if we already own this bucket (which happens if you run this twice)
|
|
exists, errBucketExists := minioClient.BucketExists(context.Background(), bucketName)
|
|
if errBucketExists == nil && exists {
|
|
// log.Printf("We already own %s\n", bucketName)
|
|
} else {
|
|
return nil, err
|
|
}
|
|
}
|
|
MinioClient = &Minio{Client: minioClient, bucket: bucketName}
|
|
return MinioClient, nil
|
|
}
|
|
|
|
func (m *Minio) UploadFile(file *multipart.FileHeader) (filePathres, key string, uploadErr error) {
|
|
f, openError := file.Open()
|
|
// mutipart.File to os.File
|
|
if openError != nil {
|
|
global.GVA_LOG.Error("function file.Open() Failed", zap.Any("err", openError.Error()))
|
|
return "", "", errors.New("function file.Open() Failed, err:" + openError.Error())
|
|
}
|
|
|
|
filecontent := bytes.Buffer{}
|
|
_, err := io.Copy(&filecontent, f)
|
|
if err != nil {
|
|
global.GVA_LOG.Error("读取文件失败", zap.Any("err", err.Error()))
|
|
return "", "", errors.New("读取文件失败, err:" + err.Error())
|
|
}
|
|
f.Close() // 创建文件 defer 关闭
|
|
|
|
// 对文件名进行加密存储
|
|
ext := filepath.Ext(file.Filename)
|
|
filename := utils.MD5V([]byte(strings.TrimSuffix(file.Filename, ext))) + ext
|
|
if global.GVA_CONFIG.Minio.BasePath == "" {
|
|
filePathres = "uploads" + "/" + time.Now().Format("2006-01-02") + "/" + filename
|
|
} else {
|
|
filePathres = global.GVA_CONFIG.Minio.BasePath + "/" + time.Now().Format("2006-01-02") + "/" + filename
|
|
}
|
|
|
|
// 根据文件扩展名检测 MIME 类型
|
|
contentType := mime.TypeByExtension(ext)
|
|
if contentType == "" {
|
|
contentType = "application/octet-stream"
|
|
}
|
|
|
|
// 设置超时10分钟
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*10)
|
|
defer cancel()
|
|
|
|
// Upload the file with PutObject 大文件自动切换为分片上传
|
|
info, err := m.Client.PutObject(ctx, global.GVA_CONFIG.Minio.BucketName, filePathres, &filecontent, file.Size, minio.PutObjectOptions{ContentType: contentType})
|
|
if err != nil {
|
|
global.GVA_LOG.Error("上传文件到minio失败", zap.Any("err", err.Error()))
|
|
return "", "", errors.New("上传文件到minio失败, err:" + err.Error())
|
|
}
|
|
return global.GVA_CONFIG.Minio.BucketUrl + "/" + info.Key, filePathres, nil
|
|
}
|
|
|
|
func (m *Minio) DeleteFile(key string) error {
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
|
defer cancel()
|
|
|
|
// Delete the object from MinIO
|
|
err := m.Client.RemoveObject(ctx, m.bucket, key, minio.RemoveObjectOptions{})
|
|
return err
|
|
}
|