forked from mtusi2026/mtusi2026lab6
55 lines
1.8 KiB
Go
55 lines
1.8 KiB
Go
package http
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/api/http/middleware"
|
|
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/domain"
|
|
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/service"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
// Optional extension keeps the mandatory proxy contract unchanged.
|
|
type directAttachments interface {
|
|
InitDirect(context.Context, string, string, service.DirectInput) (service.DirectUpload, error)
|
|
CompleteDirect(context.Context, string, string, string) (domain.Attachment, error)
|
|
}
|
|
|
|
func (h *attachmentsHandler) directInit(c echo.Context) error {
|
|
svc, ok := h.attachments.(directAttachments)
|
|
if !ok {
|
|
return WriteError(c, domain.ErrUnavailable)
|
|
}
|
|
var in service.DirectInput
|
|
if err := decodeJSON(c.Request().Body, &in); err != nil {
|
|
return WriteError(c, domain.ErrInvalidArgument)
|
|
}
|
|
out, err := svc.InitDirect(c.Request().Context(), c.Get(middleware.AuthUserIDKey).(string), c.Param("chat_id"), in)
|
|
if err != nil {
|
|
return WriteError(c, err)
|
|
}
|
|
c.Response().Header().Set("Cache-Control", "no-store")
|
|
return c.JSON(http.StatusCreated, out)
|
|
}
|
|
|
|
func (h *attachmentsHandler) directComplete(c echo.Context) error {
|
|
svc, ok := h.attachments.(directAttachments)
|
|
if !ok {
|
|
return WriteError(c, domain.ErrUnavailable)
|
|
}
|
|
var in struct {
|
|
Token string `json:"token"`
|
|
}
|
|
if err := decodeJSON(c.Request().Body, &in); err != nil {
|
|
return WriteError(c, domain.ErrInvalidArgument)
|
|
}
|
|
ctx, cancel := context.WithTimeout(c.Request().Context(), transferTimeout)
|
|
defer cancel()
|
|
att, err := svc.CompleteDirect(ctx, c.Get(middleware.AuthUserIDKey).(string), c.Param("chat_id"), in.Token)
|
|
if err != nil {
|
|
return WriteError(c, err)
|
|
}
|
|
return c.JSON(http.StatusCreated, toAttachmentResponse(att))
|
|
}
|