Files

121 lines
3.1 KiB
Go
Raw Permalink Normal View History

2025-11-29 17:25:11 +00:00
package upload
import (
"errors"
2025-11-30 19:39:47 +00:00
"fmt"
2025-11-29 17:25:11 +00:00
2025-11-30 17:12:50 +00:00
"github.com/get-drexa/drexa/internal/account"
2025-11-30 19:19:33 +00:00
"github.com/get-drexa/drexa/internal/httperr"
2025-11-29 17:25:11 +00:00
"github.com/gofiber/fiber/v2"
"github.com/uptrace/bun"
2025-11-29 17:25:11 +00:00
)
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
db *bun.DB
}
2025-11-29 17:25:11 +00:00
func NewHTTPHandler(s *Service, db *bun.DB) *HTTPHandler {
return &HTTPHandler{service: s, db: db}
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"})
}
upload, err := h.service.CreateUpload(c.Context(), h.db, account.ID, CreateUploadOptions{
2025-11-29 17:25:11 +00:00
ParentID: req.ParentID,
Name: req.Name,
})
if err != nil {
2025-11-30 19:19:33 +00:00
if errors.Is(err, ErrNotFound) {
return c.SendStatus(fiber.StatusNotFound)
}
2025-11-30 20:08:31 +00:00
if errors.Is(err, ErrParentNotDirectory) {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Parent is not a directory"})
}
if errors.Is(err, ErrConflict) {
return c.Status(fiber.StatusConflict).JSON(fiber.Map{"error": "A file with this name already exists"})
}
2025-11-30 19:19:33 +00:00
return httperr.Internal(err)
2025-11-29 17:25:11 +00:00
}
2025-11-30 19:39:47 +00:00
if upload.UploadURL == "" {
upload.UploadURL = fmt.Sprintf("%s%s/%s/content", c.BaseURL(), c.OriginalURL(), upload.ID)
}
2025-11-29 17:25:11 +00:00
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 19:39:47 +00:00
err := h.service.ReceiveUpload(c.Context(), h.db, account.ID, uploadID, c.Context().RequestBodyStream())
defer c.Context().Request.CloseBodyStream()
2025-11-29 17:25:11 +00:00
if err != nil {
2025-11-30 19:39:47 +00:00
if errors.Is(err, ErrNotFound) {
return c.SendStatus(fiber.StatusNotFound)
}
2025-11-30 19:19:33 +00:00
return httperr.Internal(err)
2025-11-29 17:25:11 +00:00
}
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 {
upload, err := h.service.CompleteUpload(c.Context(), h.db, 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)
}
2025-11-30 20:08:31 +00:00
if errors.Is(err, ErrContentNotUploaded) {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Content has not been uploaded"})
}
2025-11-30 19:19:33 +00:00
return httperr.Internal(err)
2025-11-29 17:25:11 +00:00
}
return c.JSON(upload)
}
return c.SendStatus(fiber.StatusBadRequest)
}