mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-07-09 03:04:20 -04:00
Support svg images from external websites (#434)
This commit is contained in:
parent
2641a36352
commit
580109666f
@ -24,12 +24,14 @@ using System.Net.Http;
|
|||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using System.Xml;
|
||||||
using Blurhash.SkiaSharp;
|
using Blurhash.SkiaSharp;
|
||||||
using Kyoo.Abstractions.Controllers;
|
using Kyoo.Abstractions.Controllers;
|
||||||
using Kyoo.Abstractions.Models;
|
using Kyoo.Abstractions.Models;
|
||||||
using Kyoo.Abstractions.Models.Exceptions;
|
using Kyoo.Abstractions.Models.Exceptions;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using SkiaSharp;
|
using SkiaSharp;
|
||||||
|
using SKSvg = SkiaSharp.Extended.Svg.SKSvg;
|
||||||
|
|
||||||
namespace Kyoo.Core.Controllers;
|
namespace Kyoo.Core.Controllers;
|
||||||
|
|
||||||
@ -50,6 +52,27 @@ public class ThumbnailsManager(
|
|||||||
await reader.CopyToAsync(file);
|
await reader.CopyToAsync(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private SKBitmap _SKBitmapFrom(Stream reader, bool isSvg)
|
||||||
|
{
|
||||||
|
if (isSvg)
|
||||||
|
{
|
||||||
|
SKSvg svg = new();
|
||||||
|
svg.Load(reader);
|
||||||
|
SKBitmap bitmap = new((int)svg.CanvasSize.Width, (int)svg.CanvasSize.Height);
|
||||||
|
using SKCanvas canvas = new(bitmap);
|
||||||
|
canvas.DrawPicture(svg.Picture);
|
||||||
|
return bitmap;
|
||||||
|
}
|
||||||
|
|
||||||
|
using SKCodec codec = SKCodec.Create(reader);
|
||||||
|
if (codec == null)
|
||||||
|
throw new NotSupportedException("Unsupported codec");
|
||||||
|
|
||||||
|
SKImageInfo info = codec.Info;
|
||||||
|
info.ColorType = SKColorType.Rgba8888;
|
||||||
|
return SKBitmap.Decode(codec, info);
|
||||||
|
}
|
||||||
|
|
||||||
public async Task DownloadImage(Image? image, string what)
|
public async Task DownloadImage(Image? image, string what)
|
||||||
{
|
{
|
||||||
if (image == null)
|
if (image == null)
|
||||||
@ -65,16 +88,10 @@ public class ThumbnailsManager(
|
|||||||
HttpResponseMessage response = await client.GetAsync(image.Source);
|
HttpResponseMessage response = await client.GetAsync(image.Source);
|
||||||
response.EnsureSuccessStatusCode();
|
response.EnsureSuccessStatusCode();
|
||||||
await using Stream reader = await response.Content.ReadAsStreamAsync();
|
await using Stream reader = await response.Content.ReadAsStreamAsync();
|
||||||
using SKCodec codec = SKCodec.Create(reader);
|
using SKBitmap original = _SKBitmapFrom(
|
||||||
if (codec == null)
|
reader,
|
||||||
{
|
isSvg: response.Content.Headers.ContentType?.MediaType?.Contains("svg") == true
|
||||||
logger.LogError("Unsupported codec for {What}", what);
|
);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
SKImageInfo info = codec.Info;
|
|
||||||
info.ColorType = SKColorType.Rgba8888;
|
|
||||||
using SKBitmap original = SKBitmap.Decode(codec, info);
|
|
||||||
|
|
||||||
using SKBitmap high = original.Resize(
|
using SKBitmap high = original.Resize(
|
||||||
new SKSizeI(original.Width, original.Height),
|
new SKSizeI(original.Width, original.Height),
|
||||||
|
@ -27,6 +27,7 @@
|
|||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
|
<PackageReference Include="SkiaSharp.Svg" Version="1.60.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user