mirror of
https://github.com/get-drexa/drive.git
synced 2025-12-01 05:51:39 +00:00
feat: introduce account
This commit is contained in:
@@ -3,7 +3,7 @@ package upload
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/get-drexa/drexa/internal/auth"
|
||||
"github.com/get-drexa/drexa/internal/account"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
@@ -17,17 +17,15 @@ type updateUploadRequest struct {
|
||||
}
|
||||
|
||||
type HTTPHandler struct {
|
||||
service *Service
|
||||
authMiddleware fiber.Handler
|
||||
service *Service
|
||||
}
|
||||
|
||||
func NewHTTPHandler(s *Service, authMiddleware fiber.Handler) *HTTPHandler {
|
||||
return &HTTPHandler{service: s, authMiddleware: authMiddleware}
|
||||
func NewHTTPHandler(s *Service) *HTTPHandler {
|
||||
return &HTTPHandler{service: s}
|
||||
}
|
||||
|
||||
func (h *HTTPHandler) RegisterRoutes(api fiber.Router, authMiddleware fiber.Handler) {
|
||||
func (h *HTTPHandler) RegisterRoutes(api fiber.Router) {
|
||||
upload := api.Group("/uploads")
|
||||
upload.Use(authMiddleware)
|
||||
|
||||
upload.Post("/", h.Create)
|
||||
upload.Put("/:uploadID/content", h.ReceiveContent)
|
||||
@@ -35,9 +33,9 @@ func (h *HTTPHandler) RegisterRoutes(api fiber.Router, authMiddleware fiber.Hand
|
||||
}
|
||||
|
||||
func (h *HTTPHandler) Create(c *fiber.Ctx) error {
|
||||
u, err := auth.AuthenticatedUser(c)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "Unauthorized"})
|
||||
account := account.CurrentAccount(c)
|
||||
if account == nil {
|
||||
return c.SendStatus(fiber.StatusUnauthorized)
|
||||
}
|
||||
|
||||
req := new(createUploadRequest)
|
||||
@@ -45,7 +43,7 @@ func (h *HTTPHandler) Create(c *fiber.Ctx) error {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request"})
|
||||
}
|
||||
|
||||
upload, err := h.service.CreateUpload(c.Context(), u.ID, CreateUploadOptions{
|
||||
upload, err := h.service.CreateUpload(c.Context(), account.ID, CreateUploadOptions{
|
||||
ParentID: req.ParentID,
|
||||
Name: req.Name,
|
||||
})
|
||||
@@ -57,14 +55,14 @@ func (h *HTTPHandler) Create(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func (h *HTTPHandler) ReceiveContent(c *fiber.Ctx) error {
|
||||
u, err := auth.AuthenticatedUser(c)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "Unauthorized"})
|
||||
account := account.CurrentAccount(c)
|
||||
if account == nil {
|
||||
return c.SendStatus(fiber.StatusUnauthorized)
|
||||
}
|
||||
|
||||
uploadID := c.Params("uploadID")
|
||||
|
||||
err = h.service.ReceiveUpload(c.Context(), u.ID, uploadID, c.Request().BodyStream())
|
||||
err := h.service.ReceiveUpload(c.Context(), account.ID, uploadID, c.Request().BodyStream())
|
||||
defer c.Request().CloseBodyStream()
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Internal server error"})
|
||||
@@ -74,9 +72,9 @@ func (h *HTTPHandler) ReceiveContent(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func (h *HTTPHandler) Update(c *fiber.Ctx) error {
|
||||
u, err := auth.AuthenticatedUser(c)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "Unauthorized"})
|
||||
account := account.CurrentAccount(c)
|
||||
if account == nil {
|
||||
return c.SendStatus(fiber.StatusUnauthorized)
|
||||
}
|
||||
|
||||
req := new(updateUploadRequest)
|
||||
@@ -85,7 +83,7 @@ func (h *HTTPHandler) Update(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
if req.Status == StatusCompleted {
|
||||
upload, err := h.service.CompleteUpload(c.Context(), u.ID, c.Params("uploadID"))
|
||||
upload, err := h.service.CompleteUpload(c.Context(), account.ID, c.Params("uploadID"))
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
return c.SendStatus(fiber.StatusNotFound)
|
||||
|
||||
@@ -33,8 +33,8 @@ type CreateUploadOptions struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func (s *Service) CreateUpload(ctx context.Context, userID uuid.UUID, opts CreateUploadOptions) (*Upload, error) {
|
||||
parentNode, err := s.vfs.FindNodeByPublicID(ctx, userID, opts.ParentID)
|
||||
func (s *Service) CreateUpload(ctx context.Context, accountID uuid.UUID, opts CreateUploadOptions) (*Upload, error) {
|
||||
parentNode, err := s.vfs.FindNodeByPublicID(ctx, accountID, opts.ParentID)
|
||||
if err != nil {
|
||||
if errors.Is(err, virtualfs.ErrNodeNotFound) {
|
||||
return nil, ErrNotFound
|
||||
@@ -46,7 +46,7 @@ func (s *Service) CreateUpload(ctx context.Context, userID uuid.UUID, opts Creat
|
||||
return nil, ErrParentNotDirectory
|
||||
}
|
||||
|
||||
node, err := s.vfs.CreateFile(ctx, userID, virtualfs.CreateFileOptions{
|
||||
node, err := s.vfs.CreateFile(ctx, accountID, virtualfs.CreateFileOptions{
|
||||
ParentID: parentNode.ID,
|
||||
Name: opts.Name,
|
||||
})
|
||||
@@ -77,7 +77,7 @@ func (s *Service) CreateUpload(ctx context.Context, userID uuid.UUID, opts Creat
|
||||
return upload, nil
|
||||
}
|
||||
|
||||
func (s *Service) ReceiveUpload(ctx context.Context, userID uuid.UUID, uploadID string, reader io.Reader) error {
|
||||
func (s *Service) ReceiveUpload(ctx context.Context, accountID uuid.UUID, uploadID string, reader io.Reader) error {
|
||||
n, ok := s.pendingUploads.Load(uploadID)
|
||||
if !ok {
|
||||
return ErrNotFound
|
||||
@@ -88,7 +88,7 @@ func (s *Service) ReceiveUpload(ctx context.Context, userID uuid.UUID, uploadID
|
||||
return ErrNotFound
|
||||
}
|
||||
|
||||
if upload.TargetNode.UserID != userID {
|
||||
if upload.TargetNode.AccountID != accountID {
|
||||
return ErrNotFound
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ func (s *Service) ReceiveUpload(ctx context.Context, userID uuid.UUID, uploadID
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) CompleteUpload(ctx context.Context, userID uuid.UUID, uploadID string) (*Upload, error) {
|
||||
func (s *Service) CompleteUpload(ctx context.Context, accountID uuid.UUID, uploadID string) (*Upload, error) {
|
||||
n, ok := s.pendingUploads.Load(uploadID)
|
||||
if !ok {
|
||||
return nil, ErrNotFound
|
||||
@@ -113,7 +113,7 @@ func (s *Service) CompleteUpload(ctx context.Context, userID uuid.UUID, uploadID
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
|
||||
if upload.TargetNode.UserID != userID {
|
||||
if upload.TargetNode.AccountID != accountID {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user