From aaf9a1e00a3f40738c5fcab786a0239862ae19c6 Mon Sep 17 00:00:00 2001 From: kk10 Date: Mon, 2 Nov 2026 23:28:51 +0300 Subject: [PATCH] fix(attachments): accept images and return after error responses --- db/queries/attachments.sql | 2 +- internal/db/attachments.sql.go | 2 +- internal/handler/attachment.go | 5 +++++ internal/service/attachment.go | 2 ++ 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/db/queries/attachments.sql b/db/queries/attachments.sql index 4c949a9..ef74562 100644 --- a/db/queries/attachments.sql +++ b/db/queries/attachments.sql @@ -21,7 +21,7 @@ SELECT sqlc.arg(size_bytes)::int, sqlc.arg(sha256)::text, sqlc.arg(status)::attachment_status, - sqlc.arg(error_code)::text + NULLIF(sqlc.arg(error_code)::text, '') WHERE EXISTS ( SELECT 1 FROM chat_members WHERE chat_id = sqlc.arg(chat_id) diff --git a/internal/db/attachments.sql.go b/internal/db/attachments.sql.go index c14e39d..8ba1cba 100644 --- a/internal/db/attachments.sql.go +++ b/internal/db/attachments.sql.go @@ -34,7 +34,7 @@ SELECT $7::int, $8::text, $9::attachment_status, - $10::text + NULLIF($10::text, '') WHERE EXISTS ( SELECT 1 FROM chat_members WHERE chat_id = $2 diff --git a/internal/handler/attachment.go b/internal/handler/attachment.go index 6c29dda..d9f1aa3 100644 --- a/internal/handler/attachment.go +++ b/internal/handler/attachment.go @@ -63,10 +63,13 @@ func (h *AttachmentHandler) Upload(w http.ResponseWriter, r *http.Request) { switch { case errors.Is(err, service.ErrInvalidSize): httpx.WriteError(w, http.StatusRequestEntityTooLarge, dto.ErrorCodeInvalidRequest, "file too large", requestID) + return case errors.Is(err, service.ErrInvalidImage): httpx.WriteError(w, http.StatusUnsupportedMediaType, dto.ErrorCodeInvalidRequest, "invalid image", requestID) + return case errors.Is(err, service.ErrAttachmentNotFound): httpx.WriteError(w, http.StatusNotFound, dto.ErrorCodeNotFound, "not found", requestID) + return case err != nil: httpx.WriteInternalError(w, requestID, err) return @@ -89,6 +92,7 @@ func (h *AttachmentHandler) Get(w http.ResponseWriter, r *http.Request) { switch { case errors.Is(err, service.ErrAttachmentNotFound): httpx.WriteError(w, http.StatusNotFound, dto.ErrorCodeNotFound, "not found", requestID) + return case err != nil: httpx.WriteInternalError(w, requestID, err) return @@ -111,6 +115,7 @@ func (h *AttachmentHandler) GetContent(w http.ResponseWriter, r *http.Request) { switch { case errors.Is(err, service.ErrAttachmentNotFound): httpx.WriteError(w, http.StatusNotFound, dto.ErrorCodeNotFound, "not found", requestID) + return case err != nil: httpx.WriteInternalError(w, requestID, err) return diff --git a/internal/service/attachment.go b/internal/service/attachment.go index 298ca38..2f62451 100644 --- a/internal/service/attachment.go +++ b/internal/service/attachment.go @@ -7,6 +7,8 @@ import ( "encoding/hex" "errors" "image" + _ "image/jpeg" + _ "image/png" "io" "github.com/google/uuid"