Serialier: Using local url for images

This commit is contained in:
Zoe Roux 2021-10-09 22:32:11 +02:00
parent 96494ecf28
commit 504bd5bca8
4 changed files with 51 additions and 11 deletions

View File

@ -18,6 +18,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Linq.Expressions; using System.Linq.Expressions;
namespace Kyoo.Abstractions.Models namespace Kyoo.Abstractions.Models
@ -33,7 +34,8 @@ namespace Kyoo.Abstractions.Models
Show, Show,
/// <summary> /// <summary>
/// The <see cref="LibraryItem"/> is a Movie (a <see cref="Show"/> with <see cref="Models.Show.IsMovie"/> equals to true). /// The <see cref="LibraryItem"/> is a Movie (a <see cref="Show"/> with
/// <see cref="Models.Show.IsMovie"/> equals to true).
/// </summary> /// </summary>
Movie, Movie,
@ -47,7 +49,7 @@ namespace Kyoo.Abstractions.Models
/// A type union between <see cref="Show"/> and <see cref="Collection"/>. /// A type union between <see cref="Show"/> and <see cref="Collection"/>.
/// This is used to list content put inside a library. /// This is used to list content put inside a library.
/// </summary> /// </summary>
public class LibraryItem : IResource, IThumbnails public class LibraryItem : CustomTypeDescriptor, IResource, IThumbnails
{ {
/// <inheritdoc /> /// <inheritdoc />
public int ID { get; set; } public int ID { get; set; }
@ -160,5 +162,11 @@ namespace Kyoo.Abstractions.Models
Images = x.Images, Images = x.Images,
Type = ItemType.Collection Type = ItemType.Collection
}; };
/// <inheritdoc />
public override string GetClassName()
{
return Type.ToString();
}
} }
} }

View File

@ -72,7 +72,7 @@ namespace Kyoo.Core.Tasks
/// <inheritdoc /> /// <inheritdoc />
public TaskParameters GetParameters() public TaskParameters GetParameters()
{ {
return new(); return new TaskParameters();
} }
/// <inheritdoc /> /// <inheritdoc />

View File

@ -16,6 +16,7 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>. // along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using Kyoo.Core.Models.Options;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
@ -33,21 +34,30 @@ namespace Kyoo.Core.Api
/// </summary> /// </summary>
private readonly IHttpContextAccessor _httpContextAccessor; private readonly IHttpContextAccessor _httpContextAccessor;
/// <summary>
/// The options containing the public URL of kyoo, given to <see cref="JsonSerializerContract"/>.
/// </summary>
private readonly IOptions<BasicOptions> _options;
/// <summary> /// <summary>
/// Create a new <see cref="JsonOptions"/>. /// Create a new <see cref="JsonOptions"/>.
/// </summary> /// </summary>
/// <param name="httpContextAccessor"> /// <param name="httpContextAccessor">
/// The http context accessor given to the <see cref="JsonSerializerContract"/>. /// The http context accessor given to the <see cref="JsonSerializerContract"/>.
/// </param> /// </param>
public JsonOptions(IHttpContextAccessor httpContextAccessor) /// <param name="options">
/// The options containing the public URL of kyoo, given to <see cref="JsonSerializerContract"/>.
/// </param>
public JsonOptions(IHttpContextAccessor httpContextAccessor, IOptions<BasicOptions> options)
{ {
_httpContextAccessor = httpContextAccessor; _httpContextAccessor = httpContextAccessor;
_options = options;
} }
/// <inheritdoc /> /// <inheritdoc />
public void Configure(MvcNewtonsoftJsonOptions options) public void Configure(MvcNewtonsoftJsonOptions options)
{ {
options.SerializerSettings.ContractResolver = new JsonSerializerContract(_httpContextAccessor); options.SerializerSettings.ContractResolver = new JsonSerializerContract(_httpContextAccessor, _options);
options.SerializerSettings.Converters.Add(new PeopleRoleConverter()); options.SerializerSettings.Converters.Add(new PeopleRoleConverter());
} }
} }

View File

@ -18,10 +18,13 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection; using System.Reflection;
using Kyoo.Abstractions.Models; using Kyoo.Abstractions.Models;
using Kyoo.Abstractions.Models.Attributes; using Kyoo.Abstractions.Models.Attributes;
using Kyoo.Core.Models.Options;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Serialization;
@ -40,13 +43,20 @@ namespace Kyoo.Core.Api
/// </summary> /// </summary>
private readonly IHttpContextAccessor _httpContextAccessor; private readonly IHttpContextAccessor _httpContextAccessor;
/// <summary>
/// The options containing the public URL of kyoo.
/// </summary>
private readonly IOptions<BasicOptions> _options;
/// <summary> /// <summary>
/// Create a new <see cref="JsonSerializerContract"/>. /// Create a new <see cref="JsonSerializerContract"/>.
/// </summary> /// </summary>
/// <param name="httpContextAccessor">The http context accessor to use.</param> /// <param name="httpContextAccessor">The http context accessor to use.</param>
public JsonSerializerContract(IHttpContextAccessor httpContextAccessor) /// <param name="options">The options containing the public URL of kyoo.</param>
public JsonSerializerContract(IHttpContextAccessor httpContextAccessor, IOptions<BasicOptions> options)
{ {
_httpContextAccessor = httpContextAccessor; _httpContextAccessor = httpContextAccessor;
_options = options;
} }
/// <inheritdoc /> /// <inheritdoc />
@ -97,7 +107,7 @@ namespace Kyoo.Core.Api
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(_options.Value.PublicUrl, id)
}); });
} }
@ -110,6 +120,11 @@ namespace Kyoo.Core.Api
/// </summary> /// </summary>
private class ThumbnailProvider : IValueProvider private class ThumbnailProvider : IValueProvider
{ {
/// <summary>
/// The public address of kyoo.
/// </summary>
private readonly Uri _host;
/// <summary> /// <summary>
/// The index/ID of the image to retrieve/set. /// The index/ID of the image to retrieve/set.
/// </summary> /// </summary>
@ -118,9 +133,11 @@ namespace Kyoo.Core.Api
/// <summary> /// <summary>
/// Create a new <see cref="ThumbnailProvider"/>. /// Create a new <see cref="ThumbnailProvider"/>.
/// </summary> /// </summary>
/// <param name="host">The public address of kyoo.</param>
/// <param name="imageIndex">The index/ID of the image to retrieve/set.</param> /// <param name="imageIndex">The index/ID of the image to retrieve/set.</param>
public ThumbnailProvider(int imageIndex) public ThumbnailProvider(Uri host, int imageIndex)
{ {
_host = host;
_imageIndex = imageIndex; _imageIndex = imageIndex;
} }
@ -135,9 +152,14 @@ namespace Kyoo.Core.Api
/// <inheritdoc /> /// <inheritdoc />
public object GetValue(object target) public object GetValue(object target)
{ {
if (target is IThumbnails thumb) if (target is not (IThumbnails thumb and IResource res)
return thumb.Images?.GetValueOrDefault(_imageIndex); || string.IsNullOrEmpty(thumb.Images?.GetValueOrDefault(_imageIndex)))
return null; return null;
string type = target is ICustomTypeDescriptor descriptor
? descriptor.GetClassName()
: target.GetType().Name;
return new Uri(_host, $"/api/{type}/{res.Slug}/{Images.ImageName[_imageIndex]}".ToLower())
.ToString();
} }
} }
} }