Skip to content

Commit

Permalink
Merge pull request #11193 from Youssef1313/uri-extensions
Browse files Browse the repository at this point in the history
fix!: `Uno.Extensions.UriExtensions` shouldn't be public
  • Loading branch information
Youssef1313 authored Feb 6, 2023
2 parents 348b493 + c3b7257 commit 0916b94
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 79 deletions.
1 change: 1 addition & 0 deletions build/PackageDiffIgnore.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9135,6 +9135,7 @@
<Member fullName="Windows.Foundation.IClosable" reason="Shouldn't be visible in .NET and shows as IDisposable instead." />
<Member fullName="Windows.Storage.Streams.InMemoryBuffer" reason="Windows.Storage.Streams.Buffer should be used." />
<Member fullName="Windows.Foundation.HResult" reason="Type is hidden in .NET and shouldn't be used" />
<Member fullName="Uno.Extensions.UriExtensions" reason="Not needed publicly" />
<Member fullName="Uno.Client.ICommandExtensions" reason="Only needed internally" />
</Types>
<Events>
Expand Down
81 changes: 2 additions & 79 deletions src/Uno.Foundation/UriExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,62 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Text;
#if WINDOWS_PHONE || !METRO
using System.IO.IsolatedStorage;
#endif

namespace Uno.Extensions
{
public static class UriExtensions
internal static class UriExtensions
{
private const int EscapeDataStringCharactersMaxLength = 10000;

public static IDictionary<string, string> GetParameters(this Uri uri)
{
return uri
.OriginalString
.Split(new[] { '?', '&' }, StringSplitOptions.RemoveEmptyEntries)
.Select(p => p.Split(new[] { '=' }))
.Where(parts => parts.Length > 1)
.ToDictionary(parts => parts[0], parts => String.Join("=", parts.Skip(1)));
}

internal static Uri TrimEndUriSlash(this Uri uri) => new (uri.OriginalString.TrimEnd("/"));

/// <summary>
/// Get extension of the traget file of the uri.
/// </summary>
/// <param name="uri"></param>
/// <returns></returns>
public static string GetExtension(this Uri uri)
{
var url = uri.OriginalString;

try
{
//Path.GetExtension can throw if invalid path characters are present in the Uri (some of these characters are valid in Uri)
return System.IO.Path.GetExtension(url);
}
catch (ArgumentException)
{
//Suppress the exception (Could cause crash in some cases, e.g. SetImageSource from OnApplyTemplate).

var lastIndex = url.LastIndexOf('.');

if (lastIndex != -1)
{
return url.Substring(lastIndex);
}
//try to manually extract extension from string. This solution is less efficient than Path.GetExtension, but
//will do the job for the vast majority of paths. Moreso, it will not cause any exceptions of its own.
}

return String.Empty;
}
internal static Uri TrimEndUriSlash(this Uri uri) => new(uri.OriginalString.TrimEnd("/"));

internal static bool IsAppData(this Uri uri)
{
Expand All @@ -77,30 +25,5 @@ public static bool IsLocalResource(this Uri uri)

return uri.Scheme.Equals("ms-appx", StringComparison.OrdinalIgnoreCase);
}

/// <summary>
/// Converts a string to its escaped representation.
/// This extension bypasses the Uri.EscapeDataString characters limit.
/// </summary>
/// Source: http://stackoverflow.com/questions/6695208/uri-escapedatastring-invalid-uri-the-uri-string-is-too-long
public static string EscapeDataString(string value)
{
var sb = new StringBuilder();
var loops = value.Length / EscapeDataStringCharactersMaxLength;

for (var i = 0; i <= loops; i++)
{
if (i < loops)
{
sb.Append(Uri.EscapeDataString(value.Substring(EscapeDataStringCharactersMaxLength * i, EscapeDataStringCharactersMaxLength)));
}
else
{
sb.Append(Uri.EscapeDataString(value.Substring(EscapeDataStringCharactersMaxLength * i)));
}
}

return sb.ToString();
}
}
}

0 comments on commit 0916b94

Please sign in to comment.