Skip to content

Commit

Permalink
Merge pull request #1849 from qdraw/feature/202411_extensions
Browse files Browse the repository at this point in the history
Test extensions
  • Loading branch information
qdraw authored Nov 29, 2024
2 parents 42cf049 + d1fa81a commit dd33e8a
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 125 deletions.
22 changes: 18 additions & 4 deletions starsky/starsky.foundation.platform/Helpers/FilenamesHelper.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
Expand Down Expand Up @@ -42,14 +43,16 @@ public static bool IsValidFileName(string filename)
public static string GetFileName(string filePath,
IsOsPlatformDelegate? runtimeInformationIsOsPlatform = null)
{
var path = GetFileNameToUrl(filePath);

runtimeInformationIsOsPlatform ??= RuntimeInformation.IsOSPlatform;
if ( !runtimeInformationIsOsPlatform(OSPlatform.Windows) )
{
return Path.GetFileName(filePath);
return Path.GetFileName(path);
}

const string magicString = "!@&#$#";
var systemPath = filePath.Replace("\\", magicString).Replace("/",
var systemPath = path.Replace("\\", magicString).Replace("/",
Path.DirectorySeparatorChar.ToString());
return Path.GetFileName(systemPath)
.Replace(Path.DirectorySeparatorChar.ToString(), "/")
Expand Down Expand Up @@ -92,20 +95,31 @@ public static string GetFileNameWithoutExtension(string filePath)
100)]
private static partial Regex FileExtensionWithoutDotRegex();

private static string GetFileNameToUrl(string filename)
{
var uri = new Uri(filename, UriKind.RelativeOrAbsolute);
var path = uri.IsAbsoluteUri
? uri.LocalPath
: uri.OriginalString.Split('?')[0].Split('#')[0];
return path;
}

/// <summary>
/// Get File Extension without dot
/// </summary>
/// <param name="filename">fileName or filepath</param>
/// <returns></returns>
public static string GetFileExtensionWithoutDot(string filename)
{
var path = GetFileNameToUrl(filename);

// ReSharper disable once ConvertIfStatementToReturnStatement
if ( !filename.Contains('.') )
if ( !path.Contains('.') )
{
return string.Empty;
}

return FileExtensionWithoutDotRegex().Match(filename).Value.ToLowerInvariant();
return FileExtensionWithoutDotRegex().Match(path).Value.ToLowerInvariant();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public void GetFileExtensionWithoutDot_Should_Not_Return_Extension_With_Invalid_
var extension = FilenamesHelper.GetFileExtensionWithoutDot(filename);

// Assert
Assert.AreEqual("txt", extension);
Assert.AreEqual("nsion", extension);
}

[TestMethod]
Expand Down
290 changes: 170 additions & 120 deletions starsky/starskytest/starsky.project.web/Helpers/FilenameHelpersTest.cs
Original file line number Diff line number Diff line change
@@ -1,127 +1,177 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using starsky.foundation.platform.Helpers;

namespace starskytest.starsky.project.web.Helpers
namespace starskytest.starsky.project.web.Helpers;

[TestClass]
public sealed class FilenameHelpersTest
{
[TestClass]
public sealed class FilenameHelpersTest
[TestMethod]
public void FilenamesHelper_ValidFileName()
{
var result = FilenamesHelper.IsValidFileName("test.jpg");
Assert.IsTrue(result);
}

[TestMethod]
public void FilenamesHelper_ValidFileName_StartWithUnderscore()
{
var result = FilenamesHelper.IsValidFileName("_.com");
Assert.IsTrue(result);
}

[TestMethod]
public void FilenamesHelper_NonValidFileName()
{
var result = FilenamesHelper.IsValidFileName(".jpg");
Assert.IsFalse(result);
}

[TestMethod]
public void FilenamesHelper_GetFilePath()
{
var result = FilenamesHelper.GetFileName("sdfsdf/test.jpg");
Assert.AreEqual("test.jpg", result);
}


[TestMethod]
public void FilenamesHelper_GetParentPath()
{
var result = FilenamesHelper.GetParentPath("/yes.jpg");
Assert.AreEqual("/", result);
}

[TestMethod]
public void FilenamesHelper_GetParentPathSubDir()
{
var result = FilenamesHelper.GetParentPath("/sub/yes.jpg");
Assert.AreEqual("/sub", result);
}

[TestMethod]
public void FilenamesHelper_GetFileNameWithoutExtension()
{
var result = FilenamesHelper.GetFileNameWithoutExtension("/te_st/test.com");
Assert.AreEqual("test", result);
}

[TestMethod]
public void FilenamesHelper_GetFileNameWithoutExtension_mp4()
{
var result = FilenamesHelper.GetFileNameWithoutExtension("/te_st/test.mp4");
Assert.AreEqual("test", result);
}

[TestMethod]
public void FilenamesHelper_GetFileNameWithoutExtension_example2()
{
var result =
FilenamesHelper.GetFileNameWithoutExtension(
"http://path/Lists/Test/Attachments/1/Document Test.docx");
Assert.AreEqual("Document Test", result);
}

[TestMethod]
public void FilenamesHelper_GetFileNameWithoutExtension_example3()
{
var result =
FilenamesHelper.GetFileNameWithoutExtension(
"/0000000000aaaaa__exifreadingtest00.jpg");
Assert.AreEqual("0000000000aaaaa__exifreadingtest00", result);
}

[TestMethod]
public void FilenamesHelper_GetFileNameWithoutExtension_example4()
{
var result =
FilenamesHelper.GetFileNameWithoutExtension("/0000000000aaaaa__exifreadingtest00");
Assert.AreEqual("0000000000aaaaa__exifreadingtest00", result);
}

[TestMethod]
[DataRow("/folder/test.mp4", "mp4", "test")]
[DataRow("/test.mp4", "mp4", "test")] // lowercase
[DataRow("/test.MP4", "mp4", "test")] // uppercase
[DataRow("/test.jpeg", "jpeg", "test")]
[DataRow("/test_image", "", "test_image")] // no ext
[DataRow("/test.jpeg", "jpeg", "test")]
[DataRow("/test.jpg.php", "php", "test.jpg")]
[DataRow("/test.php%00.jpg", "jpg", "test.php%00")]
[DataRow("/folder/.hiddenfile", "nfile", ".hiddenfile")] // hidden file with no extension
[DataRow("/folder/.hiddenfile.jpg", "jpg", ".hiddenfile")] // hidden file with extension
[DataRow("/folder/file.with.multiple.dots.ext", "ext",
"file.with.multiple.dots")] // file with multiple dots
[DataRow("/folder/file_with_underscore.ext", "ext",
"file_with_underscore")] // file with underscore
[DataRow("/folder/file-with-dash.ext", "ext", "file-with-dash")] // file with dash
[DataRow("/folder/file with spaces.ext", "ext", "file with spaces")] // file with spaces
[DataRow("/folder/file.ext?query=param", "ext", "file")] // file with query parameters
[DataRow("/folder/file.ext#fragment", "ext", "file")] // file with fragment
[DataRow("/folder/file.ext/", "", "")] // file with trailing slash
[DataRow("/folder/file.ext.", "", "file.ext.")] // file with trailing dot
[DataRow("http://path/Lists/Test/Attachments/1/Document Test.docx", "docx", "Document Test")]
[DataRow("http://path/Lists/Test/Attachments/1/Document Test", "", "Document Test")] // no ext
[DataRow("http://path/Lists/Test/Attachments/1/Document Test.", "", "Document Test.")] // no ext
[DataRow("http://path/Lists/Test/Attachments/1/Document Test.docx?query=param", "docx",
"Document Test")]
[DataRow("http://path/Lists/Test/Attachments/1/Document Test.docx#fragment", "docx",
"Document Test")]
[DataRow("http://path/Lists/Test/Attachments/1/Document Test.docx/", "",
"")] // file with trailing slash
public void FilenamesHelper_GetFileExtensionWithoutDot_FileName(string filePath,
string expectedExtension, string expectedFileName)
{
var result = FilenamesHelper.GetFileExtensionWithoutDot(filePath);
var fileName = FilenamesHelper.GetFileNameWithoutExtension(filePath);

Assert.AreEqual(expectedExtension, result);
Assert.AreEqual(expectedFileName, fileName);
}

[TestMethod]
[DataRow("test.jpg", true)]
[DataRow("_.com", true)]
[DataRow(".jpg", false)]
[DataRow("test", false)] // no extension
[DataRow("test.", false)] // trailing dot
[DataRow("test..jpg", false)] // double dot
[DataRow("test.jpg.", false)] // trailing dot after extension
[DataRow("test.jpg.exe", true)] // double extension
[DataRow("[email protected]", false)] // special character in name
[DataRow("test jpg", false)] // space in name
[DataRow("test.jpg ", false)] // trailing space
[DataRow(" test.jpg", false)] // leading space
[DataRow("test.jpg/another.jpg", false)] // slash in name
[DataRow("test.jpg\\another.jpg", false)] // backslash in name
[DataRow("test..", false)] // double trailing dot
[DataRow("test..jpg", false)] // double dot before extension
[DataRow("test.jpg..", false)] // double trailing dot after extension
[DataRow("test.jpg..exe", false)] // double extension with dot
[DataRow("test@jpg", false)] // special character without dot
[DataRow("test jpg", false)] // space without dot
[DataRow("testjpg", false)] // no dot
[DataRow("testjpg.", false)] // trailing dot without extension
[DataRow("testjpg..", false)] // double trailing dot without extension
[DataRow("testjpg..exe", false)] // double extension without dot
public void FilenamesHelper_IsValidFileName(string filename, bool expected)
{
var result = FilenamesHelper.IsValidFileName(filename);
Assert.AreEqual(expected, result);
}

// Get parent path for FilenamesHelper.GetParentPath
[TestMethod]
[DataRow("/yes.jpg", "/")]
[DataRow("/sub/yes.jpg", "/sub")]
[DataRow("/sub/sub2/yes.jpg", "/sub/sub2")]
[DataRow("/sub/sub2/sub3/yes.jpg", "/sub/sub2/sub3")]
[DataRow("/sub/sub2/sub3/sub4/yes.jpg", "/sub/sub2/sub3/sub4")]
[DataRow("/sub/sub2/sub3/sub4/sub5/yes.jpg", "/sub/sub2/sub3/sub4/sub5")]
public void FilenamesHelper_GetParentPath(string filePath, string expected)
{
[TestMethod]
public void FilenamesHelper_ValidFileName()
{
var result = FilenamesHelper.IsValidFileName("test.jpg");
Assert.IsTrue(result);
}

[TestMethod]
public void FilenamesHelper_ValidFileName_StartWithUnderscore()
{
var result = FilenamesHelper.IsValidFileName("_.com");
Assert.IsTrue(result);
}

[TestMethod]
public void FilenamesHelper_NonValidFileName()
{
var result = FilenamesHelper.IsValidFileName(".jpg");
Assert.IsFalse(result);
}

[TestMethod]
public void FilenamesHelper_GetFilePath()
{
var result = FilenamesHelper.GetFileName("sdfsdf/test.jpg");
Assert.AreEqual("test.jpg", result);
}


[TestMethod]
public void FilenamesHelper_GetParentPath()
{
var result = FilenamesHelper.GetParentPath("/yes.jpg");
Assert.AreEqual("/", result);
}

[TestMethod]
public void FilenamesHelper_GetParentPathSubDir()
{
var result = FilenamesHelper.GetParentPath("/sub/yes.jpg");
Assert.AreEqual("/sub", result);
}

[TestMethod]
public void FilenamesHelper_GetFileNameWithoutExtension()
{
var result = FilenamesHelper.GetFileNameWithoutExtension("/te_st/test.com");
Assert.AreEqual("test", result);
}

[TestMethod]
public void FilenamesHelper_GetFileNameWithoutExtension_mp4()
{
var result = FilenamesHelper.GetFileNameWithoutExtension("/te_st/test.mp4");
Assert.AreEqual("test", result);
}

[TestMethod]
public void FilenamesHelper_GetFileNameWithoutExtension_example2()
{
var result =
FilenamesHelper.GetFileNameWithoutExtension(
"http://path/Lists/Test/Attachments/1/Document Test.docx");
Assert.AreEqual("Document Test", result);
}

[TestMethod]
public void FilenamesHelper_GetFileNameWithoutExtension_example3()
{
var result =
FilenamesHelper.GetFileNameWithoutExtension(
"/0000000000aaaaa__exifreadingtest00.jpg");
Assert.AreEqual("0000000000aaaaa__exifreadingtest00", result);
}

[TestMethod]
public void FilenamesHelper_GetFileNameWithoutExtension_example4()
{
var result =
FilenamesHelper.GetFileNameWithoutExtension("/0000000000aaaaa__exifreadingtest00");
Assert.AreEqual("0000000000aaaaa__exifreadingtest00", result);
}

[TestMethod]
public void FilenamesHelper_GetFileExtensionWithoutDot_NoExtension()
{
var result = FilenamesHelper.GetFileExtensionWithoutDot("/test_image");
Assert.AreEqual(string.Empty, result);
}

[TestMethod]
public void FilenamesHelper_GetFileExtensionWithoutDot_Mp4()
{
var result = FilenamesHelper.GetFileExtensionWithoutDot("/test.mp4");
Assert.AreEqual("mp4", result);
}

[TestMethod]
public void FilenamesHelper_GetFileExtensionWithoutDot_UpperCase_Mp4()
{
var result = FilenamesHelper.GetFileExtensionWithoutDot("/test.MP4");
Assert.AreEqual("mp4", result);
}

[TestMethod]
public void FilenamesHelper_GetFileExtensionWithoutDot_FilePath()
{
var result = FilenamesHelper.GetFileExtensionWithoutDot("/folder/test.mp4");
Assert.AreEqual("mp4", result);
}

[TestMethod]
public void FilenamesHelper_GetFileExtensionWithoutDot_Jpeg()
{
var result = FilenamesHelper.GetFileExtensionWithoutDot("/test.jpeg");
Assert.AreEqual("jpeg", result);
}
var result = FilenamesHelper.GetParentPath(filePath);
Assert.AreEqual(expected, result);
}

}

0 comments on commit dd33e8a

Please sign in to comment.