@@ -36,7 +36,7 @@ type natsPublisher interface {
3636}
3737
3838type attachmentUploader interface {
39- Upload (ctx context.Context , traceID string , attachments []domain.AttachmentDO ) ([]domain.AttachmentDO , error )
39+ Upload (ctx context.Context , traceID string , attachments []domain.Attachment ) ([]domain.AttachmentDO , error )
4040}
4141
4242const (
@@ -72,9 +72,16 @@ func (h *Handler) handleSend(w http.ResponseWriter, r *http.Request) {
7272 traceID := uuid .New ().String ()
7373 ctx := r .Context ()
7474
75+ r .Body = http .MaxBytesReader (w , r .Body , h .cfg .MaxBodySize )
7576 var req domain.MailRequest
7677 if err := json .NewDecoder (r .Body ).Decode (& req ); err != nil {
77- writeError (w , http .StatusBadRequest , domain .ErrJSONParseError , "invalid JSON body" , traceID )
78+ var maxBytesErr * http.MaxBytesError
79+ if errors .As (err , & maxBytesErr ) {
80+ writeError (w , http .StatusRequestEntityTooLarge , domain .ErrBodyTooLarge ,
81+ fmt .Sprintf ("request body exceeds limit of %d bytes" , h .cfg .MaxBodySize ), traceID )
82+ } else {
83+ writeError (w , http .StatusBadRequest , domain .ErrJSONParseError , "invalid JSON body" , traceID )
84+ }
7885 return
7986 }
8087
@@ -119,14 +126,10 @@ func (h *Handler) handleSend(w http.ResponseWriter, r *http.Request) {
119126 return
120127 }
121128
122- // Decode attachments and upload to Object Store
123- attachments , err := decodeAttachments (req .Attachments )
124- if err != nil {
125- h .writeValidationError (w , err , traceID )
126- return
127- }
128- if len (attachments ) > 0 {
129- attachments , err = h .attStore .Upload (ctx , traceID , attachments )
129+ // Stage 6: upload attachments to Object Store (streaming base64 decode)
130+ var attachmentDOs []domain.AttachmentDO
131+ if len (req .Attachments ) > 0 {
132+ attachmentDOs , err = h .attStore .Upload (ctx , traceID , req .Attachments )
130133 if err != nil {
131134 slog .ErrorContext (ctx , "attachment upload failed" ,
132135 slog .String ("traceId" , traceID ),
@@ -148,7 +151,7 @@ func (h *Handler) handleSend(w http.ResponseWriter, r *http.Request) {
148151 Subject : req .Subject ,
149152 BodyContent : req .BodyContent ,
150153 HtmlBodyContent : req .HtmlBodyContent ,
151- Attachments : attachments ,
154+ Attachments : attachmentDOs ,
152155 TraceContext : req .TraceContext ,
153156 Test : sender .Test ,
154157 }
0 commit comments