package logic import ( "context" "spark-server/internal/module/upload/entity" "spark-server/internal/pkg/errs" "spark-server/internal/pkg/i18n" "spark-server/internal/pkg/reqctx" "spark-server/internal/pkg/validate" ) // DownloadInput requests a short-lived GET URL for an already-uploaded object key. type DownloadInput struct { ObjectKey string `json:"object_key" validate:"required,max=512" label:"field.object_key" errmsg:"required:validate.upload.key_required;max:validate.upload.key_invalid"` } func (s *Service) CreateDownload(ctx context.Context, userID int64, in *DownloadInput) (*entity.Download, error) { if !s.ready() { return nil, errs.ErrAvailable } if userID <= 0 { return nil, errs.ErrUnauthorized } if in == nil { return nil, errs.ErrInvalidArgument } if err := validate.StructLang(reqctx.Lang(ctx), in); err != nil { return nil, err } if !s.allow(userID) { return nil, errs.ErrRateLimited } key := in.ObjectKey if !entity.ValidObjectKey(key) { return nil, errs.ErrInvalidArgument.WithMsg(i18n.T(reqctx.Lang(ctx), i18n.KeyUploadKeyInvalid, nil)) } url, err := s.store.PresignGet(ctx, key, s.ttl) if err != nil { return nil, err } return &entity.Download{ ObjectKey: key, DownloadURL: url, Method: entity.MethodGET, ExpiresIn: s.expiresIn(), }, nil }