forked from mtusi2026/mtusi2026lab4
330 lines
11 KiB
Go
330 lines
11 KiB
Go
package attachments
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"mime"
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
|
|
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/common"
|
|
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/model"
|
|
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/objectstore"
|
|
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/store"
|
|
"github.com/gofiber/fiber/v3"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const copyBufferSize = 32 * 1024
|
|
|
|
type Handler struct {
|
|
store *store.Store
|
|
objects *objectstore.Client
|
|
validator *Validator
|
|
maxUploadBytes int64
|
|
drainLimit int64
|
|
uploadTimeout time.Duration
|
|
downloadTimeout time.Duration
|
|
tempDir string
|
|
}
|
|
|
|
func NewHandler(st *store.Store, client *objectstore.Client, validator *Validator, maxUploadBytes, drainLimit int64, uploadTimeout, downloadTimeout time.Duration, tempDir string) *Handler {
|
|
return &Handler{
|
|
store: st,
|
|
objects: client,
|
|
validator: validator,
|
|
maxUploadBytes: maxUploadBytes,
|
|
drainLimit: drainLimit,
|
|
uploadTimeout: uploadTimeout,
|
|
downloadTimeout: downloadTimeout,
|
|
tempDir: tempDir,
|
|
}
|
|
}
|
|
|
|
// Upload stores raw PNG/JPEG bytes. The order is deliberate:
|
|
// 1. authorize before reading the body;
|
|
// 2. stream the body to a bounded temporary file while hashing;
|
|
// 3. validate pixels and decode;
|
|
// 4. commit a pending metadata row;
|
|
// 5. PUT the object;
|
|
// 6. mark the row ready.
|
|
//
|
|
// A 201 is therefore returned only after both the object and its metadata are
|
|
// durable. Every failure between 4 and 6 has a compensation path, and the
|
|
// reconciler covers crashes that prevent compensation from running.
|
|
func (h *Handler) Upload(c fiber.Ctx) error {
|
|
userID := common.MustUserID(c)
|
|
|
|
chatID, err := common.PathUUID(c, "chat_id")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Authorization precedes any body I/O: an outsider cannot make the server
|
|
// read a large upload before learning that the chat is not theirs.
|
|
isMember, err := h.store.IsMember(c.Context(), userID, chatID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !isMember {
|
|
return common.NotFound("chat not found")
|
|
}
|
|
|
|
filename := c.Query("filename")
|
|
if err := common.ValidateFilename(filename); err != nil {
|
|
return err
|
|
}
|
|
|
|
contentType, _, err := mime.ParseMediaType(c.Get(fiber.HeaderContentType))
|
|
if err != nil || (contentType != "image/png" && contentType != "image/jpeg") {
|
|
return common.UnsupportedMedia("Content-Type must be image/png or image/jpeg")
|
|
}
|
|
|
|
// Reject an obviously oversized request before allocating a temp file or
|
|
// decoding anything. Chunked requests are caught by the streaming counter
|
|
// below.
|
|
//
|
|
// AGENT: the declared body is drained first, but only within the bounded
|
|
// HTTP envelope (upload limit plus the JSON body slack). If the server
|
|
// closed immediately, the client would often see a connection reset instead
|
|
// of the required 413 while it was still sending. Draining is bounded and
|
|
// uses a 32 KiB buffer, so it does not weaken the memory guarantee.
|
|
if rawLength := c.Get(fiber.HeaderContentLength); rawLength != "" {
|
|
declared, parseErr := strconv.ParseInt(rawLength, 10, 64)
|
|
if parseErr != nil || declared < 0 {
|
|
return common.BadRequest("invalid Content-Length")
|
|
}
|
|
if declared > h.maxUploadBytes {
|
|
if declared <= h.drainLimit {
|
|
_, _ = io.CopyBuffer(io.Discard, io.LimitReader(requestBody(c), declared), make([]byte, copyBufferSize))
|
|
}
|
|
return common.PayloadTooLarge("upload exceeds the byte limit")
|
|
}
|
|
}
|
|
|
|
if err = os.MkdirAll(h.tempDir, 0o700); err != nil {
|
|
slog.Error("create upload temp dir", "err", err)
|
|
return common.Internal()
|
|
}
|
|
file, err := os.CreateTemp(h.tempDir, "upload-*.part")
|
|
if err != nil {
|
|
slog.Error("create upload temp file", "err", err)
|
|
return common.Internal()
|
|
}
|
|
filePath := file.Name()
|
|
defer func() {
|
|
_ = file.Close()
|
|
_ = os.Remove(filePath)
|
|
}()
|
|
|
|
// AGENT: the 32 KiB copy buffer is the only request-sized allocation here.
|
|
// The body is never read with io.ReadAll, so ten simultaneous 10 MiB
|
|
// uploads do not require ten 10 MiB Go slices.
|
|
var reader io.Reader = requestBody(c)
|
|
hasher := sha256.New()
|
|
copyBuffer := make([]byte, copyBufferSize)
|
|
written, err := io.CopyBuffer(io.MultiWriter(file, hasher), io.LimitReader(reader, h.maxUploadBytes+1), copyBuffer)
|
|
if err != nil {
|
|
return common.BadRequest("incomplete upload body")
|
|
}
|
|
if written == 0 {
|
|
return common.UnsupportedMedia("upload body is empty")
|
|
}
|
|
if written > h.maxUploadBytes {
|
|
return common.PayloadTooLarge("upload exceeds the byte limit")
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(c.Context(), h.uploadTimeout)
|
|
defer cancel()
|
|
|
|
info, err := h.validator.Validate(ctx, file, contentType)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
attachmentID := uuid.New()
|
|
attachment := model.Attachment{
|
|
ID: attachmentID,
|
|
ChatID: chatID,
|
|
OwnerID: userID,
|
|
Filename: filename,
|
|
ContentType: info.ContentType,
|
|
SizeBytes: written,
|
|
SHA256: hex.EncodeToString(hasher.Sum(nil)),
|
|
Status: "pending",
|
|
Variants: []model.Variant{},
|
|
ErrorCode: nil,
|
|
CreatedAt: common.NowUTC(),
|
|
UpdatedAt: common.NowUTC(),
|
|
// AGENT: the key is server-generated from UUIDs and never contains the
|
|
// client filename. That keeps traversal and collisions out of the
|
|
// object store even before application authorization is considered.
|
|
ObjectKey: objectKey(chatID, attachmentID),
|
|
}
|
|
|
|
if err = h.store.CreatePendingAttachment(ctx, attachment); err != nil {
|
|
return err
|
|
}
|
|
|
|
if _, err = file.Seek(0, 0); err != nil {
|
|
h.compensate(attachment.ID, attachment.ObjectKey)
|
|
return common.BadRequest("cannot read upload body")
|
|
}
|
|
if err = h.objects.Put(ctx, attachment.ObjectKey, file, written, attachment.ContentType); err != nil {
|
|
slog.Error("store attachment object", "err", err, "attachment_id", attachment.ID)
|
|
h.compensate(attachment.ID, attachment.ObjectKey)
|
|
return common.Unavailable("object storage unavailable")
|
|
}
|
|
|
|
updatedAt, err := h.store.MarkAttachmentReady(ctx, attachment.ID)
|
|
if err != nil {
|
|
slog.Error("mark attachment ready", "err", err, "attachment_id", attachment.ID)
|
|
h.compensate(attachment.ID, attachment.ObjectKey)
|
|
return common.Unavailable("attachment metadata unavailable")
|
|
}
|
|
|
|
attachment.Status = "ready"
|
|
attachment.UpdatedAt = common.RFCFormTime(updatedAt)
|
|
return c.Status(fiber.StatusCreated).JSON(attachment)
|
|
}
|
|
|
|
// compensate is best effort by design. If either delete fails, the pending row
|
|
// or the object remains visible to the reconciler, which retries after the
|
|
// configured grace period.
|
|
func (h *Handler) compensate(attachmentID uuid.UUID, key string) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
if err := h.objects.Delete(ctx, key); err != nil && !errors.Is(err, objectstore.ErrNotFound) {
|
|
slog.Error("compensate: delete object", "err", err, "attachment_id", attachmentID)
|
|
}
|
|
if err := h.store.DeletePendingAttachment(ctx, attachmentID); err != nil {
|
|
slog.Error("compensate: delete pending row", "err", err, "attachment_id", attachmentID)
|
|
}
|
|
}
|
|
|
|
func (h *Handler) Get(c fiber.Ctx) error {
|
|
userID := common.MustUserID(c)
|
|
|
|
attachmentID, err := common.PathUUID(c, "attachment_id")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
attachment, err := h.store.GetAttachmentForUser(c.Context(), userID, attachmentID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if attachment.Status != "ready" {
|
|
return common.Conflict("attachment is not ready")
|
|
}
|
|
|
|
return c.JSON(attachment)
|
|
}
|
|
|
|
func (h *Handler) Download(c fiber.Ctx) error {
|
|
userID := common.MustUserID(c)
|
|
|
|
attachmentID, err := common.PathUUID(c, "attachment_id")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
variant := c.Query("variant", "original")
|
|
if variant == "" {
|
|
variant = "original"
|
|
}
|
|
if variant != "original" {
|
|
return common.BadRequest("unsupported variant")
|
|
}
|
|
|
|
attachment, err := h.store.GetAttachmentForUser(c.Context(), userID, attachmentID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if attachment.Status != "ready" {
|
|
return common.Conflict("attachment is not ready")
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(c.Context(), h.downloadTimeout)
|
|
|
|
body, size, err := h.objects.Get(ctx, attachment.ObjectKey)
|
|
if err != nil {
|
|
cancel()
|
|
if errors.Is(err, objectstore.ErrNotFound) {
|
|
slog.Error("attachment object missing", "attachment_id", attachment.ID)
|
|
return common.NotFound("attachment content not found")
|
|
}
|
|
slog.Error("read attachment object", "err", err, "attachment_id", attachment.ID)
|
|
return common.Unavailable("object storage unavailable")
|
|
}
|
|
if size != attachment.SizeBytes {
|
|
_ = body.Close()
|
|
cancel()
|
|
slog.Error("attachment object size mismatch",
|
|
"attachment_id", attachment.ID, "metadata_size", attachment.SizeBytes, "object_size", size)
|
|
return common.Unavailable("attachment content is inconsistent")
|
|
}
|
|
|
|
c.Set(fiber.HeaderContentType, attachment.ContentType)
|
|
c.Set(fiber.HeaderContentLength, strconv.FormatInt(attachment.SizeBytes, 10))
|
|
c.Set(fiber.HeaderContentDisposition, mime.FormatMediaType("inline", map[string]string{"filename": attachment.Filename}))
|
|
c.Set(fiber.HeaderCacheControl, "private, no-store")
|
|
c.Set("X-Content-Type-Options", "nosniff")
|
|
|
|
// AGENT: SendStreamWriter copies the S3 body straight to the response. The
|
|
// API never materializes the whole object in RAM and never redirects the
|
|
// client to a bucket URL, so authorization is enforced on every byte.
|
|
//
|
|
// The context and body are released inside the callback, not by a defer in
|
|
// this handler: the handler returns before fasthttp copies the stream, and
|
|
// canceling the S3 request context earlier truncates the response.
|
|
return c.SendStreamWriter(func(writer *bufio.Writer) {
|
|
defer cancel()
|
|
defer func() { _ = body.Close() }()
|
|
if _, copyErr := io.Copy(writer, body); copyErr != nil {
|
|
slog.Error("stream attachment to client", "err", copyErr, "attachment_id", attachment.ID)
|
|
}
|
|
})
|
|
}
|
|
|
|
func requestBody(c fiber.Ctx) io.Reader {
|
|
if reader := c.Request().BodyStream(); reader != nil {
|
|
return reader
|
|
}
|
|
return bytes.NewReader(c.Body())
|
|
}
|
|
|
|
func objectKey(chatID, attachmentID uuid.UUID) string {
|
|
return fmt.Sprintf("attachments/%s/%s", chatID, attachmentID)
|
|
}
|
|
|
|
// CleanupStaleUploads removes temp files abandoned by a previous crash. A
|
|
// running request removes its own file in a defer; this only handles the
|
|
// process-died-mid-upload case.
|
|
func CleanupStaleUploads(dir string, olderThan time.Duration) {
|
|
entries, err := os.ReadDir(dir)
|
|
if err != nil {
|
|
return
|
|
}
|
|
cutoff := time.Now().Add(-olderThan)
|
|
for _, entry := range entries {
|
|
if entry.IsDir() {
|
|
continue
|
|
}
|
|
info, infoErr := entry.Info()
|
|
if infoErr != nil || info.ModTime().After(cutoff) {
|
|
continue
|
|
}
|
|
_ = os.Remove(dir + string(os.PathSeparator) + entry.Name())
|
|
}
|
|
}
|