Preserve file extension#40
Merged
Merged
Conversation
Introduce a new `preserveFileNameExtension` parameter (defaulting to `true`) on `Sanitize` and `SanitizeFilename` overloads so long filenames can be truncated while keeping their extension. Update truncation logic to split name/extension and enforce the 255-byte limit on the combined result. Also simplify several legacy `#if NETSTANDARD2_0` branches and add coverage for long-name truncation behavior with and without extension preservation.
Replaces the `preserveFileNameExtension` boolean parameter with a `FilenameExtensionHandling` enum, adding a third option to throw when the extension is too long to fit within the 255-byte limit. Updates all call sites and tests accordingly.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces configurable handling of filename extensions during the 255-byte UTF-8 truncation step, allowing callers to preserve (or not preserve) the extension, and optionally throw when an extension is too long.
Changes:
- Added
FilenameExtensionHandlingenum and plumbed it through the public sanitize APIs to control extension preservation behavior. - Updated truncation logic to preserve extensions when requested (including handling overly long extensions).
- Added NUnit coverage for long-filename truncation behaviors and a README “try it yourself” link.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| SanitizeFilenameTests/FilenameTests/TrunacateLongFilenames.cs | Adds new tests for truncation behavior with/without extension preservation (includes a few typos to fix). |
| SanitizeFilename/SanitizeFilenameExtensions.cs | Extends the public extension methods to pass through FilenameExtensionHandling. |
| SanitizeFilename/SanitizeFilename.cs | Updates core sanitize entry points and truncation logic to optionally preserve extensions. |
| SanitizeFilename/FilenameExtensionHandling.cs | Introduces the enum controlling truncation/extension behavior. |
| README.md | Adds a dotnetfiddle link for quick experimentation. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.