Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ export async function tryCopyImage(
const fileName = path.parse(path.basename(imageLink)).name;
const imageLinkMd5 = plugin.settings.fileNameEncode
? md5(imageLink)
: fileName;
: encodeURI(fileName);
const imageExt = path.extname(imageLink);
const ifile = plugin.app.metadataCache.getFirstLinkpathDest(
imageLink,
Expand Down Expand Up @@ -394,6 +394,12 @@ export async function tryCopyImage(
)
.replace(/\\/g, "/");

// Skip if source and target are the same (prevents file corruption)
// This can happen when an attachment is already in the output folder
if (filePath === targetPath) {
continue;
}
Comment on lines +397 to +401
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Critical: Path separator inconsistency breaks self-copy prevention on Windows.

The string comparison filePath === targetPath will fail on Windows when ifile === null because:

  • filePath is constructed using path.join() (line 368), which produces backslashes on Windows
  • targetPath is normalized to forward slashes (line 395)

This defeats the purpose of the guard, potentially allowing file corruption from self-copy in the fallback case.

🔎 Proposed fix to normalize both paths before comparison
-                    // Skip if source and target are the same (prevents file corruption)
-                    // This can happen when an attachment is already in the output folder
-                    if (filePath === targetPath) {
-                        continue;
-                    }
+                    // Skip if source and target are the same (prevents file corruption)
+                    // This can happen when an attachment is already in the output folder
+                    // Normalize both paths to forward slashes for consistent comparison across platforms
+                    if (filePath.replace(/\\/g, "/") === targetPath) {
+                        continue;
+                    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Skip if source and target are the same (prevents file corruption)
// This can happen when an attachment is already in the output folder
if (filePath === targetPath) {
continue;
}
// Skip if source and target are the same (prevents file corruption)
// This can happen when an attachment is already in the output folder
// Normalize both paths to forward slashes for consistent comparison across platforms
if (filePath.replace(/\\/g, "/") === targetPath) {
continue;
}
🤖 Prompt for AI Agents
In src/utils.ts around lines 397-401 (and note filePath built at ~line 368 and
targetPath normalized at ~line 395), the direct string comparison filePath ===
targetPath fails on Windows due to inconsistent path separators; normalize both
paths before comparing by resolving and normalizing each path (e.g. use
path.resolve(path.normalize(...))) and, on Windows (process.platform ===
'win32'), compare their lowercase forms to handle case-insensitivity; replace
the current equality check with the normalized/resolved (and lowercased on
Windows) comparison so the self-copy guard reliably detects identical paths
across platforms.


try {
if (!fileExists(targetPath)) {
if (
Expand Down