diff --git a/internal/dto/attachment.go b/internal/dto/attachment.go index b635f91..996835f 100644 --- a/internal/dto/attachment.go +++ b/internal/dto/attachment.go @@ -14,6 +14,10 @@ type CreateAttachmentRequest struct { Filename string } +type GetAttachmentContentRequest struct { + Variant string +} + type VariantResponse struct { Name string `json:"name"` ContentType string `json:"content_type"` @@ -52,6 +56,10 @@ func (r CreateAttachmentRequest) Validate() bool { return true } +func (r GetAttachmentContentRequest) Validate() bool { + return r.Variant == "" || r.Variant == "original" +} + func ToAttachmentResponse(a model.Attachment) AttachmentResponse { variants := make([]VariantResponse, 0) return AttachmentResponse{ diff --git a/internal/handler/attachment.go b/internal/handler/attachment.go index 5c84400..5470d7b 100644 --- a/internal/handler/attachment.go +++ b/internal/handler/attachment.go @@ -113,8 +113,19 @@ func (h *AttachmentHandler) GetContent(w http.ResponseWriter, r *http.Request) { return } + req := dto.GetAttachmentContentRequest{ + Variant: r.URL.Query().Get("variant"), + } + if !req.Validate() { + httpx.WriteError(w, http.StatusNotFound, dto.ErrorCodeNotFound, "not found", requestID) + return + } + data, contentType, err := h.service.GetContent(r.Context(), userID, attachmentID) switch { + case errors.Is(err, service.ErrAttachmentNotReady): + httpx.WriteError(w, http.StatusConflict, dto.ErrorCodeConflict, "attachment not ready", requestID) + return case errors.Is(err, service.ErrAttachmentNotFound): httpx.WriteError(w, http.StatusNotFound, dto.ErrorCodeNotFound, "not found", requestID) return diff --git a/internal/service/attachment.go b/internal/service/attachment.go index 0958953..63112c9 100644 --- a/internal/service/attachment.go +++ b/internal/service/attachment.go @@ -23,6 +23,7 @@ import ( var ( ErrAttachmentNotFound = errors.New("attachment not found") + ErrAttachmentNotReady = errors.New("attachment not ready") ErrInvalidImage = errors.New("invalid image") ErrInvalidSize = errors.New("invalid size") ErrUploadReadTimeout = errors.New("upload body read timed out") @@ -203,6 +204,10 @@ func (s *AttachmentService) GetContent( return nil, "", err } + if model.AttachmentStatus(row.Status) != model.AttachmentStatusReady { + return nil, "", ErrAttachmentNotReady + } + out, err := s.s3.Get(ctx, row.S3Key) if err != nil { return nil, "", err diff --git a/internal/service/message.go b/internal/service/message.go index 79b830c..a6316e3 100644 --- a/internal/service/message.go +++ b/internal/service/message.go @@ -20,7 +20,6 @@ import ( var ( ErrMessageChatNotFound = errors.New("chat not found") - ErrAttachmentNotReady = errors.New("attachment not ready") ErrIdempotencyKeyConflict = errors.New("idempotency key reused with different request") )