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 maxPendingJobs int jobMaxAttempts int uploadTimeout time.Duration downloadTimeout time.Duration tempDir string } func NewHandler(st *store.Store, client *objectstore.Client, validator *Validator, maxUploadBytes, drainLimit int64, maxPendingJobs, jobMaxAttempts int, uploadTimeout, downloadTimeout time.Duration, tempDir string) *Handler { return &Handler{ store: st, objects: client, validator: validator, maxUploadBytes: maxUploadBytes, drainLimit: drainLimit, maxPendingJobs: maxPendingJobs, jobMaxAttempts: jobMaxAttempts, uploadTimeout: uploadTimeout, downloadTimeout: downloadTimeout, tempDir: tempDir, } } // Upload stages the original, then commits the attachment snapshot and its // outbox job together. The 202 is therefore a promise about durable bytes plus // durable intent, not a promise that resizing already happened. 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") } // Backpressure: refuse new work before reading its bytes. The check is // approximate across API replicas but bounds staging growth under overload. if h.maxPendingJobs > 0 { pending, countErr := h.store.CountPendingJobs(c.Context()) if countErr != nil { return countErr } if pending >= h.maxPendingJobs { c.Set(fiber.HeaderRetryAfter, "5") return common.Unavailable("processing queue is full") } } // Reject an obviously oversized request before allocating a temp file or // decoding anything. Chunked requests are caught by the streaming counter // below. // // 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) }() // 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() jobID := 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: "queued", Variants: []model.Variant{}, ErrorCode: nil, CreatedAt: common.NowUTC(), UpdatedAt: common.NowUTC(), // AGENT: keys are server-generated from UUIDs, never the client // filename. staging/ is the durable handoff prefix; the worker copies // the same bytes to the final attachments/ key unchanged. ObjectKey: objectKey(chatID, attachmentID), StagingKey: stagingKey(chatID, attachmentID), PipelineVersion: 1, } if _, err = file.Seek(0, 0); err != nil { return common.BadRequest("cannot read upload body") } if err = h.objects.Put(ctx, attachment.StagingKey, file, written, attachment.ContentType); err != nil { slog.Error("stage attachment object", "err", err, "attachment_id", attachment.ID) return common.Unavailable("object storage unavailable") } job := model.JobRecord{ Job: model.Job{ JobID: jobID, AttachmentID: attachmentID, PipelineVersion: attachment.PipelineVersion, CreatedAt: attachment.CreatedAt, }, MaxAttempts: h.jobMaxAttempts, } if err = h.store.CreateQueuedAttachment(ctx, attachment, job); err != nil { h.compensateStaging(attachment.StagingKey) return err } return c.Status(fiber.StatusAccepted).JSON(attachment) } // compensateStaging is best effort. If it fails, the reconciler removes an // object with no metadata row after the grace period. func (h *Handler) compensateStaging(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 staging object", "err", err) } } 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 } // Queued/processing snapshots; the client polls this // endpoint until status becomes ready or failed. 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" } switch variant { case "original", "thumbnail", "crop", "optimized": default: 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") } key := attachment.ObjectKey contentType := attachment.ContentType sizeBytes := attachment.SizeBytes if variant != "original" { found := false for _, candidate := range attachment.Variants { if candidate.Name == variant { key = store.VariantObjectKey(attachment.ObjectKey, candidate.Name) contentType = candidate.ContentType sizeBytes = candidate.SizeBytes found = true break } } if !found { return common.NotFound("attachment variant not found") } } ctx, cancel := context.WithTimeout(c.Context(), h.downloadTimeout) body, size, err := h.objects.Get(ctx, key) 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 != sizeBytes { _ = body.Close() cancel() slog.Error("attachment object size mismatch", "attachment_id", attachment.ID, "metadata_size", sizeBytes, "object_size", size) return common.Unavailable("attachment content is inconsistent") } c.Set(fiber.HeaderContentType, contentType) c.Set(fiber.HeaderContentLength, strconv.FormatInt(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) } func stagingKey(chatID, attachmentID uuid.UUID) string { return fmt.Sprintf("staging/%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()) } }