feat(api): lab5 serve attachment variants in json and content endpoints
This commit is contained in:
@@ -65,4 +65,6 @@ type Attachment struct {
|
|||||||
LockedUntil *time.Time // lease; nil = не арендовано
|
LockedUntil *time.Time // lease; nil = не арендовано
|
||||||
PipelineVersion int // guard в CompleteJob; в БД default 1
|
PipelineVersion int // guard в CompleteJob; в БД default 1
|
||||||
StagingKey string // S3-ключ оригинала до 202; "" в lab4-пути
|
StagingKey string // S3-ключ оригинала до 202; "" в lab4-пути
|
||||||
|
|
||||||
|
Variants []AttachmentVariant
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,17 @@ type AttachmentResponse struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func AttachmentFromDomain(a *domain.Attachment) AttachmentResponse {
|
func AttachmentFromDomain(a *domain.Attachment) AttachmentResponse {
|
||||||
variants := make([]VariantResponse, 0)
|
variants := make([]VariantResponse, len(a.Variants))
|
||||||
|
for i, v := range a.Variants {
|
||||||
|
variants[i] = VariantResponse{
|
||||||
|
Name: v.Name,
|
||||||
|
ContentType: v.ContentType,
|
||||||
|
SizeBytes: v.SizeBytes,
|
||||||
|
Width: v.Width,
|
||||||
|
Height: v.Height,
|
||||||
|
SHA256: hex.EncodeToString(v.SHA256),
|
||||||
|
}
|
||||||
|
}
|
||||||
errCode := a.ErrorCode
|
errCode := a.ErrorCode
|
||||||
return AttachmentResponse{
|
return AttachmentResponse{
|
||||||
ID: a.ID,
|
ID: a.ID,
|
||||||
|
|||||||
@@ -82,12 +82,8 @@ func (h *AttachmentHandler) Content(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
variant := c.DefaultQuery("variant", "original")
|
variant := c.DefaultQuery("variant", "original")
|
||||||
if variant != "original" {
|
|
||||||
respondError(c, &domain.NotFoundError{Entity: "variant", ID: variant})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
rc, size, contentType, err := h.svc.Download(c.Request.Context(), me.ID, attachmentID)
|
rc, size, contentType, err := h.svc.Download(c.Request.Context(), me.ID, attachmentID, variant)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
respondError(c, err)
|
respondError(c, err)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -167,7 +167,7 @@ func (s *AttachmentService) Get(ctx context.Context, userID, attachmentID string
|
|||||||
|
|
||||||
func (s *AttachmentService) Download(
|
func (s *AttachmentService) Download(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
userID, attachmentID string,
|
userID, attachmentID, variantName string,
|
||||||
) (io.ReadCloser, int64, string, error) {
|
) (io.ReadCloser, int64, string, error) {
|
||||||
a, err := s.Get(ctx, userID, attachmentID)
|
a, err := s.Get(ctx, userID, attachmentID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -176,7 +176,18 @@ func (s *AttachmentService) Download(
|
|||||||
if a.Status != domain.AttachmentStatusReady {
|
if a.Status != domain.AttachmentStatusReady {
|
||||||
return nil, 0, "", &domain.ConflictError{Reason: "вложение не готово"}
|
return nil, 0, "", &domain.ConflictError{Reason: "вложение не готово"}
|
||||||
}
|
}
|
||||||
return s.storage.Get(ctx, a.ObjectKey)
|
|
||||||
|
if variantName == "original" || variantName == "" {
|
||||||
|
return s.storage.Get(ctx, a.ObjectKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range a.Variants {
|
||||||
|
if v.Name == variantName {
|
||||||
|
return s.storage.Get(ctx, v.ObjectKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, 0, "", &domain.NotFoundError{Entity: "variant", ID: variantName}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *AttachmentService) ensureMember(ctx context.Context, userID, chatID string) error {
|
func (s *AttachmentService) ensureMember(ctx context.Context, userID, chatID string) error {
|
||||||
|
|||||||
Reference in New Issue
Block a user