mirror of
https://github.com/get-drexa/drive.git
synced 2026-02-02 13:21:17 +00:00
refactor: make vfs methods accept bun.IDB
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"github.com/get-drexa/drexa/internal/account"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type createUploadRequest struct {
|
||||
@@ -18,10 +19,11 @@ type updateUploadRequest struct {
|
||||
|
||||
type HTTPHandler struct {
|
||||
service *Service
|
||||
db *bun.DB
|
||||
}
|
||||
|
||||
func NewHTTPHandler(s *Service) *HTTPHandler {
|
||||
return &HTTPHandler{service: s}
|
||||
func NewHTTPHandler(s *Service, db *bun.DB) *HTTPHandler {
|
||||
return &HTTPHandler{service: s, db: db}
|
||||
}
|
||||
|
||||
func (h *HTTPHandler) RegisterRoutes(api fiber.Router) {
|
||||
@@ -43,7 +45,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(), account.ID, CreateUploadOptions{
|
||||
upload, err := h.service.CreateUpload(c.Context(), h.db, account.ID, CreateUploadOptions{
|
||||
ParentID: req.ParentID,
|
||||
Name: req.Name,
|
||||
})
|
||||
@@ -62,7 +64,7 @@ func (h *HTTPHandler) ReceiveContent(c *fiber.Ctx) error {
|
||||
|
||||
uploadID := c.Params("uploadID")
|
||||
|
||||
err := h.service.ReceiveUpload(c.Context(), account.ID, uploadID, c.Request().BodyStream())
|
||||
err := h.service.ReceiveUpload(c.Context(), h.db, 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"})
|
||||
@@ -83,7 +85,7 @@ func (h *HTTPHandler) Update(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
if req.Status == StatusCompleted {
|
||||
upload, err := h.service.CompleteUpload(c.Context(), account.ID, c.Params("uploadID"))
|
||||
upload, err := h.service.CompleteUpload(c.Context(), h.db, account.ID, c.Params("uploadID"))
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
return c.SendStatus(fiber.StatusNotFound)
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/get-drexa/drexa/internal/blob"
|
||||
"github.com/get-drexa/drexa/internal/virtualfs"
|
||||
"github.com/google/uuid"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
@@ -33,8 +34,8 @@ type CreateUploadOptions struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
func (s *Service) CreateUpload(ctx context.Context, accountID uuid.UUID, opts CreateUploadOptions) (*Upload, error) {
|
||||
parentNode, err := s.vfs.FindNodeByPublicID(ctx, accountID, opts.ParentID)
|
||||
func (s *Service) CreateUpload(ctx context.Context, db bun.IDB, accountID uuid.UUID, opts CreateUploadOptions) (*Upload, error) {
|
||||
parentNode, err := s.vfs.FindNodeByPublicID(ctx, db, accountID, opts.ParentID)
|
||||
if err != nil {
|
||||
if errors.Is(err, virtualfs.ErrNodeNotFound) {
|
||||
return nil, ErrNotFound
|
||||
@@ -46,7 +47,7 @@ func (s *Service) CreateUpload(ctx context.Context, accountID uuid.UUID, opts Cr
|
||||
return nil, ErrParentNotDirectory
|
||||
}
|
||||
|
||||
node, err := s.vfs.CreateFile(ctx, accountID, virtualfs.CreateFileOptions{
|
||||
node, err := s.vfs.CreateFile(ctx, db, accountID, virtualfs.CreateFileOptions{
|
||||
ParentID: parentNode.ID,
|
||||
Name: opts.Name,
|
||||
})
|
||||
@@ -61,7 +62,7 @@ func (s *Service) CreateUpload(ctx context.Context, accountID uuid.UUID, opts Cr
|
||||
Duration: 1 * time.Hour,
|
||||
})
|
||||
if err != nil {
|
||||
_ = s.vfs.PermanentlyDeleteNode(ctx, node)
|
||||
_ = s.vfs.PermanentlyDeleteNode(ctx, db, node)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -77,7 +78,7 @@ func (s *Service) CreateUpload(ctx context.Context, accountID uuid.UUID, opts Cr
|
||||
return upload, nil
|
||||
}
|
||||
|
||||
func (s *Service) ReceiveUpload(ctx context.Context, accountID uuid.UUID, uploadID string, reader io.Reader) error {
|
||||
func (s *Service) ReceiveUpload(ctx context.Context, db bun.IDB, accountID uuid.UUID, uploadID string, reader io.Reader) error {
|
||||
n, ok := s.pendingUploads.Load(uploadID)
|
||||
if !ok {
|
||||
return ErrNotFound
|
||||
@@ -92,7 +93,7 @@ func (s *Service) ReceiveUpload(ctx context.Context, accountID uuid.UUID, upload
|
||||
return ErrNotFound
|
||||
}
|
||||
|
||||
err := s.vfs.WriteFile(ctx, upload.TargetNode, virtualfs.FileContentFromReader(reader))
|
||||
err := s.vfs.WriteFile(ctx, db, upload.TargetNode, virtualfs.FileContentFromReader(reader))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -102,7 +103,7 @@ func (s *Service) ReceiveUpload(ctx context.Context, accountID uuid.UUID, upload
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) CompleteUpload(ctx context.Context, accountID uuid.UUID, uploadID string) (*Upload, error) {
|
||||
func (s *Service) CompleteUpload(ctx context.Context, db bun.IDB, accountID uuid.UUID, uploadID string) (*Upload, error) {
|
||||
n, ok := s.pendingUploads.Load(uploadID)
|
||||
if !ok {
|
||||
return nil, ErrNotFound
|
||||
@@ -121,7 +122,7 @@ func (s *Service) CompleteUpload(ctx context.Context, accountID uuid.UUID, uploa
|
||||
return upload, nil
|
||||
}
|
||||
|
||||
err := s.vfs.WriteFile(ctx, upload.TargetNode, virtualfs.FileContentFromBlobKey(upload.TargetNode.BlobKey))
|
||||
err := s.vfs.WriteFile(ctx, db, upload.TargetNode, virtualfs.FileContentFromBlobKey(upload.TargetNode.BlobKey))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user