forked from mtusi2026/mtusi2026lab4
refactor: isolate attachment lifecycle
This commit is contained in:
@@ -12,6 +12,8 @@ import (
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab4/internal/model"
|
||||
)
|
||||
|
||||
var ErrNotFound = errors.New("object not found")
|
||||
@@ -26,12 +28,6 @@ type Config struct {
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
type Object struct {
|
||||
Key string
|
||||
Size int64
|
||||
LastModified time.Time
|
||||
}
|
||||
|
||||
type Store struct {
|
||||
client *minio.Client
|
||||
bucket string
|
||||
@@ -115,14 +111,14 @@ func (s *Store) Get(ctx context.Context, key string) (io.ReadCloser, error) {
|
||||
return &timeoutReader{ReadCloser: object, cancel: cancel}, nil
|
||||
}
|
||||
|
||||
func (s *Store) Stat(ctx context.Context, key string) (Object, error) {
|
||||
func (s *Store) Stat(ctx context.Context, key string) (model.StoredObject, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, s.timeout)
|
||||
defer cancel()
|
||||
info, err := s.client.StatObject(ctx, s.bucket, key, minio.StatObjectOptions{})
|
||||
if err != nil {
|
||||
return Object{}, objectError("stat S3 object", err)
|
||||
return model.StoredObject{}, objectError("stat S3 object", err)
|
||||
}
|
||||
return Object{Key: info.Key, Size: info.Size, LastModified: info.LastModified}, nil
|
||||
return model.StoredObject{Key: info.Key, Size: info.Size, LastModified: info.LastModified}, nil
|
||||
}
|
||||
|
||||
func (s *Store) Delete(ctx context.Context, key string) error {
|
||||
@@ -134,19 +130,19 @@ func (s *Store) Delete(ctx context.Context, key string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) List(ctx context.Context, prefix, after string, limit int) ([]Object, string, error) {
|
||||
func (s *Store) List(ctx context.Context, prefix, after string, limit int) ([]model.StoredObject, string, error) {
|
||||
if limit < 1 || limit > 1000 {
|
||||
return nil, "", errors.New("object list limit must be between 1 and 1000")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(ctx, s.timeout)
|
||||
defer cancel()
|
||||
objects := make([]Object, 0, limit+1)
|
||||
objects := make([]model.StoredObject, 0, limit+1)
|
||||
options := minio.ListObjectsOptions{Prefix: prefix, Recursive: true, StartAfter: after, MaxKeys: limit + 1}
|
||||
for info := range s.client.ListObjects(ctx, s.bucket, options) {
|
||||
if info.Err != nil {
|
||||
return nil, "", fmt.Errorf("list S3 objects: %w", info.Err)
|
||||
}
|
||||
objects = append(objects, Object{Key: info.Key, Size: info.Size, LastModified: info.LastModified})
|
||||
objects = append(objects, model.StoredObject{Key: info.Key, Size: info.Size, LastModified: info.LastModified})
|
||||
if len(objects) == limit+1 {
|
||||
cancel()
|
||||
break
|
||||
|
||||
Reference in New Issue
Block a user