Skip to content

Commit 1556f02

Browse files
committed
Add http/https support
1 parent 5ab0118 commit 1556f02

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

src/Avalonia.Svg.Skia/SvgSource.cs

+24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.ComponentModel;
3+
using System.Diagnostics;
34
using System.IO;
5+
using System.Net.Http;
46
using Avalonia.Platform;
57
using Svg.Skia;
68

@@ -27,6 +29,28 @@ public class SvgSource : SKSvg
2729
return source;
2830
}
2931

32+
if (Uri.TryCreate(path, UriKind.Absolute, out var uriHttp) && (uriHttp.Scheme == "http" || uriHttp.Scheme == "https"))
33+
{
34+
try
35+
{
36+
var response = new HttpClient().GetAsync(uriHttp).Result;
37+
if (response.IsSuccessStatusCode)
38+
{
39+
var stream = response.Content.ReadAsStreamAsync().Result;
40+
var source = new T();
41+
source.Load(stream);
42+
return source;
43+
}
44+
}
45+
catch (HttpRequestException e)
46+
{
47+
Debug.WriteLine("Failed to connect to " + uriHttp);
48+
Debug.WriteLine(e.ToString());
49+
}
50+
51+
return default;
52+
}
53+
3054
var uri = path.StartsWith("/") ? new Uri(path, UriKind.Relative) : new Uri(path, UriKind.RelativeOrAbsolute);
3155
if (uri.IsAbsoluteUri && uri.IsFile)
3256
{

src/Avalonia.Svg/SvgSource.cs

+23-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.ComponentModel;
3+
using System.Diagnostics;
34
using System.IO;
5+
using System.Net.Http;
46
using Avalonia.Platform;
57
using ShimSkiaSharp;
68
using SM = Svg.Model;
@@ -29,22 +31,35 @@ public class SvgSource
2931
if (File.Exists(path))
3032
{
3133
var document = SM.SvgExtensions.Open(path);
32-
if (document is { })
34+
return document is { } ? SM.SvgExtensions.ToModel(document, s_assetLoader, out _, out _) : default;
35+
}
36+
37+
if (Uri.TryCreate(path, UriKind.Absolute, out var uriHttp) && (uriHttp.Scheme == "http" || uriHttp.Scheme == "https"))
38+
{
39+
try
3340
{
34-
return SM.SvgExtensions.ToModel(document, s_assetLoader, out _, out _);
41+
var response = new HttpClient().GetAsync(uriHttp).Result;
42+
if (response.IsSuccessStatusCode)
43+
{
44+
var stream = response.Content.ReadAsStreamAsync().Result;
45+
var document = SM.SvgExtensions.Open(stream);
46+
return document is { } ? SM.SvgExtensions.ToModel(document, s_assetLoader, out _, out _) : default;
47+
}
3548
}
49+
catch (HttpRequestException e)
50+
{
51+
Debug.WriteLine("Failed to connect to " + uriHttp);
52+
Debug.WriteLine(e.ToString());
53+
}
54+
3655
return default;
3756
}
3857

3958
var uri = path.StartsWith("/") ? new Uri(path, UriKind.Relative) : new Uri(path, UriKind.RelativeOrAbsolute);
4059
if (uri.IsAbsoluteUri && uri.IsFile)
4160
{
4261
var document = SM.SvgExtensions.Open(uri.LocalPath);
43-
if (document is { })
44-
{
45-
return SM.SvgExtensions.ToModel(document, s_assetLoader, out _, out _);
46-
}
47-
return default;
62+
return document is { } ? SM.SvgExtensions.ToModel(document, s_assetLoader, out _, out _) : default;
4863
}
4964
else
5065
{
@@ -55,11 +70,7 @@ public class SvgSource
5570
return default;
5671
}
5772
var document = SM.SvgExtensions.Open(stream);
58-
if (document is { })
59-
{
60-
return SM.SvgExtensions.ToModel(document, s_assetLoader, out _, out _);
61-
}
62-
return default;
73+
return document is { } ? SM.SvgExtensions.ToModel(document, s_assetLoader, out _, out _) : default;
6374
}
6475
}
6576

0 commit comments

Comments
 (0)