diff --git a/src/core/PathUtils.ts b/src/core/PathUtils.ts index 5356ad8..6e32830 100644 --- a/src/core/PathUtils.ts +++ b/src/core/PathUtils.ts @@ -255,4 +255,36 @@ export class PathUtils { return false; } + + /** + * Converts an absolute path to a vault-relative path. + * + * This method is used to convert absolute filesystem paths to vault-relative + * paths suitable for use with Obsidian's vault adapter operations. If the path + * does not start with the vault base path, it is returned unchanged. + * + * @param absolutePath - The absolute path to convert + * @returns The vault-relative path (without leading slash) or the original path if not within vault + * + * @example + * ```typescript + * const pathUtils = new PathUtils(app); + * const vaultPath = '/Users/name/vault'; + * const absolutePath = '/Users/name/vault/folder/file.md'; + * const relative = pathUtils.toVaultRelativePath(absolutePath); + * // Returns: 'folder/file.md' + * ``` + */ + toVaultRelativePath(absolutePath: string): string { + const vaultBasePath = this.getVaultPath(); + if (!absolutePath.startsWith(vaultBasePath)) { + return absolutePath; + } + + let relativePath = absolutePath.substring(vaultBasePath.length); + if (relativePath.startsWith('/') || relativePath.startsWith('\\')) { + relativePath = relativePath.substring(1); + } + return relativePath; + } } \ No newline at end of file diff --git a/src/plugin/EmbedProcessor.ts b/src/plugin/EmbedProcessor.ts index db4293e..8df5569 100644 --- a/src/plugin/EmbedProcessor.ts +++ b/src/plugin/EmbedProcessor.ts @@ -14,6 +14,8 @@ import * as path from 'path'; import * as https from 'https'; import * as http from 'http'; +const execAsync = util.promisify(exec); + export class EmbedProcessor { private readonly pathUtils: PathUtils; @@ -21,6 +23,44 @@ export class EmbedProcessor { this.pathUtils = new PathUtils(plugin.app); } + /** + * Handle completion of image download and write to vault. + * @param chunks Downloaded image data chunks + * @param outputPath Absolute path where the image should be written + * @param resolve Promise resolver to call with result + */ + private async handleDownloadComplete( + chunks: Buffer[], + outputPath: string, + resolve: (value: string | null) => void + ): Promise { + try { + // Check if file already exists to avoid overwriting + const exists = await this.pathUtils.fileExists(outputPath); + if (exists) { + console.warn(`Export: Image file already exists, using existing: ${outputPath}`); + resolve(outputPath); + return; + } + + // Combine chunks and write using vault adapter + const buffer = Buffer.concat(chunks); + // Convert Buffer to ArrayBuffer for vault adapter + const arrayBuffer = buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength); + + // Convert absolute path to vault-relative for adapter operations + const relativeOutputPath = this.pathUtils.toVaultRelativePath(outputPath); + + await this.plugin.app.vault.adapter.writeBinary(relativeOutputPath, arrayBuffer); + + console.debug(`Export:Successfully downloaded image to: ${outputPath}`); + resolve(outputPath); + } catch (err) { + console.error(`Export: Error writing image file: ${err.message}`); + resolve(null); + } + } + /** * Resolve a local file path using Obsidian's API and filesystem fallbacks * @param decodedPath The decoded (non-URL-encoded) path to resolve @@ -144,41 +184,8 @@ export class EmbedProcessor { }); response.on('end', () => { - void (async () => { - try { - // Check if file already exists to avoid overwriting - const exists = await this.pathUtils.fileExists(outputPath); - if (exists) { - console.warn(`Export: Image file already exists, using existing: ${outputPath}`); - resolve(outputPath); - return; - } - - // Combine chunks and write using vault adapter - const buffer = Buffer.concat(chunks); - // Convert Buffer to ArrayBuffer for vault adapter - const arrayBuffer = buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength); - - // Convert absolute path to vault-relative for adapter operations - const vaultBasePath = this.pathUtils.getVaultPath(); - let relativeOutputPath = outputPath; - if (outputPath.startsWith(vaultBasePath)) { - relativeOutputPath = outputPath.substring(vaultBasePath.length); - if (relativeOutputPath.startsWith('/') || relativeOutputPath.startsWith('\\')) { - relativeOutputPath = relativeOutputPath.substring(1); - } - } - - await this.plugin.app.vault.adapter.writeBinary(relativeOutputPath, arrayBuffer); - - console.debug(`Export:Successfully downloaded image to: ${outputPath}`); - resolve(outputPath); - } catch (err) { - console.error(`Export: Error writing image file: ${err.message}`); - resolve(null); - } - })(); - }); + void this.handleDownloadComplete(chunks, outputPath, resolve); + }); response.on('error', (err) => { console.error(`Export: Error receiving image data: ${err.message}`); @@ -361,8 +368,6 @@ export class EmbedProcessor { const convertedImagePath = this.pathUtils.joinPath(vaultTempImagesDir, pngFileName); - const execAsync = util.promisify(exec); - try { // Use PathResolver to get the correct ImageMagick path const { PathResolver } = await import('./PathResolver'); @@ -556,21 +561,8 @@ export class EmbedProcessor { // Copy file using vault adapter // Convert absolute paths to vault-relative paths for adapter operations - let relativeSourcePath = imagePath; - if (imagePath.startsWith(vaultBasePath)) { - relativeSourcePath = imagePath.substring(vaultBasePath.length); - if (relativeSourcePath.startsWith('/') || relativeSourcePath.startsWith('\\')) { - relativeSourcePath = relativeSourcePath.substring(1); - } - } - - let relativeVaultImagePath = vaultImagePath; - if (vaultImagePath.startsWith(vaultBasePath)) { - relativeVaultImagePath = vaultImagePath.substring(vaultBasePath.length); - if (relativeVaultImagePath.startsWith('/') || relativeVaultImagePath.startsWith('\\')) { - relativeVaultImagePath = relativeVaultImagePath.substring(1); - } - } + const relativeSourcePath = this.pathUtils.toVaultRelativePath(imagePath); + const relativeVaultImagePath = this.pathUtils.toVaultRelativePath(vaultImagePath); const sourceBuffer = await this.plugin.app.vault.adapter.readBinary(relativeSourcePath); await this.plugin.app.vault.adapter.writeBinary(relativeVaultImagePath, sourceBuffer);