fix(attachment): check download variant and readiness
This commit is contained in:
@@ -14,6 +14,10 @@ type CreateAttachmentRequest struct {
|
|||||||
Filename string
|
Filename string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GetAttachmentContentRequest struct {
|
||||||
|
Variant string
|
||||||
|
}
|
||||||
|
|
||||||
type VariantResponse struct {
|
type VariantResponse struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
ContentType string `json:"content_type"`
|
ContentType string `json:"content_type"`
|
||||||
@@ -52,6 +56,10 @@ func (r CreateAttachmentRequest) Validate() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r GetAttachmentContentRequest) Validate() bool {
|
||||||
|
return r.Variant == "" || r.Variant == "original"
|
||||||
|
}
|
||||||
|
|
||||||
func ToAttachmentResponse(a model.Attachment) AttachmentResponse {
|
func ToAttachmentResponse(a model.Attachment) AttachmentResponse {
|
||||||
variants := make([]VariantResponse, 0)
|
variants := make([]VariantResponse, 0)
|
||||||
return AttachmentResponse{
|
return AttachmentResponse{
|
||||||
|
|||||||
@@ -113,8 +113,19 @@ func (h *AttachmentHandler) GetContent(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
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)
|
data, contentType, err := h.service.GetContent(r.Context(), userID, attachmentID)
|
||||||
switch {
|
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):
|
case errors.Is(err, service.ErrAttachmentNotFound):
|
||||||
httpx.WriteError(w, http.StatusNotFound, dto.ErrorCodeNotFound, "not found", requestID)
|
httpx.WriteError(w, http.StatusNotFound, dto.ErrorCodeNotFound, "not found", requestID)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
ErrAttachmentNotFound = errors.New("attachment not found")
|
ErrAttachmentNotFound = errors.New("attachment not found")
|
||||||
|
ErrAttachmentNotReady = errors.New("attachment not ready")
|
||||||
ErrInvalidImage = errors.New("invalid image")
|
ErrInvalidImage = errors.New("invalid image")
|
||||||
ErrInvalidSize = errors.New("invalid size")
|
ErrInvalidSize = errors.New("invalid size")
|
||||||
ErrUploadReadTimeout = errors.New("upload body read timed out")
|
ErrUploadReadTimeout = errors.New("upload body read timed out")
|
||||||
@@ -203,6 +204,10 @@ func (s *AttachmentService) GetContent(
|
|||||||
return nil, "", err
|
return nil, "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if model.AttachmentStatus(row.Status) != model.AttachmentStatusReady {
|
||||||
|
return nil, "", ErrAttachmentNotReady
|
||||||
|
}
|
||||||
|
|
||||||
out, err := s.s3.Get(ctx, row.S3Key)
|
out, err := s.s3.Get(ctx, row.S3Key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, "", err
|
return nil, "", err
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
ErrMessageChatNotFound = errors.New("chat not found")
|
ErrMessageChatNotFound = errors.New("chat not found")
|
||||||
ErrAttachmentNotReady = errors.New("attachment not ready")
|
|
||||||
ErrIdempotencyKeyConflict = errors.New("idempotency key reused with different request")
|
ErrIdempotencyKeyConflict = errors.New("idempotency key reused with different request")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user