141 lines
4.3 KiB
Go
141 lines
4.3 KiB
Go
package handler
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/google/uuid"
|
|
|
|
"mtusi2026-messenger/internal/dto"
|
|
"mtusi2026-messenger/internal/httpx"
|
|
"mtusi2026-messenger/internal/middleware"
|
|
"mtusi2026-messenger/internal/service"
|
|
)
|
|
|
|
type AttachmentHandler struct {
|
|
service *service.AttachmentService
|
|
maxBytes int
|
|
}
|
|
|
|
func NewAttachmentHandler(service *service.AttachmentService, maxBytes int) *AttachmentHandler {
|
|
return &AttachmentHandler{
|
|
service: service,
|
|
maxBytes: maxBytes,
|
|
}
|
|
}
|
|
|
|
func (h *AttachmentHandler) Upload(w http.ResponseWriter, r *http.Request) {
|
|
requestID := middleware.GetRequestID(r.Context())
|
|
userID := middleware.GetUserID(r.Context())
|
|
|
|
chatID, err := uuid.Parse(chi.URLParam(r, "chat_id"))
|
|
if err != nil {
|
|
httpx.WriteError(w, http.StatusBadRequest, dto.ErrorCodeInvalidRequest, "invalid chat_id", requestID)
|
|
return
|
|
}
|
|
|
|
req := dto.CreateAttachmentRequest{
|
|
Filename: r.URL.Query().Get("filename"),
|
|
}
|
|
if !req.Validate() {
|
|
httpx.WriteError(w, http.StatusBadRequest, dto.ErrorCodeInvalidRequest, "invalid filename", requestID)
|
|
return
|
|
}
|
|
|
|
if deadline, ok := r.Context().Deadline(); ok {
|
|
controller := http.NewResponseController(w)
|
|
if err := controller.SetReadDeadline(deadline); err != nil {
|
|
httpx.WriteInternalError(w, requestID, err)
|
|
return
|
|
}
|
|
defer func() { _ = controller.SetReadDeadline(time.Time{}) }()
|
|
}
|
|
|
|
contentType := r.Header.Get("Content-Type")
|
|
r.Body = http.MaxBytesReader(w, r.Body, int64(h.maxBytes))
|
|
|
|
att, err := h.service.Upload(r.Context(), userID, chatID, req.Filename, r.Body, contentType)
|
|
switch {
|
|
case errors.Is(err, service.ErrUploadReadTimeout):
|
|
r.Close = true
|
|
w.Header().Set("Connection", "close")
|
|
httpx.WriteError(w, http.StatusRequestTimeout, dto.ErrorCodeInvalidRequest, "upload body read timed out", requestID)
|
|
return
|
|
case errors.Is(err, service.ErrInvalidSize):
|
|
httpx.WriteError(w, http.StatusRequestEntityTooLarge, dto.ErrorCodePayloadTooLarge, "file too large", requestID)
|
|
return
|
|
case errors.Is(err, service.ErrInvalidImage):
|
|
httpx.WriteError(w, http.StatusUnsupportedMediaType, dto.ErrorCodeUnsupportedMediaType, "invalid image", requestID)
|
|
return
|
|
case errors.Is(err, service.ErrAttachmentNotFound):
|
|
httpx.WriteError(w, http.StatusNotFound, dto.ErrorCodeNotFound, "not found", requestID)
|
|
return
|
|
case err != nil:
|
|
httpx.WriteInternalError(w, requestID, err)
|
|
return
|
|
}
|
|
|
|
httpx.WriteJSON(w, http.StatusAccepted, dto.ToAttachmentResponse(att))
|
|
}
|
|
|
|
func (h *AttachmentHandler) Get(w http.ResponseWriter, r *http.Request) {
|
|
requestID := middleware.GetRequestID(r.Context())
|
|
userID := middleware.GetUserID(r.Context())
|
|
|
|
attachmentID, err := uuid.Parse(chi.URLParam(r, "attachment_id"))
|
|
if err != nil {
|
|
httpx.WriteError(w, http.StatusBadRequest, dto.ErrorCodeInvalidRequest, "invalid attachment_id", requestID)
|
|
return
|
|
}
|
|
|
|
att, err := h.service.Get(r.Context(), userID, attachmentID)
|
|
switch {
|
|
case errors.Is(err, service.ErrAttachmentNotFound):
|
|
httpx.WriteError(w, http.StatusNotFound, dto.ErrorCodeNotFound, "not found", requestID)
|
|
return
|
|
case err != nil:
|
|
httpx.WriteInternalError(w, requestID, err)
|
|
return
|
|
}
|
|
|
|
httpx.WriteJSON(w, http.StatusOK, dto.ToAttachmentResponse(att))
|
|
}
|
|
|
|
func (h *AttachmentHandler) GetContent(w http.ResponseWriter, r *http.Request) {
|
|
requestID := middleware.GetRequestID(r.Context())
|
|
userID := middleware.GetUserID(r.Context())
|
|
|
|
attachmentID, err := uuid.Parse(chi.URLParam(r, "attachment_id"))
|
|
if err != nil {
|
|
httpx.WriteError(w, http.StatusBadRequest, dto.ErrorCodeInvalidRequest, "invalid attachment_id", requestID)
|
|
return
|
|
}
|
|
|
|
req := dto.GetAttachmentContentRequest{
|
|
Variant: r.URL.Query().Get("variant"),
|
|
}
|
|
if !req.Validate() {
|
|
httpx.WriteError(w, http.StatusNotFound, dto.ErrorCodeNotFound, "not found", requestID)
|
|
return
|
|
}
|
|
|
|
data, contentType, err := h.service.GetContent(r.Context(), userID, attachmentID, req.Variant)
|
|
switch {
|
|
case errors.Is(err, service.ErrAttachmentNotReady):
|
|
httpx.WriteError(w, http.StatusConflict, dto.ErrorCodeConflict, "attachment not ready", requestID)
|
|
return
|
|
case errors.Is(err, service.ErrAttachmentNotFound):
|
|
httpx.WriteError(w, http.StatusNotFound, dto.ErrorCodeNotFound, "not found", requestID)
|
|
return
|
|
case err != nil:
|
|
httpx.WriteInternalError(w, requestID, err)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", string(contentType))
|
|
w.WriteHeader(http.StatusOK)
|
|
_, _ = w.Write(data)
|
|
}
|