forked from mtusi2026/mtusi2026lab4
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)
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
@@ -10,6 +9,8 @@ import (
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
@@ -55,18 +56,39 @@ func (s *AttachmentService) Upload(
|
||||
ctx context.Context,
|
||||
userID, chatID uuid.UUID,
|
||||
filename string,
|
||||
body []byte,
|
||||
body io.Reader,
|
||||
contentType string,
|
||||
) (model.Attachment, error) {
|
||||
if len(body) > s.maxBytes {
|
||||
return model.Attachment{}, ErrInvalidSize
|
||||
}
|
||||
|
||||
if err := s.ensureMember(ctx, userID, chatID); err != nil {
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
|
||||
detectedType, err := s.checkImage(body, contentType)
|
||||
tmp, err := os.CreateTemp("", "upload-*.tmp")
|
||||
if err != nil {
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
defer func() {
|
||||
_ = tmp.Close()
|
||||
_ = os.Remove(tmp.Name())
|
||||
}()
|
||||
|
||||
hasher := sha256.New()
|
||||
size, err := io.Copy(io.MultiWriter(tmp, hasher), body)
|
||||
if err != nil {
|
||||
var maxBytesErr *http.MaxBytesError
|
||||
if errors.As(err, &maxBytesErr) {
|
||||
return model.Attachment{}, ErrInvalidSize
|
||||
}
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
if size > int64(s.maxBytes) {
|
||||
return model.Attachment{}, ErrInvalidSize
|
||||
}
|
||||
|
||||
if _, err := tmp.Seek(0, io.SeekStart); err != nil {
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
detectedType, err := s.checkImage(tmp, contentType)
|
||||
if err != nil {
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
@@ -76,12 +98,13 @@ func (s *AttachmentService) Upload(
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
|
||||
sum := sha256.Sum256(body)
|
||||
hash := hex.EncodeToString(sum[:])
|
||||
|
||||
hash := hex.EncodeToString(hasher.Sum(nil))
|
||||
key := "chats/" + chatID.String() + "/attachments/" + id.String()
|
||||
|
||||
if err := s.s3.Put(ctx, key, bytes.NewReader(body), int64(len(body)), string(detectedType)); err != nil {
|
||||
if _, err := tmp.Seek(0, io.SeekStart); err != nil {
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
if err := s.s3.Put(ctx, key, tmp, size, string(detectedType)); err != nil {
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
|
||||
@@ -92,7 +115,7 @@ func (s *AttachmentService) Upload(
|
||||
S3Key: key,
|
||||
Filename: filename,
|
||||
ContentType: db.AttachmentContentType(detectedType),
|
||||
SizeBytes: len(body),
|
||||
SizeBytes: int(size),
|
||||
Sha256: hash,
|
||||
Status: db.AttachmentStatusReady,
|
||||
ErrorCode: "",
|
||||
@@ -177,8 +200,8 @@ func (s *AttachmentService) GetContent(
|
||||
return data, model.ContentType(row.ContentType), nil
|
||||
}
|
||||
|
||||
func (s *AttachmentService) checkImage(data []byte, contentType string) (model.ContentType, error) {
|
||||
cfg, format, err := image.DecodeConfig(bytes.NewReader(data))
|
||||
func (s *AttachmentService) checkImage(r io.Reader, contentType string) (model.ContentType, error) {
|
||||
cfg, format, err := image.DecodeConfig(r)
|
||||
if err != nil {
|
||||
return "", ErrInvalidImage
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user