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

171 lines
5.6 KiB
Go
Raw 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
)
// createUploadRequest represents a new upload session request
// @Description Request to initiate a file upload
2025-11-29 17:25:11 +00:00
type createUploadRequest struct {
// ID of the parent directory to upload into
ParentID string `json:"parentId" example:"kRp2XYTq9A55"`
// Name of the file being uploaded
Name string `json:"name" example:"document.pdf"`
2025-11-29 17:25:11 +00:00
}
// updateUploadRequest represents an upload status update
// @Description Request to update upload status (e.g., mark as completed)
2025-11-29 17:25:11 +00:00
type updateUploadRequest struct {
// New status for the upload
Status Status `json:"status" example:"completed" enums:"completed"`
2025-11-29 17:25:11 +00:00
}
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
}
// Create initiates a new file upload session
// @Summary Create upload session
// @Description Start a new file upload session. Returns an upload URL to PUT file content to.
// @Tags uploads
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param accountID path string true "Account ID" format(uuid)
// @Param request body createUploadRequest true "Upload details"
// @Success 200 {object} Upload "Upload session created"
// @Failure 400 {object} map[string]string "Parent is not a directory"
// @Failure 401 {string} string "Not authenticated"
// @Failure 404 {string} string "Parent directory not found"
// @Failure 409 {object} map[string]string "File with this name already exists"
// @Router /accounts/{accountID}/uploads [post]
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)
}
// ReceiveContent receives the file content for an upload
// @Summary Upload file content
// @Description Stream file content to complete an upload. Send raw binary data in the request body.
// @Tags uploads
// @Accept application/octet-stream
// @Security BearerAuth
// @Param accountID path string true "Account ID" format(uuid)
// @Param uploadID path string true "Upload session ID"
// @Param file body []byte true "File content (binary)"
// @Success 204 {string} string "Content received successfully"
// @Failure 401 {string} string "Not authenticated"
// @Failure 404 {string} string "Upload session not found"
// @Router /accounts/{accountID}/uploads/{uploadID}/content [put]
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)
}
// Update updates the upload status
// @Summary Complete upload
// @Description Mark an upload as completed after content has been uploaded. This finalizes the file in the filesystem.
// @Tags uploads
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param accountID path string true "Account ID" format(uuid)
// @Param uploadID path string true "Upload session ID"
// @Param request body updateUploadRequest true "Status update"
// @Success 200 {object} Upload "Upload completed"
// @Failure 400 {object} map[string]string "Content not uploaded yet or invalid status"
// @Failure 401 {string} string "Not authenticated"
// @Failure 404 {string} string "Upload session not found"
// @Router /accounts/{accountID}/uploads/{uploadID} [patch]
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)
}