fix(ImageOptimizer): Convert absolute paths to vault-relative for adapter.remove()
- Fixed vault.adapter.remove() call to use vault-relative paths instead of absolute paths - Added proper path conversion by stripping vault base path and leading separators - Maintains fs.unlink fallback when plugin.app is not available - Prevents potential file removal failures due to incorrect path format - Follows Obsidian API best practices for vault operations 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 file changed
+14
-1
@@ -218,7 +218,20 @@ export class ImageOptimizer {
|
|||||||
// Clean up the original PNG if conversion succeeded
|
// Clean up the original PNG if conversion succeeded
|
||||||
try {
|
try {
|
||||||
if (plugin?.app) {
|
if (plugin?.app) {
|
||||||
await plugin.app.vault.adapter.remove(inputImagePath);
|
// Convert absolute path to vault-relative path for adapter.remove()
|
||||||
|
const pathUtils = new PathUtils(plugin.app);
|
||||||
|
const vaultBasePath = pathUtils.getVaultPath();
|
||||||
|
|
||||||
|
let relativePath = inputImagePath;
|
||||||
|
if (inputImagePath.startsWith(vaultBasePath)) {
|
||||||
|
// Remove vault base path and leading separator
|
||||||
|
relativePath = inputImagePath.substring(vaultBasePath.length);
|
||||||
|
if (relativePath.startsWith('/') || relativePath.startsWith('\\')) {
|
||||||
|
relativePath = relativePath.substring(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await plugin.app.vault.adapter.remove(relativePath);
|
||||||
} else {
|
} else {
|
||||||
const fs = require('fs').promises;
|
const fs = require('fs').promises;
|
||||||
await fs.unlink(inputImagePath);
|
await fs.unlink(inputImagePath);
|
||||||
|
|||||||
Reference in new issue
Block a user