Доработайте публикацию и сверку вложений по итогам ревью
This commit is contained in:
@@ -8,40 +8,52 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/httpx"
|
||||
"messenger/internal/platform/ident"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Handler struct{ s *Service }
|
||||
|
||||
func NewHandler(s *Service) *Handler { return &Handler{s} }
|
||||
func (h *Handler) Register(protected, binary gin.IRouter) {
|
||||
binary.POST("/chats/:chat_id/attachments", httpx.WithUser(h.upload))
|
||||
func (h *Handler) Register(protected, binaryProtected gin.IRouter) {
|
||||
binaryProtected.POST("/chats/:chat_id/attachments", httpx.WithUser(h.upload))
|
||||
protected.GET("/attachments/:attachment_id", httpx.WithUser(h.metadata))
|
||||
binary.GET("/attachments/:attachment_id/content", httpx.WithUser(h.content))
|
||||
binaryProtected.GET("/attachments/:attachment_id/content", httpx.WithUser(h.content))
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
OwnerID string `json:"owner_id"`
|
||||
Filename string `json:"filename"`
|
||||
ContentType string `json:"content_type"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
SHA256 string `json:"sha256"`
|
||||
Status string `json:"status"`
|
||||
Variants []any `json:"variants"`
|
||||
ErrorCode *string `json:"error_code"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
OwnerID string `json:"owner_id"`
|
||||
Filename string `json:"filename"`
|
||||
ContentType string `json:"content_type"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
SHA256 string `json:"sha256"`
|
||||
Status string `json:"status"`
|
||||
// Lab4's shared response contract requires variants=[], even without processing.
|
||||
Variants []any `json:"variants"`
|
||||
ErrorCode *string `json:"error_code"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
func toResponse(a Attachment) Response {
|
||||
return Response{
|
||||
ID: a.ID.String(), ChatID: a.ChatID.String(), OwnerID: a.OwnerID.String(), Filename: a.Filename, ContentType: a.MIMEType, SizeBytes: a.SizeBytes, SHA256: hex.EncodeToString(a.FileHash), Status: a.Status, Variants: []any{}, CreatedAt: httpx.Timestamp(a.CreatedAt), UpdatedAt: httpx.Timestamp(a.CreatedAt),
|
||||
ID: a.ID.String(),
|
||||
ChatID: a.ChatID.String(),
|
||||
OwnerID: a.OwnerID.String(),
|
||||
Filename: a.Filename,
|
||||
ContentType: a.MIMEType,
|
||||
SizeBytes: a.SizeBytes,
|
||||
SHA256: hex.EncodeToString(a.FileHash),
|
||||
Status: string(a.Status),
|
||||
Variants: []any{},
|
||||
CreatedAt: httpx.Timestamp(a.CreatedAt),
|
||||
UpdatedAt: httpx.Timestamp(a.CreatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,8 +77,14 @@ func (h *Handler) upload(c *gin.Context, actor uuid.UUID) {
|
||||
httpx.Error(c, apperr.UnsupportedMedia("Content-Type must be image/png or image/jpeg"))
|
||||
return
|
||||
}
|
||||
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, h.s.maxBytes)
|
||||
a, err := h.s.Upload(c.Request.Context(), actor, id, names[0], media, c.Request.ContentLength, c.Request.Body)
|
||||
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, h.s.config.MaxBytes)
|
||||
input := UploadInput{
|
||||
ChatID: id,
|
||||
Filename: names[0],
|
||||
MIMEType: media,
|
||||
SizeBytes: c.Request.ContentLength,
|
||||
}
|
||||
a, err := h.s.Upload(c.Request.Context(), actor, input, c.Request.Body)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
@@ -109,7 +127,11 @@ func (h *Handler) content(c *gin.Context, actor uuid.UUID) {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
defer r.Close()
|
||||
defer func() {
|
||||
if err := r.Close(); err != nil {
|
||||
slog.Warn("download reader close failed", "request_id", httpx.RequestID(c), "error", err)
|
||||
}
|
||||
}()
|
||||
c.Header("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename": a.Filename}))
|
||||
c.Header("X-Content-Type-Options", "nosniff")
|
||||
c.Header("Content-Type", a.MIMEType)
|
||||
|
||||
Reference in New Issue
Block a user