mirror of
https://github.com/get-drexa/drive.git
synced 2025-12-07 08:31:40 +00:00
fix: use db tx when creating directory
This commit is contained in:
@@ -65,7 +65,13 @@ func (h *HTTPHandler) createDirectory(c *fiber.Ctx) error {
|
|||||||
return c.SendStatus(fiber.StatusBadRequest)
|
return c.SendStatus(fiber.StatusBadRequest)
|
||||||
}
|
}
|
||||||
|
|
||||||
parent, err := h.vfs.FindNodeByPublicID(c.Context(), h.db, account.ID, req.ParentID)
|
tx, err := h.db.BeginTx(c.Context(), nil)
|
||||||
|
if err != nil {
|
||||||
|
return httperr.Internal(err)
|
||||||
|
}
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
|
parent, err := h.vfs.FindNodeByPublicID(c.Context(), tx, account.ID, req.ParentID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, virtualfs.ErrNodeNotFound) {
|
if errors.Is(err, virtualfs.ErrNodeNotFound) {
|
||||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Parent not found"})
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Parent not found"})
|
||||||
@@ -77,7 +83,7 @@ func (h *HTTPHandler) createDirectory(c *fiber.Ctx) error {
|
|||||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Parent is not a directory"})
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Parent is not a directory"})
|
||||||
}
|
}
|
||||||
|
|
||||||
node, err := h.vfs.CreateDirectory(c.Context(), h.db, account.ID, parent.ID, req.Name)
|
node, err := h.vfs.CreateDirectory(c.Context(), tx, account.ID, parent.ID, req.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, virtualfs.ErrNodeConflict) {
|
if errors.Is(err, virtualfs.ErrNodeConflict) {
|
||||||
return c.Status(fiber.StatusConflict).JSON(fiber.Map{"error": "Directory already exists"})
|
return c.Status(fiber.StatusConflict).JSON(fiber.Map{"error": "Directory already exists"})
|
||||||
@@ -85,6 +91,11 @@ func (h *HTTPHandler) createDirectory(c *fiber.Ctx) error {
|
|||||||
return httperr.Internal(err)
|
return httperr.Internal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = tx.Commit()
|
||||||
|
if err != nil {
|
||||||
|
return httperr.Internal(err)
|
||||||
|
}
|
||||||
|
|
||||||
return c.JSON(DirectoryInfo{
|
return c.JSON(DirectoryInfo{
|
||||||
Kind: DirItemKindDirectory,
|
Kind: DirItemKindDirectory,
|
||||||
ID: node.PublicID,
|
ID: node.PublicID,
|
||||||
|
|||||||
Reference in New Issue
Block a user