feat(handler):lab4 add AttachmentHandler with Upload
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"mime"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
"high-loud-apps-labs/internal/httpapi/dto"
|
||||
"high-loud-apps-labs/internal/httpapi/middleware"
|
||||
"high-loud-apps-labs/internal/service"
|
||||
)
|
||||
|
||||
type AttachmentHandler struct {
|
||||
svc *service.AttachmentService
|
||||
}
|
||||
|
||||
func NewAttachmentHandler(svc *service.AttachmentService) *AttachmentHandler {
|
||||
return &AttachmentHandler{svc: svc}
|
||||
}
|
||||
|
||||
func (h *AttachmentHandler) Upload(c *gin.Context) {
|
||||
me := middleware.CurrentUser(c)
|
||||
|
||||
chatID, err := parseUUIDPathParam(c, "chat_id")
|
||||
if err != nil {
|
||||
respondError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
filename := c.Query("filename")
|
||||
if filename == "" {
|
||||
respondError(c, &domain.ValidationError{
|
||||
Field: "filename",
|
||||
Message: "обязателен",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
mediaType, _, err := mime.ParseMediaType(c.GetHeader("Content-Type"))
|
||||
if err != nil || (mediaType != "image/png" && mediaType != "image/jpeg") {
|
||||
respondError(c, &domain.UnsupportedMediaTypeError{
|
||||
MediaType: c.GetHeader("Content-Type"),
|
||||
Expected: "image/png или image/jpeg",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
att, err := h.svc.Upload(c.Request.Context(), me.ID, chatID, filename, c.Request.Body)
|
||||
if err != nil {
|
||||
respondError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, dto.AttachmentFromDomain(att))
|
||||
}
|
||||
Reference in New Issue
Block a user