refactor(service): stream attachment uploads via temp file
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user