lab4: вложения в S3 (MinIO), reconciler PostgreSQL↔S3, тесты и CI

This commit is contained in:
2026-11-04 20:11:33 +03:00
parent ec540a1a72
commit 77b6e5c2c7
86 changed files with 6921 additions and 2 deletions
+90
View File
@@ -0,0 +1,90 @@
package http
import (
"io"
"log/slog"
"net/http"
"strconv"
"github.com/charawein/mtusi2026lab4/internal/service"
"github.com/charawein/mtusi2026lab4/internal/transport/httputil"
)
type AttachmentHandler struct {
attachments *service.AttachmentService
}
func NewAttachmentHandler(attachments *service.AttachmentService) *AttachmentHandler {
return &AttachmentHandler{attachments: attachments}
}
// Upload: POST /api/v1/chats/{chat_id}/attachments?filename=... с сырыми байтами PNG/JPEG.
func (h *AttachmentHandler) Upload(w http.ResponseWriter, r *http.Request) {
uid := httputil.UserIDFromContext(r.Context())
if uid == "" {
httputil.WriteError(w, r, service.ErrUnauthorized)
return
}
q := r.URL.Query()
if len(q["filename"]) != 1 {
httputil.WriteError(w, r, service.ErrInvalidInput)
return
}
// MaxBytesReader — второй рубеж: не дать прочитать больше лимита + 1 байт,
// даже если Content-Length солгал.
r.Body = http.MaxBytesReader(w, r.Body, 10*1024*1024+1)
a, err := h.attachments.Upload(r.Context(), uid, r.PathValue("chat_id"), q.Get("filename"),
r.Header.Get("Content-Type"), r.Body, r.ContentLength)
if err != nil {
httputil.WriteError(w, r, err)
return
}
httputil.WriteJSON(w, r, http.StatusCreated, a)
}
// Get: GET /api/v1/attachments/{attachment_id} — метаданные.
func (h *AttachmentHandler) Get(w http.ResponseWriter, r *http.Request) {
uid := httputil.UserIDFromContext(r.Context())
a, err := h.attachments.Get(r.Context(), uid, r.PathValue("attachment_id"))
if err != nil {
httputil.WriteError(w, r, err)
return
}
httputil.WriteJSON(w, r, http.StatusOK, a)
}
// Content: GET /api/v1/attachments/{attachment_id}/content[?variant=original] — байты
// через авторизованный API, без redirect на S3.
func (h *AttachmentHandler) Content(w http.ResponseWriter, r *http.Request) {
uid := httputil.UserIDFromContext(r.Context())
variant := r.URL.Query().Get("variant")
switch variant {
case "", "original":
case "thumbnail", "crop", "optimized":
// Варианты появятся в lab5; у ready-объекта lab4 их нет — после проверки доступа 404.
if _, err := h.attachments.Get(r.Context(), uid, r.PathValue("attachment_id")); err != nil {
httputil.WriteError(w, r, err)
return
}
httputil.WriteError(w, r, service.ErrNotFound)
return
default:
httputil.WriteError(w, r, service.ErrInvalidInput)
return
}
a, rc, err := h.attachments.Open(r.Context(), uid, r.PathValue("attachment_id"))
if err != nil {
httputil.WriteError(w, r, err)
return
}
defer rc.Close()
w.Header().Set("Content-Type", a.ContentType)
w.Header().Set("Content-Length", strconv.FormatInt(a.SizeBytes, 10))
w.Header().Set("Cache-Control", "private, no-store")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(http.StatusOK)
if n, err := io.Copy(w, rc); err != nil {
// Заголовок уже отправлен: клиент увидит обрыв по Content-Length.
slog.Warn("attachment download interrupted", "attachment_id", a.ID, "sent", n, "err", err)
}
}