-
-
Notifications
You must be signed in to change notification settings - Fork 0
Preserve file extension #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ebde119
Add optional extension-preserving truncation
stesee 3657bef
Replace bool param with FilenameExtensionHandling enum
stesee 5652e10
Add dotnetfiddle link to README
stesee ccaed78
Remove trailing whitespace in test file
stesee 308c06b
Rename enum value for clarity and add unit tests for filename truncat…
stesee 43855d9
Add test job for multiple OS and upload test artifacts
stesee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| namespace Codeuctivity | ||
| { | ||
| /// <summary> | ||
| /// Defines how file extensions should be handled when truncating long filenames. | ||
| /// </summary> | ||
| public enum FilenameExtensionHandling | ||
| { | ||
| /// <summary> | ||
| /// Preserve the file extension, truncating only the base filename if necessary to fit within the 255-byte limit. | ||
| /// If the extension itself is too long, it will also be truncated to ensure the total filename fits. | ||
| /// </summary> | ||
| PreserveFilenameExtension = 0, | ||
|
|
||
| /// <summary> | ||
| /// Do not preserve the file extension. Truncate the entire filename including the extension. | ||
| /// </summary> | ||
| DoNotPreserveFilenameExtension = 1, | ||
|
|
||
| /// <summary> | ||
| /// Throw an exception if the filename extension is too long to fit within the 255-byte limit. | ||
| /// </summary> | ||
| ThrowWhenFilenameExtensionIsTooLong = 2 | ||
| } | ||
| } |
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
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
88 changes: 88 additions & 0 deletions
88
SanitizeFilenameTests/FilenameTests/TruncateLongFilenames.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| using Codeuctivity; | ||
|
|
||
| namespace SanitizeFilenameTests.FilenameTests | ||
| { | ||
| [Parallelizable(ParallelScope.Fixtures)] | ||
| internal class TruncateLongFilenames : SanitizeFilenamesTestsBase | ||
| { | ||
| public TruncateLongFilenames() | ||
| { | ||
| FileWriteAsserter = new FileWriteAsserter(); | ||
| } | ||
|
|
||
| public FileWriteAsserter FileWriteAsserter { get; } | ||
|
|
||
| [OneTimeTearDown] | ||
| public void TearDown() | ||
| { | ||
| FileWriteAsserter.Dispose(); | ||
| } | ||
|
|
||
| [Test] | ||
| [TestCase(FilenameExtensionHandling.PreserveFilenameExtension, "a.txt")] | ||
| [TestCase(FilenameExtensionHandling.DoNotPreserveFilenameExtension, "aaaaaa")] | ||
| public void ShouldTruncateFileExtensionSpecificBehaviour(FilenameExtensionHandling handling, string expectedSanitizedFilenameEnd) | ||
| { | ||
| var filename = new string('a', 300); | ||
| filename += ".txt"; | ||
| var sanitizedFilename = filename.SanitizeFilename(filenameExtensionHandling: handling); | ||
|
|
||
| Assert.That(sanitizedFilename, Does.EndWith(expectedSanitizedFilenameEnd)); | ||
| Assert.That(FileWriteAsserter.TryWriteFileToTempDirectory(sanitizedFilename), Is.True); | ||
| Assert.That(System.Text.Encoding.UTF8.GetByteCount(sanitizedFilename), Is.LessThanOrEqualTo(255)); | ||
| } | ||
|
|
||
| [Test] | ||
| [TestCase(FilenameExtensionHandling.PreserveFilenameExtension, ".aaaaaa")] | ||
| [TestCase(FilenameExtensionHandling.DoNotPreserveFilenameExtension, "file")] | ||
| public void ShouldPreserveExtensionEvenWhenExceedingMaxLength(FilenameExtensionHandling handling, string expectedSanitizedFilenameStart) | ||
| { | ||
| // Create a filename where even the extension alone exceeds the max length when combined with minimal filename | ||
| var veryLongExtension = "." + new string('a', 300); // 301 bytes for extension alone | ||
| var filename = "file" + veryLongExtension; | ||
| var sanitizedFilename = filename.SanitizeFilename(filenameExtensionHandling: handling); | ||
|
|
||
| // Should preserve the extension despite length constraints | ||
| Assert.That(sanitizedFilename, Does.StartWith(expectedSanitizedFilenameStart)); | ||
| Assert.That(System.Text.Encoding.UTF8.GetByteCount(sanitizedFilename), Is.LessThanOrEqualTo(255)); | ||
| Assert.That(FileWriteAsserter.TryWriteFileToTempDirectory(sanitizedFilename), Is.True); | ||
| } | ||
|
|
||
| [Test] | ||
| [TestCase("shortname.txt")] | ||
| [TestCase("another.doc")] | ||
| [TestCase("file.json")] | ||
| public void ShouldPreserveExtensionForShortFileNames(string filename) | ||
| { | ||
| var sanitizedFilename = filename.SanitizeFilename(filenameExtensionHandling: FilenameExtensionHandling.PreserveFilenameExtension); | ||
|
|
||
| var extension = Path.GetExtension(filename); | ||
| Assert.That(sanitizedFilename, Does.EndWith(extension)); | ||
| Assert.That(sanitizedFilename, Is.EqualTo(filename)); | ||
| Assert.That(FileWriteAsserter.TryWriteFileToTempDirectory(sanitizedFilename), Is.True); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ShouldHandleFileWithoutExtension() | ||
| { | ||
| var filename = new string('a', 300); // No extension | ||
| var sanitizedFilename = filename.SanitizeFilename(filenameExtensionHandling: FilenameExtensionHandling.PreserveFilenameExtension); | ||
|
|
||
| Assert.That(System.Text.Encoding.UTF8.GetByteCount(sanitizedFilename), Is.LessThanOrEqualTo(255)); | ||
| Assert.That(FileWriteAsserter.TryWriteFileToTempDirectory(sanitizedFilename), Is.True); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ShouldThrowWhenFilenameExtensionIsTooLong() | ||
| { | ||
| // Create a filename where the extension is very long | ||
| var veryLongExtension = "." + new string('a', 300); | ||
| var filename = "file" + veryLongExtension; | ||
|
|
||
| var ex = Assert.Throws<ArgumentException>(() => | ||
| filename.SanitizeFilename(filenameExtensionHandling: FilenameExtensionHandling.ThrowWhenFilenameExtensionIsTooLong)); | ||
|
|
||
| Assert.That(ex.Message, Does.Contain("extension is too long")); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.