Refactors path handling in EmbedProcessor
Introduces toVaultRelativePath method to centralize path conversions, reducing code duplication and improving maintainability. Extracts image download completion logic into handleDownloadComplete for clarity. Relates to obsidian-bot-review
This commit is contained in:
2 files changed
+76
-52
No files matched your search
@@ -255,4 +255,36 @@ export class PathUtils {
|
|||||||
|
|
||||||
return false;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -14,6 +14,8 @@ import * as path from 'path';
|
|||||||
import * as https from 'https';
|
import * as https from 'https';
|
||||||
import * as http from 'http';
|
import * as http from 'http';
|
||||||
|
|
||||||
|
const execAsync = util.promisify(exec);
|
||||||
|
|
||||||
export class EmbedProcessor {
|
export class EmbedProcessor {
|
||||||
private readonly pathUtils: PathUtils;
|
private readonly pathUtils: PathUtils;
|
||||||
|
|
||||||
@@ -21,6 +23,44 @@ export class EmbedProcessor {
|
|||||||
this.pathUtils = new PathUtils(plugin.app);
|
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<void> {
|
||||||
|
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
|
* Resolve a local file path using Obsidian's API and filesystem fallbacks
|
||||||
* @param decodedPath The decoded (non-URL-encoded) path to resolve
|
* @param decodedPath The decoded (non-URL-encoded) path to resolve
|
||||||
@@ -144,41 +184,8 @@ export class EmbedProcessor {
|
|||||||
});
|
});
|
||||||
|
|
||||||
response.on('end', () => {
|
response.on('end', () => {
|
||||||
void (async () => {
|
void this.handleDownloadComplete(chunks, outputPath, resolve);
|
||||||
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);
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
});
|
|
||||||
|
|
||||||
response.on('error', (err) => {
|
response.on('error', (err) => {
|
||||||
console.error(`Export: Error receiving image data: ${err.message}`);
|
console.error(`Export: Error receiving image data: ${err.message}`);
|
||||||
@@ -361,8 +368,6 @@ export class EmbedProcessor {
|
|||||||
|
|
||||||
const convertedImagePath = this.pathUtils.joinPath(vaultTempImagesDir, pngFileName);
|
const convertedImagePath = this.pathUtils.joinPath(vaultTempImagesDir, pngFileName);
|
||||||
|
|
||||||
const execAsync = util.promisify(exec);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Use PathResolver to get the correct ImageMagick path
|
// Use PathResolver to get the correct ImageMagick path
|
||||||
const { PathResolver } = await import('./PathResolver');
|
const { PathResolver } = await import('./PathResolver');
|
||||||
@@ -556,21 +561,8 @@ export class EmbedProcessor {
|
|||||||
|
|
||||||
// Copy file using vault adapter
|
// Copy file using vault adapter
|
||||||
// Convert absolute paths to vault-relative paths for adapter operations
|
// Convert absolute paths to vault-relative paths for adapter operations
|
||||||
let relativeSourcePath = imagePath;
|
const relativeSourcePath = this.pathUtils.toVaultRelativePath(imagePath);
|
||||||
if (imagePath.startsWith(vaultBasePath)) {
|
const relativeVaultImagePath = this.pathUtils.toVaultRelativePath(vaultImagePath);
|
||||||
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 sourceBuffer = await this.plugin.app.vault.adapter.readBinary(relativeSourcePath);
|
const sourceBuffer = await this.plugin.app.vault.adapter.readBinary(relativeSourcePath);
|
||||||
await this.plugin.app.vault.adapter.writeBinary(relativeVaultImagePath, sourceBuffer);
|
await this.plugin.app.vault.adapter.writeBinary(relativeVaultImagePath, sourceBuffer);
|
||||||
|
|||||||
Reference in new issue
Block a user