chore(migrate) moved codebase from lab4

This commit is contained in:
Mikhail Verkovykh
2026-11-14 23:17:36 +03:00
parent 276dbdad8a
commit 28d68dc30b
123 changed files with 11677 additions and 0 deletions
+329
View File
@@ -0,0 +1,329 @@
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())
}
}
+129
View File
@@ -0,0 +1,129 @@
// Package attachments implements the synchronous upload, metadata lookup and
// authorized download path for chat attachments.
package attachments
import (
"context"
"errors"
"image"
_ "image/jpeg"
_ "image/png"
"io"
"mime"
"net/http"
"os"
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/common"
"golang.org/x/sync/semaphore"
)
type Validator struct {
maxPixels int64
decodeBudget *semaphore.Weighted
budgetBytes int64
}
type ImageInfo struct {
ContentType string
Width int
Height int
}
func NewValidator(maxPixels, decodeBudgetBytes int64) *Validator {
return &Validator{
maxPixels: maxPixels,
decodeBudget: semaphore.NewWeighted(decodeBudgetBytes),
budgetBytes: decodeBudgetBytes,
}
}
// Validate checks the declared media type against the real bytes and then
// decodes the image under a shared memory budget. The decode is what turns a
// structurally broken or truncated file into 415 instead of a saved object.
//
// AGENT: image.DecodeConfig first reads only the header. This is the important
// ordering: a decompression bomb is rejected from width×height before the
// decoder is allowed to allocate pixel memory.
func (v *Validator) Validate(ctx context.Context, file *os.File, declared string) (ImageInfo, error) {
mediaType, _, err := mime.ParseMediaType(declared)
if err != nil {
return ImageInfo{}, common.UnsupportedMedia("Content-Type must be image/png or image/jpeg")
}
if mediaType != "image/png" && mediaType != "image/jpeg" {
return ImageInfo{}, common.UnsupportedMedia("Content-Type must be image/png or image/jpeg")
}
if _, err = file.Seek(0, 0); err != nil {
return ImageInfo{}, common.BadRequest("cannot read upload body")
}
head := make([]byte, 512)
n, readErr := file.Read(head)
if readErr != nil && !errors.Is(readErr, io.EOF) {
return ImageInfo{}, common.BadRequest("cannot read upload body")
}
detected := http.DetectContentType(head[:n])
if detected != mediaType {
return ImageInfo{}, common.UnsupportedMedia("file content does not match Content-Type")
}
if _, err = file.Seek(0, 0); err != nil {
return ImageInfo{}, common.BadRequest("cannot read upload body")
}
config, format, err := image.DecodeConfig(file)
if err != nil {
return ImageInfo{}, common.UnsupportedMedia("invalid or corrupted image")
}
if format != "png" && format != "jpeg" {
return ImageInfo{}, common.UnsupportedMedia("only PNG and JPEG images are accepted")
}
if (format == "png" && mediaType != "image/png") || (format == "jpeg" && mediaType != "image/jpeg") {
return ImageInfo{}, common.UnsupportedMedia("file content does not match Content-Type")
}
if config.Width <= 0 || config.Height <= 0 {
return ImageInfo{}, common.UnsupportedMedia("invalid or corrupted image")
}
pixels := int64(config.Width) * int64(config.Height)
if pixels <= 0 || pixels > v.maxPixels {
return ImageInfo{}, common.PayloadTooLarge("image has too many pixels")
}
// Approximate decoded RGBA size. The semaphore caps the sum across
// concurrent requests; a single 40 Mp image needs ~160 MiB, so a lower
// configured budget is a deliberate operator choice and becomes a 413.
weight := pixels * 4
if weight > v.budgetBytes {
return ImageInfo{}, common.PayloadTooLarge("image is too large to validate")
}
if err = v.decodeBudget.Acquire(ctx, weight); err != nil {
return ImageInfo{}, common.Unavailable("image validation is busy")
}
defer v.decodeBudget.Release(weight)
if _, err = file.Seek(0, 0); err != nil {
return ImageInfo{}, common.BadRequest("cannot read upload body")
}
info, decodeErr := safeDecode(file)
if decodeErr != nil {
return ImageInfo{}, common.UnsupportedMedia("invalid or corrupted image")
}
bounds := info.Bounds()
if bounds.Dx() != config.Width || bounds.Dy() != config.Height {
return ImageInfo{}, common.UnsupportedMedia("invalid or corrupted image")
}
return ImageInfo{ContentType: mediaType, Width: config.Width, Height: config.Height}, nil
}
// safeDecode converts a decoder panic into an ordinary error. A malformed
// client file must never crash the process or become a 500.
func safeDecode(file *os.File) (img image.Image, err error) {
defer func() {
if recovered := recover(); recovered != nil {
img = nil
err = errors.New("image decoder panic")
}
}()
img, _, err = image.Decode(file)
return img, err
}
+134
View File
@@ -0,0 +1,134 @@
package attachments
import (
"bytes"
"context"
"image"
"image/color"
"image/jpeg"
"image/png"
"os"
"path/filepath"
"testing"
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/common"
)
// writeTemp stores raw bytes in a temporary file and returns its path. The
// validator works on *os.File so it can seek, sniff and decode the same body
// without loading it into memory twice.
func writeTemp(t *testing.T, name string, data []byte) *os.File {
t.Helper()
path := filepath.Join(t.TempDir(), name)
if err := os.WriteFile(path, data, 0o600); err != nil {
t.Fatalf("write %s: %v", path, err)
}
file, err := os.Open(path)
if err != nil {
t.Fatalf("open %s: %v", path, err)
}
t.Cleanup(func() { _ = file.Close() })
return file
}
func encodePNG(t *testing.T, width, height int) []byte {
t.Helper()
img := image.NewRGBA(image.Rect(0, 0, width, height))
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
img.Set(x, y, color.RGBA{R: uint8(x), G: uint8(y), B: 0x40, A: 0xff})
}
}
var buf bytes.Buffer
if err := png.Encode(&buf, img); err != nil {
t.Fatalf("encode png: %v", err)
}
return buf.Bytes()
}
func TestValidateAcceptsPNGAndJPEG(t *testing.T) {
validator := NewValidator(40_000_000, 256<<20)
ctx := context.Background()
pngFile := writeTemp(t, "image.png", encodePNG(t, 8, 6))
info, err := validator.Validate(ctx, pngFile, "image/png")
if err != nil {
t.Fatalf("valid png rejected: %v", err)
}
if info.ContentType != "image/png" || info.Width != 8 || info.Height != 6 {
t.Fatalf("unexpected png info: %+v", info)
}
img := image.NewRGBA(image.Rect(0, 0, 4, 3))
var buf bytes.Buffer
if err = jpeg.Encode(&buf, img, nil); err != nil {
t.Fatalf("encode jpeg: %v", err)
}
jpegFile := writeTemp(t, "image.jpg", buf.Bytes())
info, err = validator.Validate(ctx, jpegFile, "image/jpeg; charset=binary")
if err != nil {
t.Fatalf("valid jpeg rejected: %v", err)
}
if info.ContentType != "image/jpeg" || info.Width != 4 || info.Height != 3 {
t.Fatalf("unexpected jpeg info: %+v", info)
}
}
func TestValidateRejectsMismatchedOrBrokenContent(t *testing.T) {
validator := NewValidator(40_000_000, 256<<20)
ctx := context.Background()
pngBytes := encodePNG(t, 8, 6)
cases := []struct {
name string
data []byte
declared string
code string
}{
{"png declared as jpeg", pngBytes, "image/jpeg", common.ErrorCodeUnsupportedMedia},
{"text declared as png", []byte("this is not an image at all"), "image/png", common.ErrorCodeUnsupportedMedia},
{"unsupported declared type", pngBytes, "image/gif", common.ErrorCodeUnsupportedMedia},
{"garbage declared type", pngBytes, "not a media type", common.ErrorCodeUnsupportedMedia},
{"truncated png", pngBytes[:len(pngBytes)/2], "image/png", common.ErrorCodeUnsupportedMedia},
{"empty body", nil, "image/png", common.ErrorCodeUnsupportedMedia},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
file := writeTemp(t, "upload", tc.data)
if _, err := validator.Validate(ctx, file, tc.declared); !common.IsCode(err, tc.code) {
t.Fatalf("Validate(%q) = %v, want code %s", tc.name, err, tc.code)
}
})
}
}
func TestValidateRejectsTooManyPixelsBeforeDecoding(t *testing.T) {
validator := NewValidator(100, 256<<20)
file := writeTemp(t, "big.png", encodePNG(t, 20, 20))
_, err := validator.Validate(context.Background(), file, "image/png")
if !common.IsCode(err, common.ErrorCodePayloadTooLarge) {
t.Fatalf("Validate = %v, want %s", err, common.ErrorCodePayloadTooLarge)
}
}
func TestValidateRejectsImageAboveDecodeBudget(t *testing.T) {
// A 100-pixel image needs ~400 bytes of RGBA memory, more than this budget.
validator := NewValidator(40_000_000, 100)
file := writeTemp(t, "budget.png", encodePNG(t, 10, 10))
_, err := validator.Validate(context.Background(), file, "image/png")
if !common.IsCode(err, common.ErrorCodePayloadTooLarge) {
t.Fatalf("Validate = %v, want %s", err, common.ErrorCodePayloadTooLarge)
}
}
func TestSafeDecodeConvertsPanicToError(t *testing.T) {
// image.Decode does not panic on arbitrary bytes, so this only guards the
// contract that safeDecode always returns an error instead of crashing.
file := writeTemp(t, "junk", []byte{0x89, 'P', 'N', 'G', 0x0d, 0x0a, 0x1a, 0x0a, 0xff})
if _, err := safeDecode(file); err == nil {
t.Fatal("safeDecode accepted junk bytes")
}
}