From b4b4680422d252796e3e49e915fdea7ea796157f Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Sun, 19 Jan 2025 13:00:07 +0000 Subject: [PATCH] Double check before delete --- plugin/src/file-operations/file-operations.ts | 2 ++ plugin/src/file-operations/obsidian-file-operations.ts | 7 ++++++- plugin/src/sync-operations/syncer.ts | 7 +++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/plugin/src/file-operations/file-operations.ts b/plugin/src/file-operations/file-operations.ts index 6632799..2dc182f 100644 --- a/plugin/src/file-operations/file-operations.ts +++ b/plugin/src/file-operations/file-operations.ts @@ -7,6 +7,8 @@ export interface FileOperations { getFileSize: (path: RelativePath) => Promise; + exists: (path: RelativePath) => Promise; + getModificationTime: (path: RelativePath) => Promise; // Create and write the file if it doesn't exist. Otherwise, it has the same behavior as write. diff --git a/plugin/src/file-operations/obsidian-file-operations.ts b/plugin/src/file-operations/obsidian-file-operations.ts index 95dcdd0..4afd113 100644 --- a/plugin/src/file-operations/obsidian-file-operations.ts +++ b/plugin/src/file-operations/obsidian-file-operations.ts @@ -39,6 +39,11 @@ export class ObsidianFileOperations implements FileOperations { return new Date((await this.statFile(path)).mtime); } + public async exists(path: RelativePath): Promise { + Logger.getInstance().debug(`Checking existance of ${path}`); + return this.vault.adapter.exists(normalizePath(path)); + } + public async create( path: RelativePath, newContent: Uint8Array @@ -108,7 +113,7 @@ export class ObsidianFileOperations implements FileOperations { public async remove(path: RelativePath): Promise { Logger.getInstance().debug(`Removing file: ${path}`); if (await this.vault.adapter.exists(normalizePath(path))) { - return this.vault.adapter.remove(normalizePath(path)); + await this.vault.adapter.trashSystem(normalizePath(path)); } } diff --git a/plugin/src/sync-operations/syncer.ts b/plugin/src/sync-operations/syncer.ts index e23e692..dea4b5d 100644 --- a/plugin/src/sync-operations/syncer.ts +++ b/plugin/src/sync-operations/syncer.ts @@ -176,6 +176,13 @@ export class Syncer { `Document ${relativePath} has been deleted locally, scheduling sync to delete it` ); + if (await this.operations.exists(relativePath)) { + Logger.getInstance().debug( + `Document ${relativePath} actually exists locally, skipping` + ); + return Promise.resolve(); + } + return this.internalSyncLocallyDeletedFile(relativePath); }) );