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

View File

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

View File

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

View File

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