106 lines
3.9 KiB
Go
106 lines
3.9 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
|
|
"kra/internal/biz"
|
|
"kra/internal/conf"
|
|
"kra/internal/service/dto"
|
|
)
|
|
|
|
type MediaService struct {
|
|
uc *biz.MediaUsecase
|
|
runtime *conf.Runtime
|
|
}
|
|
|
|
func NewMediaService(uc *biz.MediaUsecase, runtime *conf.Runtime) *MediaService {
|
|
return &MediaService{uc: uc, runtime: runtime}
|
|
}
|
|
func (s *MediaService) MediaConfig() *conf.AdminBackend_Media {
|
|
if config := s.runtime.Admin(); config != nil {
|
|
return config.Media
|
|
}
|
|
return nil
|
|
}
|
|
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.FindMedia(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return mediaDTO(v), nil
|
|
}
|
|
func (s *MediaService) MediaList(ctx context.Context, filter biz.MediaFilter) ([]map[string]any, int64, error) {
|
|
items, total, err := s.uc.ListMedia(ctx, filter)
|
|
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.UpdateMediaName(ctx, id, name)
|
|
}
|
|
func (s *MediaService) ImportURLs(ctx context.Context, items []*biz.MediaFile) error {
|
|
return s.uc.CreateMediaBatch(ctx, items)
|
|
}
|
|
func (s *MediaService) ImportURLRequests(ctx context.Context, 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: value.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 {
|
|
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.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.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.DeleteCategory(ctx, id)
|
|
}
|