diff --git a/src/Avalonia.Svg.Skia/SvgSource.cs b/src/Avalonia.Svg.Skia/SvgSource.cs index 79301a97..05dc3236 100644 --- a/src/Avalonia.Svg.Skia/SvgSource.cs +++ b/src/Avalonia.Svg.Skia/SvgSource.cs @@ -1,6 +1,8 @@ using System; using System.ComponentModel; +using System.Diagnostics; using System.IO; +using System.Net.Http; using Avalonia.Platform; using Svg.Skia; @@ -27,6 +29,28 @@ public class SvgSource : SKSvg return source; } + if (Uri.TryCreate(path, UriKind.Absolute, out var uriHttp) && (uriHttp.Scheme == "http" || uriHttp.Scheme == "https")) + { + try + { + var response = new HttpClient().GetAsync(uriHttp).Result; + if (response.IsSuccessStatusCode) + { + var stream = response.Content.ReadAsStreamAsync().Result; + var source = new T(); + source.Load(stream); + return source; + } + } + catch (HttpRequestException e) + { + Debug.WriteLine("Failed to connect to " + uriHttp); + Debug.WriteLine(e.ToString()); + } + + return default; + } + var uri = path.StartsWith("/") ? new Uri(path, UriKind.Relative) : new Uri(path, UriKind.RelativeOrAbsolute); if (uri.IsAbsoluteUri && uri.IsFile) { diff --git a/src/Avalonia.Svg/SvgSource.cs b/src/Avalonia.Svg/SvgSource.cs index e872c0c6..9083509b 100644 --- a/src/Avalonia.Svg/SvgSource.cs +++ b/src/Avalonia.Svg/SvgSource.cs @@ -1,6 +1,8 @@ using System; using System.ComponentModel; +using System.Diagnostics; using System.IO; +using System.Net.Http; using Avalonia.Platform; using ShimSkiaSharp; using SM = Svg.Model; @@ -29,10 +31,27 @@ public class SvgSource if (File.Exists(path)) { var document = SM.SvgExtensions.Open(path); - if (document is { }) + return document is { } ? SM.SvgExtensions.ToModel(document, s_assetLoader, out _, out _) : default; + } + + if (Uri.TryCreate(path, UriKind.Absolute, out var uriHttp) && (uriHttp.Scheme == "http" || uriHttp.Scheme == "https")) + { + try { - return SM.SvgExtensions.ToModel(document, s_assetLoader, out _, out _); + var response = new HttpClient().GetAsync(uriHttp).Result; + if (response.IsSuccessStatusCode) + { + var stream = response.Content.ReadAsStreamAsync().Result; + var document = SM.SvgExtensions.Open(stream); + return document is { } ? SM.SvgExtensions.ToModel(document, s_assetLoader, out _, out _) : default; + } } + catch (HttpRequestException e) + { + Debug.WriteLine("Failed to connect to " + uriHttp); + Debug.WriteLine(e.ToString()); + } + return default; } @@ -40,11 +59,7 @@ public class SvgSource if (uri.IsAbsoluteUri && uri.IsFile) { var document = SM.SvgExtensions.Open(uri.LocalPath); - if (document is { }) - { - return SM.SvgExtensions.ToModel(document, s_assetLoader, out _, out _); - } - return default; + return document is { } ? SM.SvgExtensions.ToModel(document, s_assetLoader, out _, out _) : default; } else { @@ -55,11 +70,7 @@ public class SvgSource return default; } var document = SM.SvgExtensions.Open(stream); - if (document is { }) - { - return SM.SvgExtensions.ToModel(document, s_assetLoader, out _, out _); - } - return default; + return document is { } ? SM.SvgExtensions.ToModel(document, s_assetLoader, out _, out _) : default; } }