refactor: 重构服务层和仓库层

This commit is contained in:
lan
2025-12-03 10:58:39 +08:00
parent 034e02e93a
commit e873c58af9
32 changed files with 1445 additions and 5213 deletions

View File

@@ -3,6 +3,9 @@ package storage
import (
"context"
"fmt"
"io"
"net/url"
"strings"
"time"
"carrotskin/pkg/config"
@@ -137,3 +140,64 @@ func (s *StorageClient) BuildFileURL(bucketName, objectName string) string {
func (s *StorageClient) GetPublicURL() string {
return s.publicURL
}
// ObjectInfo 对象信息
type ObjectInfo struct {
Size int64
LastModified time.Time
ContentType string
ETag string
}
// GetObject 获取对象内容和信息
func (s *StorageClient) GetObject(ctx context.Context, bucketName, objectName string) (io.ReadCloser, *ObjectInfo, error) {
obj, err := s.client.GetObject(ctx, bucketName, objectName, minio.GetObjectOptions{})
if err != nil {
return nil, nil, fmt.Errorf("获取对象失败: %w", err)
}
stat, err := obj.Stat()
if err != nil {
obj.Close()
return nil, nil, fmt.Errorf("获取对象信息失败: %w", err)
}
info := &ObjectInfo{
Size: stat.Size,
LastModified: stat.LastModified,
ContentType: stat.ContentType,
ETag: stat.ETag,
}
return obj, info, nil
}
// ParseFileURL 从文件URL中解析出bucket和objectName
// URL格式: {publicURL}/{bucket}/{objectName}
func (s *StorageClient) ParseFileURL(fileURL string) (bucket, objectName string, err error) {
// 移除 publicURL 前缀
if !strings.HasPrefix(fileURL, s.publicURL) {
return "", "", fmt.Errorf("URL格式不正确必须以 %s 开头", s.publicURL)
}
// 移除 publicURL 前缀和开头的 /
path := strings.TrimPrefix(fileURL, s.publicURL)
path = strings.TrimPrefix(path, "/")
// 解析路径
parts := strings.SplitN(path, "/", 2)
if len(parts) < 2 {
return "", "", fmt.Errorf("URL格式不正确无法解析bucket和objectName")
}
bucket = parts[0]
objectName = parts[1]
// URL解码 objectName
decoded, err := url.PathUnescape(objectName)
if err == nil {
objectName = decoded
}
return bucket, objectName, nil
}