Files

42 lines
1.4 KiB
Go

package oss
import (
"context"
"fmt"
"regexp"
"gitea.redpowerfuture.com/red-future/common/utils"
"github.com/gogf/gf/v2/frame/g"
)
// ossObjectPathPattern 匹配 MinIO 上传生成的对象路径(不带 http 前缀的相对路径):
// /YYYY-MM-DD/32位uuid.扩展名,如 /2026-08-19/1e9d9e48-3f6b-4a2c-8d5e-1f2a3b4c.png
var ossObjectPathPattern = regexp.MustCompile(`^/\d{4}-\d{2}-\d{2}/[0-9a-fA-F-]{32}\.[a-zA-Z0-9]{1,10}$`)
// IsOSSPath 判断字符串是否为 MinIO 对象路径(无 http(s) 前缀)。
// 模型网关把结果转存 OSS 后返回该裸路径,消费方据此识别"已是文件路径"而不再重复上传。
// 对象命名规则见 oss/minio 的 ensureBucketAndObjectName,格式变化只需改这一处。
func IsOSSPath(s string) bool {
return ossObjectPathPattern.MatchString(s)
}
// GetBucketName 获取 bucket 名称(tenantid-{tenantId},桶归属租户)。
func GetBucketName(ctx context.Context) (bucketName string, err error) {
user, err := utils.GetUserInfo(ctx)
if err != nil {
return
}
bucketName = fmt.Sprintf("tenantid-%d", user.TenantId)
return
}
// GetFileAddressPrefix 拼接图片前缀地址({filePrefix}/{bucketName})。
func GetFileAddressPrefix(ctx context.Context) (imageUrl string, err error) {
bucketName, err := GetBucketName(ctx)
if err != nil {
return
}
imageUrl = fmt.Sprintf("%s/%s", g.Cfg().MustGet(ctx, "filePrefix").String(), bucketName)
return
}