Files
drive/apps/backend/internal/upload/http.go

98 lines
2.4 KiB
Go
Raw Normal View History

2025-11-29 17:25:11 +00:00
package upload
import (
"errors"
2025-11-30 17:12:50 +00:00
"github.com/get-drexa/drexa/internal/account"
2025-11-29 17:25:11 +00:00
"github.com/gofiber/fiber/v2"
)
type createUploadRequest struct {
ParentID string `json:"parentId"`
Name string `json:"name"`
}
type updateUploadRequest struct {
Status Status `json:"status"`
}
type HTTPHandler struct {
2025-11-30 17:12:50 +00:00
service *Service
}
2025-11-29 17:25:11 +00:00
2025-11-30 17:12:50 +00:00
func NewHTTPHandler(s *Service) *HTTPHandler {
return &HTTPHandler{service: s}
2025-11-29 17:25:11 +00:00
}
2025-11-30 17:12:50 +00:00
func (h *HTTPHandler) RegisterRoutes(api fiber.Router) {
upload := api.Group("/uploads")
upload.Post("/", h.Create)
upload.Put("/:uploadID/content", h.ReceiveContent)
upload.Patch("/:uploadID", h.Update)
2025-11-29 17:25:11 +00:00
}
func (h *HTTPHandler) Create(c *fiber.Ctx) error {
2025-11-30 17:12:50 +00:00
account := account.CurrentAccount(c)
if account == nil {
return c.SendStatus(fiber.StatusUnauthorized)
2025-11-29 17:25:11 +00:00
}
req := new(createUploadRequest)
if err := c.BodyParser(req); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request"})
}
2025-11-30 17:12:50 +00:00
upload, err := h.service.CreateUpload(c.Context(), account.ID, CreateUploadOptions{
2025-11-29 17:25:11 +00:00
ParentID: req.ParentID,
Name: req.Name,
})
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Internal server error"})
}
return c.JSON(upload)
}
func (h *HTTPHandler) ReceiveContent(c *fiber.Ctx) error {
2025-11-30 17:12:50 +00:00
account := account.CurrentAccount(c)
if account == nil {
return c.SendStatus(fiber.StatusUnauthorized)
2025-11-29 17:25:11 +00:00
}
uploadID := c.Params("uploadID")
2025-11-30 17:12:50 +00:00
err := h.service.ReceiveUpload(c.Context(), account.ID, uploadID, c.Request().BodyStream())
2025-11-29 17:25:11 +00:00
defer c.Request().CloseBodyStream()
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Internal server error"})
}
return c.SendStatus(fiber.StatusNoContent)
}
func (h *HTTPHandler) Update(c *fiber.Ctx) error {
2025-11-30 17:12:50 +00:00
account := account.CurrentAccount(c)
if account == nil {
return c.SendStatus(fiber.StatusUnauthorized)
2025-11-29 17:25:11 +00:00
}
req := new(updateUploadRequest)
if err := c.BodyParser(req); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request"})
}
if req.Status == StatusCompleted {
2025-11-30 17:12:50 +00:00
upload, err := h.service.CompleteUpload(c.Context(), account.ID, c.Params("uploadID"))
2025-11-29 17:25:11 +00:00
if err != nil {
if errors.Is(err, ErrNotFound) {
return c.SendStatus(fiber.StatusNotFound)
}
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Internal server error"})
}
return c.JSON(upload)
}
return c.SendStatus(fiber.StatusBadRequest)
}