refactor(service): stream attachment uploads via temp file

This commit is contained in:
kk10
2026-11-03 14:24:07 +03:00
parent b19e64cc29
commit fbd9c56b1f
2 changed files with 38 additions and 28 deletions
+1 -14
View File
@@ -2,7 +2,6 @@ package handler
import (
"errors"
"io"
"net/http"
"github.com/go-chi/chi/v5"
@@ -47,19 +46,7 @@ func (h *AttachmentHandler) Upload(w http.ResponseWriter, r *http.Request) {
contentType := r.Header.Get("Content-Type")
r.Body = http.MaxBytesReader(w, r.Body, int64(h.maxBytes))
body, err := io.ReadAll(r.Body)
if err != nil {
var maxBytesErr *http.MaxBytesError
if errors.As(err, &maxBytesErr) {
httpx.WriteError(w, http.StatusRequestEntityTooLarge, dto.ErrorCodePayloadTooLarge, "file too large", requestID)
return
}
httpx.WriteError(w, http.StatusInternalServerError, dto.ErrorCodeInternalError, "read error", requestID)
return
}
att, err := h.service.Upload(r.Context(), userID, chatID, req.Filename, body, contentType)
att, err := h.service.Upload(r.Context(), userID, chatID, req.Filename, r.Body, contentType)
switch {
case errors.Is(err, service.ErrInvalidSize):
httpx.WriteError(w, http.StatusRequestEntityTooLarge, dto.ErrorCodePayloadTooLarge, "file too large", requestID)