aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Api/ModelBinders
diff options
context:
space:
mode:
authorJoshua M. Boniface <joshua@boniface.me>2020-11-21 17:17:44 -0500
committerGitHub <noreply@github.com>2020-11-21 17:17:44 -0500
commit509a4d090031c1fc417b287df8be2660dbfcdd57 (patch)
tree9e02f5b70309148c6735dbe3b311fa0ea6be53fb /Jellyfin.Api/ModelBinders
parentfab03942f5ec358c66d2ca87458a456f7b73c93c (diff)
parent2f75f84b6f7d21e9337ee597f8367266a13a27a6 (diff)
Merge pull request #4526 from crobibero/mark-played
Fix marking item as played
Diffstat (limited to 'Jellyfin.Api/ModelBinders')
-rw-r--r--Jellyfin.Api/ModelBinders/LegacyDateTimeModelBinder.cs49
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;
+ }
+ }
+}