Skip to content

fix: resolve attachment export inconsistencies (#108)#121

Merged
bingryan merged 1 commit intomasterfrom
bingryan/analyze-issue-108
Jan 3, 2026
Merged

fix: resolve attachment export inconsistencies (#108)#121
bingryan merged 1 commit intomasterfrom
bingryan/analyze-issue-108

Conversation

@bingryan
Copy link
Copy Markdown
Owner

@bingryan bingryan commented Jan 3, 2026

Summary

  • Fix filename encoding inconsistency between tryCopyImage and tryCopyMarkdownByRead functions
  • Add check to prevent self-copy when source and target paths are identical

Details

Problem (Issue #108)

When exporting a folder with attachments:

  • Some attachments had encoded names (e.g., 02fb6aa80be454d22bffb5ca5d55727e.jpg) while others kept original names
  • Attachments were placed in different folders (attachment vs attachments) inconsistently
  • Some attachment files were corrupted during export

Root Cause

  1. tryCopyImage used fileName directly for encoding
  2. tryCopyMarkdownByRead used encodeURI(fileName) for encoding
  3. No check to prevent copying a file to itself when the attachment was already in the output folder

Fix

  1. Unified encoding logic: Both functions now use encodeURI(fileName) when fileNameEncode is off
  2. Self-copy prevention: Added check to skip copying when filePath === targetPath

Test plan

  • Export a folder with attachments from various locations
  • Verify all attachments use consistent naming (all encoded or all original based on setting)
  • Verify no attachment files are corrupted
  • Verify all attachments go to the configured attachment folder

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced image filename handling to properly encode special characters and unusual naming patterns, ensuring attachments are processed correctly in all scenarios.
    • Resolved potential file corruption issues by preventing redundant copying when image attachments already exist in the output folder, improving stability during file operations.

✏️ Tip: You can customize this high-level summary in your review settings.

- Unify filename encoding between tryCopyImage and tryCopyMarkdownByRead
  (both now use encodeURI(fileName) when fileNameEncode is off)
- Add check to skip copying when source and target paths are identical
  (prevents file corruption for attachments already in output folder)

Fixes issues where:
- Some attachments had encoded names while others didn't
- Attachment files were corrupted during export
- Attachments were placed in different folders inconsistently

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Jan 3, 2026

📝 Walkthrough

Walkthrough

Modified the tryCopyImage function in src/utils.ts to encode filenames using encodeURI() and added a guard condition that skips file copying when source and target paths are identical to prevent file corruption.

Changes

Cohort / File(s) Summary
Image file handling improvements
src/utils.ts
Updated tryCopyImage(): (1) Image filenames now use encodeURI(fileName) for proper URI encoding when constructing imageLinkMd5-derived filenames; (2) Added guard to skip copying when computed source path equals target path, preventing potential file corruption when attachments already exist in output folder.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 A carrot path, now cleanly encoded,
No duplicate files will be corroded!
Two tiny tweaks, both wise and keen,
Keep images safe and pristine—hops unseen 🌟

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: resolve attachment export inconsistencies (#108)' directly relates to the main change: fixing inconsistent filename encoding and self-copy issues in attachment export.
✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e631e1a and a0c0bd5.

📒 Files selected for processing (1)
  • src/utils.ts
🔇 Additional comments (1)
src/utils.ts (1)

358-358: LGTM! Encoding consistency achieved.

This change correctly unifies the filename encoding behavior with tryCopyMarkdownByRead (line 751), addressing the inconsistency mentioned in the PR objectives where tryCopyImage used raw fileName while tryCopyMarkdownByRead used encodeURI(fileName).

Comment on lines +397 to +401
// 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;
}
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.

@bingryan bingryan merged commit d4129f9 into master Jan 3, 2026
5 checks passed
@bingryan bingryan deleted the bingryan/analyze-issue-108 branch January 3, 2026 05:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant