From 37015c8ed6235a6c9a7dd7c1210c02a1a97bb832 Mon Sep 17 00:00:00 2001 From: Jarvis Date: Thu, 25 Sep 2025 12:43:00 +0200 Subject: [PATCH] fix: resolve embedded file path resolution issue in PathUtils.fileExists - Fix pathUtils.fileExists() to handle absolute filesystem paths - Add proper conversion from absolute to vault-relative paths - Add fs fallback for external files outside vault - Resolves 'File not found' errors for embedded PDF, image, and document files - Maintains compatibility with both vault-relative and absolute paths This fixes the path resolution issue that occurred after migrating to vault.adapter, where embedded files were showing as 'not found' because absolute paths were being passed to vault.adapter.exists() which only accepts vault-relative paths. --- src/core/PathUtils.ts | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/core/PathUtils.ts b/src/core/PathUtils.ts index fa8441e..3f46799 100644 --- a/src/core/PathUtils.ts +++ b/src/core/PathUtils.ts @@ -75,13 +75,47 @@ export class PathUtils { /** * Check if file or directory exists + * Handles both vault-relative paths and absolute filesystem paths */ async fileExists(path: string): Promise { - const normalizedPath = normalizePath(path); try { - return await this.app.vault.adapter.exists(normalizedPath); + // First, try as vault-relative path + const normalizedPath = normalizePath(path); + const exists = await this.app.vault.adapter.exists(normalizedPath); + if (exists) { + return true; + } } catch { - return false; + // If vault.adapter fails, path might be absolute } + + // If path is absolute, convert to vault-relative and try again + const vaultBasePath = this.getVaultPath(); + if (path.startsWith(vaultBasePath)) { + try { + // Remove vault base path and leading separator + let relativePath = path.substring(vaultBasePath.length); + if (relativePath.startsWith('/') || relativePath.startsWith('\\')) { + relativePath = relativePath.substring(1); + } + const normalizedRelativePath = normalizePath(relativePath); + return await this.app.vault.adapter.exists(normalizedRelativePath); + } catch { + return false; + } + } + + // For paths outside vault, use fs fallback (needed for external file processing) + if (path.includes('/') && !path.startsWith('.')) { + try { + const fs = require('fs').promises; + await fs.access(path); + return true; + } catch { + return false; + } + } + + return false; } } \ No newline at end of file