105 lines
4.2 KiB
Go
105 lines
4.2 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"kra/internal/biz"
|
|
)
|
|
|
|
type MediaService struct{ uc *biz.MediaUsecase }
|
|
|
|
func NewMediaService(uc *biz.MediaUsecase) *MediaService { return &MediaService{uc: uc} }
|
|
func mediaDTO(v *biz.MediaFile) map[string]any {
|
|
return map[string]any{"ID": v.ID, "CreatedAt": v.CreatedAt, "UpdatedAt": v.UpdatedAt, "DeletedAt": nil, "name": v.Name, "classId": v.CategoryID, "url": v.URL, "tag": v.Tag, "key": v.Key, "size": v.Size, "mime": v.Mime, "md5": v.MD5, "userId": v.UserID}
|
|
}
|
|
func categoryDTO(v *biz.AttachmentCategory) map[string]any {
|
|
children := make([]map[string]any, 0, len(v.Children))
|
|
for _, x := range v.Children {
|
|
children = append(children, categoryDTO(x))
|
|
}
|
|
return map[string]any{"ID": v.ID, "CreatedAt": v.CreatedAt, "UpdatedAt": v.UpdatedAt, "DeletedAt": nil, "name": v.Name, "pid": v.ParentID, "children": children}
|
|
}
|
|
func (s *MediaService) Upload(ctx context.Context, userID uint, name, mime string, categoryID int, reader io.Reader, save bool) (map[string]any, error) {
|
|
v, err := s.uc.Upload(ctx, userID, name, mime, categoryID, reader, save)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return mediaDTO(v), nil
|
|
}
|
|
func (s *MediaService) Media(ctx context.Context, id uint) (map[string]any, error) {
|
|
v, err := s.uc.Repo().FindMedia(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return mediaDTO(v), nil
|
|
}
|
|
func (s *MediaService) MediaList(ctx context.Context, page, size int, keyword string, categoryID int, tag string, userID uint) ([]map[string]any, int64, error) {
|
|
items, total, err := s.uc.Repo().ListMedia(ctx, page, size, keyword, categoryID, tag, userID)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
out := make([]map[string]any, 0, len(items))
|
|
for _, v := range items {
|
|
out = append(out, mediaDTO(v))
|
|
}
|
|
return out, total, nil
|
|
}
|
|
func (s *MediaService) Delete(ctx context.Context, id uint) error { return s.uc.Delete(ctx, id) }
|
|
func (s *MediaService) Rename(ctx context.Context, id uint, name string) error {
|
|
return s.uc.Repo().UpdateMediaName(ctx, id, name)
|
|
}
|
|
func (s *MediaService) ImportURLs(ctx context.Context, items []*biz.MediaFile) error {
|
|
return s.uc.Repo().CreateMediaBatch(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 {
|
|
return nil, "", false, err
|
|
}
|
|
out := make([]map[string]any, 0, len(items))
|
|
for _, v := range items {
|
|
out = append(out, map[string]any{"name": v.Name, "key": v.Path, "url": v.URL, "size": v.Size})
|
|
}
|
|
return out, next, more, nil
|
|
}
|
|
func (s *MediaService) Categories(ctx context.Context) ([]map[string]any, error) {
|
|
items, err := s.uc.Repo().ListCategories(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]map[string]any, 0, len(items))
|
|
for _, v := range items {
|
|
out = append(out, categoryDTO(v))
|
|
}
|
|
return out, nil
|
|
}
|
|
func (s *MediaService) SaveCategory(ctx context.Context, v *biz.AttachmentCategory) error {
|
|
return s.uc.Repo().SaveCategory(ctx, v)
|
|
}
|
|
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)
|
|
}
|