spark-server/internal/module/upload/logic/download.go
zhr ae9a71034d feat: implement Spark IM V3 modular monolith
Add HTTP /v1 MVC, TCP send path, groups with server-side member-avatar collage, object-storage upload, and friends/favorites around the architecture-v3 layout.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-17 19:52:11 +08:00

49 lines
1.3 KiB
Go

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
}