From 6845219e943c95d7f00ecd18035feac15b0921bf Mon Sep 17 00:00:00 2001 From: Alexander Lazarenko Date: Thu, 30 Oct 2025 02:10:01 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=BF=D1=8B=D1=82=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=B7=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=BB=D0=BE=D0=BA=D0=B0=D0=BB=D1=8C=D0=BD=D0=BE=D0=B3=D0=BE=20?= =?UTF-8?q?=D0=B1=D0=BB=D0=BE=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api.go | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/api.go b/api.go index 619bd26..0c59031 100644 --- a/api.go +++ b/api.go @@ -487,19 +487,29 @@ func (ag *Agate) RegisterLocalSnapshot(ctx context.Context, snapshotID, parentID } // 2. Add the file to the blob store - localFile, err := os.Open(localPath) - if err != nil { - return fmt.Errorf("failed to open local snapshot file: %w", err) - } - defer func() { - if err := localFile.Close(); err != nil { - ag.options.Logger.Printf("ERROR: failed to close local file: %v", err) + // Check if blob already exists. If so, we assume it's the correct one and skip overwriting. + // This is to prevent issues when registering a file that is already in the blob store. + _, err = ag.options.BlobStore.GetBlobPath(ctx, snapshotID) + if err == nil { + ag.options.Logger.Printf("blob for snapshot %s already exists, skipping storing it", snapshotID) + } else if errors.Is(err, models.ErrNotFound) { + // Blob does not exist, so we store it. + localFile, err := os.Open(localPath) + if err != nil { + return fmt.Errorf("failed to open local snapshot file: %w", err) } - }() + defer func() { + if err := localFile.Close(); err != nil { + ag.options.Logger.Printf("ERROR: failed to close local file: %v", err) + } + }() - _, err = ag.options.BlobStore.StoreBlob(ctx, snapshotID, localFile) - if err != nil { - return fmt.Errorf("failed to store blob from local file: %w", err) + if _, err = ag.options.BlobStore.StoreBlob(ctx, snapshotID, localFile); err != nil { + return fmt.Errorf("failed to store blob from local file: %w", err) + } + } else { + // Another error occurred when checking for the blob. + return fmt.Errorf("failed to check for existing blob: %w", err) } // 3. Create and save snapshot metadata