Skip to content

Commit

Permalink
Add http/https support
Browse files Browse the repository at this point in the history
  • Loading branch information
wieslawsoltes committed Nov 17, 2022
1 parent 5ab0118 commit 1556f02
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
24 changes: 24 additions & 0 deletions src/Avalonia.Svg.Skia/SvgSource.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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)
{
Expand Down
35 changes: 23 additions & 12 deletions src/Avalonia.Svg/SvgSource.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -29,22 +31,35 @@ 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;
}

var uri = path.StartsWith("/") ? new Uri(path, UriKind.Relative) : new Uri(path, UriKind.RelativeOrAbsolute);
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
{
Expand All @@ -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;
}
}

Expand Down

0 comments on commit 1556f02

Please sign in to comment.