diff options
| author | Joshua M. Boniface <joshua@boniface.me> | 2020-11-21 17:20:31 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-11-21 17:20:31 -0500 |
| commit | bf54b5579c15bdf8bb6e3dd6934ea23c45a62573 (patch) | |
| tree | 1b2df4bdf89a70f578b72240969416034afc33db /Jellyfin.Api/ModelBinders/LegacyDateTimeModelBinder.cs | |
| parent | 4a81ee43dc7aed94012c312a8262a1426be9b6d9 (diff) | |
| parent | b707d8e09d776a1dd1f0cdd6cf13cd1f3e638028 (diff) | |
Merge branch 'master' into defer_image_fetching
Diffstat (limited to 'Jellyfin.Api/ModelBinders/LegacyDateTimeModelBinder.cs')
| -rw-r--r-- | Jellyfin.Api/ModelBinders/LegacyDateTimeModelBinder.cs | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/Jellyfin.Api/ModelBinders/LegacyDateTimeModelBinder.cs b/Jellyfin.Api/ModelBinders/LegacyDateTimeModelBinder.cs new file mode 100644 index 000000000..e1cb725f3 --- /dev/null +++ b/Jellyfin.Api/ModelBinders/LegacyDateTimeModelBinder.cs @@ -0,0 +1,49 @@ +using System; +using System.Globalization; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc.ModelBinding; +using Microsoft.AspNetCore.Mvc.ModelBinding.Binders; +using Microsoft.Extensions.Logging; + +namespace Jellyfin.Api.ModelBinders +{ + /// <summary> + /// DateTime model binder. + /// </summary> + public class LegacyDateTimeModelBinder : IModelBinder + { + // Borrowed from the DateTimeModelBinderProvider + private const DateTimeStyles SupportedStyles = DateTimeStyles.AdjustToUniversal | DateTimeStyles.AllowWhiteSpaces; + private readonly DateTimeModelBinder _defaultModelBinder; + + /// <summary> + /// Initializes a new instance of the <see cref="LegacyDateTimeModelBinder"/> class. + /// </summary> + /// <param name="loggerFactory">Instance of the <see cref="ILoggerFactory"/> interface.</param> + public LegacyDateTimeModelBinder(ILoggerFactory loggerFactory) + { + _defaultModelBinder = new DateTimeModelBinder(SupportedStyles, loggerFactory); + } + + /// <inheritdoc /> + public Task BindModelAsync(ModelBindingContext bindingContext) + { + var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); + if (valueProviderResult.Values.Count == 1) + { + var dateTimeString = valueProviderResult.FirstValue; + // Mark Played Item. + if (DateTime.TryParseExact(dateTimeString, "yyyyMMddHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out var dateTime)) + { + bindingContext.Result = ModelBindingResult.Success(dateTime); + } + else + { + return _defaultModelBinder.BindModelAsync(bindingContext); + } + } + + return Task.CompletedTask; + } + } +} |
