package handlers import ( "errors" "mime" "net/http" "strings" "mtusi2026lab4/internal/application" "mtusi2026lab4/internal/domain" "mtusi2026lab4/internal/transport/http/dto" "mtusi2026lab4/internal/transport/http/middleware" "mtusi2026lab4/internal/transport/http/response" "github.com/google/uuid" ) func (h *Handler) UploadAttachment(w http.ResponseWriter, r *http.Request) { requestID := middleware.GetRequestID(r.Context()) currentUser := middleware.GetCurrentUser(r.Context()) parsedChatID, err := uuid.Parse(r.PathValue("chat_id")) if err != nil { response.WriteError(w, http.StatusBadRequest, response.CodeInvalidRequest, "invalid chat_id uuid", requestID) return } filename := r.URL.Query().Get("filename") if filename == "" { response.WriteError(w, http.StatusBadRequest, response.CodeInvalidRequest, "filename query parameter is required", requestID) return } contentType, parameters, err := mime.ParseMediaType(r.Header.Get("Content-Type")) if err != nil || len(parameters) != 0 || (contentType != "image/png" && contentType != "image/jpeg") { response.WriteError(w, http.StatusUnsupportedMediaType, response.CodeUnsupportedMediaType, "Content-Type must be image/png or image/jpeg", requestID) return } if r.ContentLength > domain.MaxAttachmentSizeBytes { response.WriteError(w, http.StatusRequestEntityTooLarge, response.CodePayloadTooLarge, "attachment exceeds 10 MiB", requestID) return } if r.Body == nil { response.WriteError(w, http.StatusBadRequest, response.CodeInvalidRequest, "attachment body is required", requestID) return } defer r.Body.Close() attachment, err := h.service.UploadAttachment( r.Context(), application.UploadAttachmentCommand{ ActorID: currentUser.ID, ChatID: parsedChatID.String(), Filename: filename, DeclaredContentType: contentType, Body: r.Body, }, ) switch { case errors.Is(err, application.ErrPayloadTooLarge): response.WriteError(w, http.StatusRequestEntityTooLarge, response.CodePayloadTooLarge, "attachment exceeds 10 MiB or 40 million pixels", requestID) return case errors.Is(err, application.ErrUnsupportedMediaType), errors.Is(err, application.ErrInvalidImage): response.WriteError(w, http.StatusUnsupportedMediaType, response.CodeUnsupportedMediaType, "body is not a valid PNG/JPEG matching the declared Content-Type", requestID) return case errors.Is(err, application.ErrInvalidInput): message := strings.TrimPrefix(err.Error(), application.ErrInvalidInput.Error()+": ") response.WriteError(w, http.StatusBadRequest, response.CodeInvalidRequest, message, requestID) return case errors.Is(err, domain.ErrNotFound): response.WriteError(w, http.StatusNotFound, response.CodeNotFound, "chat not found", requestID) return case errors.Is(err, application.ErrStorageUnavailable): response.WriteError(w, http.StatusServiceUnavailable, response.CodeUnavailable, "object storage is temporarily unavailable", requestID) return case err != nil: response.WriteError(w, http.StatusServiceUnavailable, response.CodeUnavailable, "service is temporarily unavailable", requestID) return } response.WriteJSON(w, http.StatusCreated, dto.AttachmentFromDomain(attachment), requestID) }