48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
msgentity "spark-server/internal/module/message/entity"
|
|
uploadentity "spark-server/internal/module/upload/entity"
|
|
"spark-server/internal/pkg/errs"
|
|
"spark-server/internal/pkg/reqctx"
|
|
)
|
|
|
|
func (s *Service) copyMedia(ctx context.Context, payload *msgentity.Content) ([]string, error) {
|
|
keys := payload.ObjectKeys()
|
|
if len(keys) == 0 {
|
|
return nil, nil
|
|
}
|
|
if s.objects == nil {
|
|
return nil, errs.ErrAvailable
|
|
}
|
|
day := time.Now().UTC().Format("20060102")
|
|
mapping := make(map[string]string, len(keys))
|
|
copied := make([]string, 0, len(keys))
|
|
for _, src := range keys {
|
|
if _, ok := mapping[src]; ok {
|
|
continue
|
|
}
|
|
dst := uploadentity.BuildFavKey(reqctx.NewObjectName(), day, uploadentity.ExtOfKey(src))
|
|
if err := s.objects.Copy(ctx, src, dst); err != nil {
|
|
s.deleteKeys(ctx, copied)
|
|
return nil, err
|
|
}
|
|
mapping[src] = dst
|
|
copied = append(copied, dst)
|
|
}
|
|
payload.ReplaceObjectKeys(mapping)
|
|
return copied, nil
|
|
}
|
|
|
|
func (s *Service) deleteKeys(ctx context.Context, keys []string) {
|
|
if s.objects == nil {
|
|
return
|
|
}
|
|
for _, key := range keys {
|
|
_ = s.objects.Delete(ctx, key)
|
|
}
|
|
}
|