refactor(service): stream attachment uploads via temp file
This commit is contained in:
@@ -2,7 +2,6 @@ package handler
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/go-chi/chi/v5"
|
"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")
|
contentType := r.Header.Get("Content-Type")
|
||||||
r.Body = http.MaxBytesReader(w, r.Body, int64(h.maxBytes))
|
r.Body = http.MaxBytesReader(w, r.Body, int64(h.maxBytes))
|
||||||
|
|
||||||
body, err := io.ReadAll(r.Body)
|
att, err := h.service.Upload(r.Context(), userID, chatID, req.Filename, r.Body, contentType)
|
||||||
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)
|
|
||||||
switch {
|
switch {
|
||||||
case errors.Is(err, service.ErrInvalidSize):
|
case errors.Is(err, service.ErrInvalidSize):
|
||||||
httpx.WriteError(w, http.StatusRequestEntityTooLarge, dto.ErrorCodePayloadTooLarge, "file too large", requestID)
|
httpx.WriteError(w, http.StatusRequestEntityTooLarge, dto.ErrorCodePayloadTooLarge, "file too large", requestID)
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
@@ -10,6 +9,8 @@ import (
|
|||||||
_ "image/jpeg"
|
_ "image/jpeg"
|
||||||
_ "image/png"
|
_ "image/png"
|
||||||
"io"
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
|
||||||
@@ -55,18 +56,39 @@ func (s *AttachmentService) Upload(
|
|||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
userID, chatID uuid.UUID,
|
userID, chatID uuid.UUID,
|
||||||
filename string,
|
filename string,
|
||||||
body []byte,
|
body io.Reader,
|
||||||
contentType string,
|
contentType string,
|
||||||
) (model.Attachment, error) {
|
) (model.Attachment, error) {
|
||||||
if len(body) > s.maxBytes {
|
|
||||||
return model.Attachment{}, ErrInvalidSize
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := s.ensureMember(ctx, userID, chatID); err != nil {
|
if err := s.ensureMember(ctx, userID, chatID); err != nil {
|
||||||
return model.Attachment{}, err
|
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 {
|
if err != nil {
|
||||||
return model.Attachment{}, err
|
return model.Attachment{}, err
|
||||||
}
|
}
|
||||||
@@ -76,12 +98,13 @@ func (s *AttachmentService) Upload(
|
|||||||
return model.Attachment{}, err
|
return model.Attachment{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
sum := sha256.Sum256(body)
|
hash := hex.EncodeToString(hasher.Sum(nil))
|
||||||
hash := hex.EncodeToString(sum[:])
|
|
||||||
|
|
||||||
key := "chats/" + chatID.String() + "/attachments/" + id.String()
|
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
|
return model.Attachment{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,7 +115,7 @@ func (s *AttachmentService) Upload(
|
|||||||
S3Key: key,
|
S3Key: key,
|
||||||
Filename: filename,
|
Filename: filename,
|
||||||
ContentType: db.AttachmentContentType(detectedType),
|
ContentType: db.AttachmentContentType(detectedType),
|
||||||
SizeBytes: len(body),
|
SizeBytes: int(size),
|
||||||
Sha256: hash,
|
Sha256: hash,
|
||||||
Status: db.AttachmentStatusReady,
|
Status: db.AttachmentStatusReady,
|
||||||
ErrorCode: "",
|
ErrorCode: "",
|
||||||
@@ -177,8 +200,8 @@ func (s *AttachmentService) GetContent(
|
|||||||
return data, model.ContentType(row.ContentType), nil
|
return data, model.ContentType(row.ContentType), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *AttachmentService) checkImage(data []byte, contentType string) (model.ContentType, error) {
|
func (s *AttachmentService) checkImage(r io.Reader, contentType string) (model.ContentType, error) {
|
||||||
cfg, format, err := image.DecodeConfig(bytes.NewReader(data))
|
cfg, format, err := image.DecodeConfig(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", ErrInvalidImage
|
return "", ErrInvalidImage
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user