Browse Source

Handle long file paths in UriFromFilePath (#19955)

* fix long path

* Update UriExtensionsTests.cs

* Update StorageProviderHelpers.cs
pull/19997/head
startewho 3 months ago
committed by GitHub
parent
commit
e294245f01
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 18
      src/Avalonia.Base/Platform/Storage/FileIO/StorageProviderHelpers.cs
  2. 11
      tests/Avalonia.Base.UnitTests/Utilities/UriExtensionsTests.cs

18
src/Avalonia.Base/Platform/Storage/FileIO/StorageProviderHelpers.cs

@ -37,11 +37,19 @@ internal static class StorageProviderHelpers
public static Uri UriFromFilePath(string path, bool isDirectory)
{
var uriPath = new StringBuilder(path)
.Replace("%", $"%{(int)'%':X2}")
.Replace("[", $"%{(int)'[':X2}")
.Replace("]", $"%{(int)']':X2}");
var uriPath = new StringBuilder();
bool isLongPath = path.StartsWith(@"\\?\", StringComparison.Ordinal);//Windows long path prefix
if (isLongPath)
{
uriPath.Append(path, 4, path.Length - 4);
}
else
{
uriPath.Append(path);
}
uriPath = uriPath.Replace("%", $"%{(int)'%':X2}")
.Replace("[", $"%{(int)'[':X2}")
.Replace("]", $"%{(int)']':X2}");
if (!path.EndsWith('/') && isDirectory)
{
uriPath.Append('/');

11
tests/Avalonia.Base.UnitTests/Utilities/UriExtensionsTests.cs

@ -40,4 +40,15 @@ public class UriExtensionsTests
Assert.Equal(path, uri.LocalPath);
}
[Theory]
[InlineData(@"\\?\D:\abcdefgh\abcdefgh\abcdefabcdefgh\abcdefghabcdefghabcdefgha\bcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh\abcdefghabcdefghabcdefgha\bcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh",
@"D:\abcdefgh\abcdefgh\abcdefabcdefgh\abcdefghabcdefghabcdefgha\bcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh\abcdefghabcdefghabcdefgha\bcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh")]
public void Should_Convert_Long_File_Path_To_Uri_And_Back(string prepath,string path)
{
var uri = StorageProviderHelpers.UriFromFilePath(prepath, false);
Assert.Equal(path, uri.LocalPath);
}
}

Loading…
Cancel
Save