From 629b1139ba246ff69a644d20b42bd91dae59ca58 Mon Sep 17 00:00:00 2001 From: EdWorth120 <158087205+EdWorth120@users.noreply.github.com> Date: Wed, 12 Mar 2025 16:08:19 -0300 Subject: [PATCH] Fixes the document record being abandoned inside the database after an attachment is deleted. (#579) --- .../core/services/service_items_attachments.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/backend/internal/core/services/service_items_attachments.go b/backend/internal/core/services/service_items_attachments.go index 5ed2ea6e..9bc97d4f 100644 --- a/backend/internal/core/services/service_items_attachments.go +++ b/backend/internal/core/services/service_items_attachments.go @@ -2,14 +2,12 @@ package services import ( "context" - "io" - "os" - "github.com/google/uuid" "github.com/rs/zerolog/log" "github.com/sysadminsmedia/homebox/backend/internal/data/ent" "github.com/sysadminsmedia/homebox/backend/internal/data/ent/attachment" "github.com/sysadminsmedia/homebox/backend/internal/data/repo" + "io" ) func (svc *ItemService) AttachmentPath(ctx context.Context, attachmentID uuid.UUID) (*ent.Document, error) { @@ -77,14 +75,19 @@ func (svc *ItemService) AttachmentDelete(ctx context.Context, gid, itemID, attac return err } + documentID := attachment.Edges.Document.GetID() + // Delete the attachment err = svc.repo.Attachments.Delete(ctx, attachmentID) if err != nil { return err } - // Remove File - err = os.Remove(attachment.Edges.Document.Path) + // Delete the document, this function also removes the file + err = svc.repo.Docs.Delete(ctx, documentID) + if err != nil { + return err + } return err }