Fix/filename - #167
Conversation
The text extraction tool execution was failing because of the removal of the serializer queue, that was not propagated in this feature: 3dcef59
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the WalkthroughThis change introduces a new Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~15 minutes
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
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. Comment |
| name = name.replace("-", "_") | ||
|
|
||
| # Collapse multiple underscores | ||
| name = re.sub(r"_+", "_", name) |
There was a problem hiding this comment.
Is is a problem to have multiple underscore? I know I have files with them, this case seems legit to me
There was a problem hiding this comment.
openrag/openrag/components/files.py
Lines 21 to 22 in 8bd8f1c
With this code, if there are consecutive special characters, each one is replaced with “_”, which can result in filenames containing many underscores. I added that line for further cleaning, but having multiple underscores doesn’t cause any issues during indexing.
| name = re.sub(r"[^\w\-]", "_", name) | ||
|
|
||
| # Replace hyphens with underscores | ||
| name = name.replace("-", "_") |
There was a problem hiding this comment.
Is hyphens really problematic ? Lot of files have it
There was a problem hiding this comment.
This isn’t an issue during indexation, but it becomes one with Chainlit.
For example, the filename allocution_de_Mr_trump,__au_COP21_-_Hello_20251119065407.txt indexes correctly, but when displayed in the Chainlit UI, some underscores are interpreted as Markdown italics markers, breaking the file links.
-
No issue with filenames like:
allocution_de_Mr_trump,__au_COP21__Hello_20251119065407.txt(only underscores)
allocution_de_Mr_trump,-au-COP21-Hello-20251119065407.txt(only hyphens) -
The issue happens specifically with:
allocution_de_Mr_trump,__au_COP21_-_Hello_20251119065407.txt
as shown in the screenshot.
This suggests that Chainlit doesn’t fail on _ or - individually, but on certain combinations of both.
To keep things simple, we can sanitize the filename for safe display and during indexation. In another file metadata we can store the original name, such as "original_filename".
There was a problem hiding this comment.
Ok, let's keep it like this then and add the original_filename
| ("file__name__here.txt", "file_name_here.txt"), | ||
| # Edge cases | ||
| ("", ""), | ||
| ("file(1).txt", "file_1.txt"), |
There was a problem hiding this comment.
Could you add a test with . in file name? 🙏
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
openrag/components/files.py (1)
73-94: Avoid mutable default{}formetadatainserialize_fileUsing
{}as a default argument is a Python anti-pattern that can cause subtle bugs. Since the function parameter is typed asOptional[Dict], useNoneas the default and initialize inside the function:-async def serialize_file(task_id: str, path: str, metadata: Optional[Dict] = {}): +async def serialize_file(task_id: str, path: str, metadata: Optional[Dict] = None): @@ - serializer = ray.get_actor("DocSerializer", namespace="openrag") - # Kick off the remote task - future = serializer.serialize_document.remote(task_id, path, metadata=metadata) + serializer = ray.get_actor("DocSerializer", namespace="openrag") + # Kick off the remote task + if metadata is None: + metadata = {} + future = serializer.serialize_document.remote(task_id, path, metadata=metadata)Note:
serialize_documentinopenrag/components/indexer/loaders/serializer.pyhas the same issue and should be fixed.
♻️ Duplicate comments (2)
openrag/components/files.py (1)
13-34: sanitize_filename is reasonable; consider Path-based splitting and revisiting hyphen/underscore normalizationThe sanitizer correctly strips problematic characters, normalizes separators, and is idempotent. Two optional refinements to consider:
- Use
Path(filename)(stem/suffix) for name/extension splitting to handle edge cases more robustly, as previously suggested.- Re-evaluate whether converting all hyphens to underscores and collapsing multiple underscores is desired, since both are valid and common in filenames; if not strictly needed, you could keep those characters as-is while still replacing truly problematic ones.
openrag/components/test_files.py (1)
60-82: Good sanitizer test coverage; add cases with dots and path separatorsThe parametrized tests nicely cover spaces, special characters, multiple underscores, and an empty string. To fully exercise
sanitize_filenameand address the earlier request about dots, consider adding cases like:
("file.v1.txt", "file_v1.txt")# dot in basename(".env", "env")# leading dot("folder/file.txt", "folder_file.txt")# path separatorThese will lock in behavior for common edge cases and protect against regressions.
🧹 Nitpick comments (1)
openrag/routers/utils.py (1)
213-236: In‑place filename sanitization is good; consider preserving the original name if neededSanitizing
file.filenamein place after format checks is a good point to normalize names and ensures downstream code (e.g. disk writes) sees the sanitized value. If any part of the system needs to display or log the raw user‑provided filename, consider storing it separately (e.g. inmetadata["original_filename"]) before overwritingfile.filename.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
openrag/components/files.py(1 hunks)openrag/components/test_files.py(3 hunks)openrag/routers/utils.py(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
openrag/routers/utils.py (1)
openrag/components/files.py (1)
sanitize_filename(13-34)
openrag/components/test_files.py (1)
openrag/components/files.py (2)
sanitize_filename(13-34)save_file_to_disk(44-70)
🔇 Additional comments (2)
openrag/routers/utils.py (1)
7-7: Import ofsanitize_filenameis consistent and scoped correctlyBringing
sanitize_filenamein viafrom components.files import sanitize_filenamematches existing import style and keeps routers using the shared utility; no issues here.openrag/components/test_files.py (1)
5-7: Test imports and save_file_to_disk usage look correctUpdating imports to pull
sanitize_filenameandsave_file_to_diskfromcomponents.filesand simplifying thesave_file_to_diskcall keeps the tests aligned with the production module; behavior is unchanged and clear.Also applies to: 19-23
8bd8f1c to
5c8a9fb
Compare
This PR adds a
sanitize_filenameutility function to clean and normalize uploaded filenames, preventing issues from special characters and ensuring consistent file naming.Changes:
sanitize_filenamefunction removes special characters, normalizes separators to underscores, and handles edge casesSummary by CodeRabbit
New Features
Tests
✏️ Tip: You can customize this high-level summary in your review settings.