mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-06-03 05:34:23 -04:00
Implement blurhash
This commit is contained in:
parent
a6c8105d8c
commit
3cdfc91c93
@ -29,7 +29,7 @@ namespace Kyoo.Abstractions.Models
|
|||||||
public interface IThumbnails
|
public interface IThumbnails
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A poster is a 9/16 format image with the cover of the resource.
|
/// A poster is a 2/3 format image with the cover of the resource.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Image? Poster { get; set; }
|
public Image? Poster { get; set; }
|
||||||
|
|
||||||
@ -82,6 +82,12 @@ namespace Kyoo.Abstractions.Models
|
|||||||
return base.ConvertFrom(context, culture, value)!;
|
return base.ConvertFrom(context, culture, value)!;
|
||||||
return new Image(source);
|
return new Image(source);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -341,8 +341,15 @@ namespace Kyoo.Core.Controllers
|
|||||||
if (obj == null)
|
if (obj == null)
|
||||||
throw new ArgumentNullException(nameof(obj));
|
throw new ArgumentNullException(nameof(obj));
|
||||||
await Validate(obj);
|
await Validate(obj);
|
||||||
// if (obj is IThumbnails thumbs)
|
if (obj is IThumbnails thumbs)
|
||||||
// await _thumbnailsManager.DownloadImages(thumbs);
|
{
|
||||||
|
if (thumbs.Poster != null)
|
||||||
|
Database.Entry(thumbs).Reference(x => x.Poster).TargetEntry.State = EntityState.Added;
|
||||||
|
if (thumbs.Thumbnail != null)
|
||||||
|
Database.Entry(thumbs).Reference(x => x.Thumbnail).TargetEntry.State = EntityState.Added;
|
||||||
|
if (thumbs.Logo != null)
|
||||||
|
Database.Entry(thumbs).Reference(x => x.Logo).TargetEntry.State = EntityState.Added;
|
||||||
|
}
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -427,6 +434,13 @@ namespace Kyoo.Core.Controllers
|
|||||||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
|
||||||
protected virtual Task EditRelations(T resource, T changed, bool resetOld)
|
protected virtual Task EditRelations(T resource, T changed, bool resetOld)
|
||||||
{
|
{
|
||||||
|
if (resource is IThumbnails thumbs)
|
||||||
|
{
|
||||||
|
// FIXME: I think this throws if poster is set to null.
|
||||||
|
Database.Entry(thumbs).Reference(x => x.Poster).TargetEntry.State = EntityState.Modified;
|
||||||
|
Database.Entry(thumbs).Reference(x => x.Thumbnail).TargetEntry.State = EntityState.Modified;
|
||||||
|
Database.Entry(thumbs).Reference(x => x.Logo).TargetEntry.State = EntityState.Modified;
|
||||||
|
}
|
||||||
return Validate(resource);
|
return Validate(resource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,6 +66,8 @@ namespace Kyoo.Core.Controllers
|
|||||||
{
|
{
|
||||||
await base.Create(obj);
|
await base.Create(obj);
|
||||||
_database.Entry(obj).State = EntityState.Added;
|
_database.Entry(obj).State = EntityState.Added;
|
||||||
|
if (obj.Logo != null)
|
||||||
|
_database.Entry(obj).Reference(x => x.Logo).TargetEntry.State = EntityState.Added;
|
||||||
await _database.SaveChangesAsync(() => Get(obj.Slug));
|
await _database.SaveChangesAsync(() => Get(obj.Slug));
|
||||||
OnResourceCreated(obj);
|
OnResourceCreated(obj);
|
||||||
return obj;
|
return obj;
|
||||||
|
@ -20,7 +20,7 @@ using System;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using BlurHashSharp.SkiaSharp;
|
using Blurhash.SkiaSharp;
|
||||||
using Kyoo.Abstractions.Controllers;
|
using Kyoo.Abstractions.Controllers;
|
||||||
using Kyoo.Abstractions.Models;
|
using Kyoo.Abstractions.Models;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
@ -62,28 +62,33 @@ namespace Kyoo.Core.Controllers
|
|||||||
await reader.CopyToAsync(file);
|
await reader.CopyToAsync(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task _DownloadImage(string? url, string localPath, string what)
|
private async Task _DownloadImage(Image? image, string localPath, string what)
|
||||||
{
|
{
|
||||||
if (url == null)
|
if (image == null)
|
||||||
return;
|
return;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Downloading image {What}", what);
|
_logger.LogInformation("Downloading image {What}", what);
|
||||||
|
|
||||||
HttpClient client = _clientFactory.CreateClient();
|
HttpClient client = _clientFactory.CreateClient();
|
||||||
HttpResponseMessage response = await client.GetAsync(url);
|
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();
|
||||||
SKBitmap bitmap = SKBitmap.Decode(reader);
|
using SKCodec codec = SKCodec.Create(reader);
|
||||||
|
SKImageInfo info = codec.Info;
|
||||||
|
info.ColorType = SKColorType.Rgba8888;
|
||||||
|
SKBitmap bitmap = SKBitmap.Decode(codec, info);
|
||||||
|
|
||||||
bitmap.Resize(new SKSizeI(bitmap.Width, bitmap.Height), SKFilterQuality.High);
|
bitmap.Resize(new SKSizeI(bitmap.Width, bitmap.Height), SKFilterQuality.High);
|
||||||
await _WriteTo(bitmap, $"{localPath}.{ImageQuality.High.ToString().ToLowerInvariant()}.jpg");
|
await _WriteTo(bitmap, $"{localPath}.{ImageQuality.High.ToString().ToLowerInvariant()}.jpg");
|
||||||
|
|
||||||
bitmap.Resize(new SKSizeI(bitmap.Width, bitmap.Height), SKFilterQuality.Medium);
|
bitmap.Resize(new SKSizeI((int)(bitmap.Width / 1.5), (int)(bitmap.Height / 1.5)), SKFilterQuality.Medium);
|
||||||
await _WriteTo(bitmap, $"{localPath}.{ImageQuality.Medium.ToString().ToLowerInvariant()}.jpg");
|
await _WriteTo(bitmap, $"{localPath}.{ImageQuality.Medium.ToString().ToLowerInvariant()}.jpg");
|
||||||
|
|
||||||
bitmap.Resize(new SKSizeI(bitmap.Width, bitmap.Height), SKFilterQuality.Low);
|
bitmap.Resize(new SKSizeI(bitmap.Width / 2, bitmap.Height / 2), SKFilterQuality.Low);
|
||||||
await _WriteTo(bitmap, $"{localPath}.{ImageQuality.Low.ToString().ToLowerInvariant()}.jpg");
|
await _WriteTo(bitmap, $"{localPath}.{ImageQuality.Low.ToString().ToLowerInvariant()}.jpg");
|
||||||
|
|
||||||
|
image.Blurhash = Blurhasher.Encode(bitmap, 4, 3);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@ -99,9 +104,9 @@ namespace Kyoo.Core.Controllers
|
|||||||
throw new ArgumentNullException(nameof(item));
|
throw new ArgumentNullException(nameof(item));
|
||||||
|
|
||||||
string name = item is IResource res ? res.Slug : "???";
|
string name = item is IResource res ? res.Slug : "???";
|
||||||
await _DownloadImage(item.Poster?.Source, _GetBaseImagePath(item, "poster"), $"The poster of {name}");
|
await _DownloadImage(item.Poster, _GetBaseImagePath(item, "poster"), $"The poster of {name}");
|
||||||
await _DownloadImage(item.Thumbnail?.Source, _GetBaseImagePath(item, "thumbnail"), $"The poster of {name}");
|
await _DownloadImage(item.Thumbnail, _GetBaseImagePath(item, "thumbnail"), $"The poster of {name}");
|
||||||
await _DownloadImage(item.Logo?.Source, _GetBaseImagePath(item, "logo"), $"The poster of {name}");
|
await _DownloadImage(item.Logo, _GetBaseImagePath(item, "logo"), $"The poster of {name}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string _GetBaseImagePath<T>(T item, string image)
|
private static string _GetBaseImagePath<T>(T item, string image)
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="AspNetCore.Proxy" Version="4.4.0" />
|
<PackageReference Include="AspNetCore.Proxy" Version="4.4.0" />
|
||||||
<PackageReference Include="BlurHashSharp.SkiaSharp" Version="1.3.0" />
|
<PackageReference Include="Blurhash.SkiaSharp" Version="2.0.0" />
|
||||||
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.9" />
|
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.9" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.9" />
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.9" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
|
@ -135,10 +135,8 @@ namespace Kyoo.Core.Api
|
|||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public override async Task<ActionResult<T>> Create([FromBody] T resource)
|
public override async Task<ActionResult<T>> Create([FromBody] T resource)
|
||||||
{
|
{
|
||||||
// TODO: Remove this method and use a websocket API to do that.
|
await _thumbs.DownloadImages(resource);
|
||||||
ActionResult<T> ret = await base.Create(resource);
|
return await base.Create(resource);
|
||||||
await _thumbs.DownloadImages(ret.Value);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,46 +0,0 @@
|
|||||||
// Kyoo - A portable and vast media library solution.
|
|
||||||
// Copyright (c) Kyoo.
|
|
||||||
//
|
|
||||||
// See AUTHORS.md and LICENSE file in the project root for full license information.
|
|
||||||
//
|
|
||||||
// Kyoo is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// any later version.
|
|
||||||
//
|
|
||||||
// Kyoo is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU General Public License
|
|
||||||
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using Kyoo.Abstractions.Models;
|
|
||||||
using Newtonsoft.Json;
|
|
||||||
using Newtonsoft.Json.Linq;
|
|
||||||
|
|
||||||
namespace Kyoo.Core.Api
|
|
||||||
{
|
|
||||||
public class ImageConverter : JsonConverter<Image>
|
|
||||||
{
|
|
||||||
/// <inheritdoc />
|
|
||||||
public override void WriteJson(JsonWriter writer, Image value, JsonSerializer serializer)
|
|
||||||
{
|
|
||||||
JObject obj = JObject.FromObject(value, serializer);
|
|
||||||
obj.WriteTo(writer);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public override Image ReadJson(JsonReader reader,
|
|
||||||
Type objectType,
|
|
||||||
Image existingValue,
|
|
||||||
bool hasExistingValue,
|
|
||||||
JsonSerializer serializer)
|
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -78,29 +78,28 @@ namespace Kyoo.Core.Api
|
|||||||
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
|
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
|
||||||
{
|
{
|
||||||
IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
|
IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);
|
||||||
if (!type.IsAssignableTo(typeof(Image)))
|
// if (!type.IsAssignableTo(typeof(Image)))
|
||||||
return properties;
|
// return properties;
|
||||||
// foreach ((int id, string image) in Images.ImageName)
|
// foreach ((int id, string image) in Images.ImageName)
|
||||||
// {
|
// {
|
||||||
// properties.Add(new JsonProperty
|
// properties.Add(new JsonProperty
|
||||||
// {
|
// {
|
||||||
// DeclaringType = type,
|
// DeclaringType = type,
|
||||||
// PropertyName = image.ToLower(),
|
// PropertyName = image.ToLower(),
|
||||||
// UnderlyingName = image,
|
// UnderlyingName = image,
|
||||||
// PropertyType = typeof(string),
|
// PropertyType = typeof(string),
|
||||||
// Readable = true,
|
// Readable = true,
|
||||||
// Writable = false,
|
// Writable = false,
|
||||||
// ItemIsReference = false,
|
// ItemIsReference = false,
|
||||||
// TypeNameHandling = TypeNameHandling.None,
|
// TypeNameHandling = TypeNameHandling.None,
|
||||||
// ShouldSerialize = x =>
|
// ShouldSerialize = x =>
|
||||||
// {
|
// {
|
||||||
// IThumbnails thumb = (IThumbnails)x;
|
// IThumbnails thumb = (IThumbnails)x;
|
||||||
// return thumb?.Images?.ContainsKey(id) == true;
|
// return thumb?.Images?.ContainsKey(id) == true;
|
||||||
// },
|
// },
|
||||||
// ValueProvider = new ThumbnailProvider(id)
|
// ValueProvider = new ThumbnailProvider(id)
|
||||||
// });
|
// });
|
||||||
// }
|
// }
|
||||||
|
|
||||||
return properties;
|
return properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user