refactor: separate backend domains and storage adapters
This commit is contained in:
+12
-23
@@ -3,7 +3,9 @@ package service
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
|
||||
"kra/internal/biz"
|
||||
"kra/internal/service/dto"
|
||||
)
|
||||
|
||||
type MediaService struct{ uc *biz.MediaUsecase }
|
||||
@@ -51,6 +53,13 @@ func (s *MediaService) Rename(ctx context.Context, id uint, name string) error {
|
||||
func (s *MediaService) ImportURLs(ctx context.Context, items []*biz.MediaFile) error {
|
||||
return s.uc.Repo().CreateMediaBatch(ctx, items)
|
||||
}
|
||||
func (s *MediaService) ImportURLRequests(ctx context.Context, userID uint, values []dto.ImportMediaRequest) error {
|
||||
items := make([]*biz.MediaFile, 0, len(values))
|
||||
for _, value := range values {
|
||||
items = append(items, &biz.MediaFile{Name: value.Name, CategoryID: value.ClassID, URL: value.URL, Tag: value.Tag, Key: value.Key, Size: value.Size, Mime: value.Mime, MD5: value.MD5, UserID: userID})
|
||||
}
|
||||
return s.ImportURLs(ctx, items)
|
||||
}
|
||||
func (s *MediaService) Storage(ctx context.Context, prefix, cursor string, limit int) ([]map[string]any, string, bool, error) {
|
||||
items, next, more, err := s.uc.ListStorage(ctx, prefix, cursor, limit)
|
||||
if err != nil {
|
||||
@@ -76,29 +85,9 @@ func (s *MediaService) Categories(ctx context.Context) ([]map[string]any, error)
|
||||
func (s *MediaService) SaveCategory(ctx context.Context, v *biz.AttachmentCategory) error {
|
||||
return s.uc.Repo().SaveCategory(ctx, v)
|
||||
}
|
||||
func (s *MediaService) SaveCategoryRequest(ctx context.Context, value *dto.CategoryRequest) error {
|
||||
return s.SaveCategory(ctx, &biz.AttachmentCategory{ID: value.ID, Name: value.Name, ParentID: value.PID})
|
||||
}
|
||||
func (s *MediaService) DeleteCategory(ctx context.Context, id uint) error {
|
||||
return s.uc.Repo().DeleteCategory(ctx, id)
|
||||
}
|
||||
func (s *MediaService) InitUpload(ctx context.Context, userID uint, name, hash string, size, chunkSize int64, total int) (map[string]any, error) {
|
||||
session, media, chunks, err := s.uc.InitUpload(ctx, userID, name, hash, size, chunkSize, total)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if media != nil {
|
||||
return map[string]any{"instant": true, "media": mediaDTO(media), "uploadedChunks": []int{}}, nil
|
||||
}
|
||||
return map[string]any{"instant": false, "uploadId": session.ID, "uploadedChunks": chunks, "media": nil}, nil
|
||||
}
|
||||
func (s *MediaService) SaveChunk(ctx context.Context, userID, uploadID uint, index int, hash string, reader io.Reader) error {
|
||||
return s.uc.SaveChunk(ctx, userID, uploadID, index, hash, reader)
|
||||
}
|
||||
func (s *MediaService) CompleteUpload(ctx context.Context, userID, uploadID uint) (map[string]any, error) {
|
||||
v, err := s.uc.CompleteUpload(ctx, userID, uploadID, "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return mediaDTO(v), nil
|
||||
}
|
||||
func (s *MediaService) CancelUpload(ctx context.Context, userID, uploadID uint) error {
|
||||
return s.uc.CancelUpload(ctx, userID, uploadID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user