2020-11-20 17:22:40 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
2023-01-31 11:18:10 +00:00
|
|
|
|
namespace Jellyfin.Api.ModelBinders;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// DateTime model binder.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class LegacyDateTimeModelBinder : IModelBinder
|
2020-11-20 17:22:40 +00:00
|
|
|
|
{
|
2023-01-31 11:18:10 +00:00
|
|
|
|
// Borrowed from the DateTimeModelBinderProvider
|
|
|
|
|
private const DateTimeStyles SupportedStyles = DateTimeStyles.AdjustToUniversal | DateTimeStyles.AllowWhiteSpaces;
|
|
|
|
|
private readonly DateTimeModelBinder _defaultModelBinder;
|
|
|
|
|
|
2020-11-20 17:22:40 +00:00
|
|
|
|
/// <summary>
|
2023-01-31 11:18:10 +00:00
|
|
|
|
/// Initializes a new instance of the <see cref="LegacyDateTimeModelBinder"/> class.
|
2020-11-20 17:22:40 +00:00
|
|
|
|
/// </summary>
|
2023-01-31 11:18:10 +00:00
|
|
|
|
/// <param name="loggerFactory">Instance of the <see cref="ILoggerFactory"/> interface.</param>
|
|
|
|
|
public LegacyDateTimeModelBinder(ILoggerFactory loggerFactory)
|
2020-11-20 17:22:40 +00:00
|
|
|
|
{
|
2023-01-31 11:18:10 +00:00
|
|
|
|
_defaultModelBinder = new DateTimeModelBinder(SupportedStyles, loggerFactory);
|
|
|
|
|
}
|
2020-11-20 17:22:40 +00:00
|
|
|
|
|
2023-01-31 11:18:10 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public Task BindModelAsync(ModelBindingContext bindingContext)
|
|
|
|
|
{
|
|
|
|
|
var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
|
|
|
|
|
if (valueProviderResult.Values.Count == 1)
|
2020-11-20 17:22:40 +00:00
|
|
|
|
{
|
2023-01-31 11:18:10 +00:00
|
|
|
|
var dateTimeString = valueProviderResult.FirstValue;
|
|
|
|
|
// Mark Played Item.
|
|
|
|
|
if (DateTime.TryParseExact(dateTimeString, "yyyyMMddHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out var dateTime))
|
2020-11-20 17:22:40 +00:00
|
|
|
|
{
|
2023-01-31 11:18:10 +00:00
|
|
|
|
bindingContext.Result = ModelBindingResult.Success(dateTime);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return _defaultModelBinder.BindModelAsync(bindingContext);
|
2020-11-20 17:22:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-31 11:18:10 +00:00
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
2020-11-20 17:22:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|