2020-10-01 17:56:59 +00:00
|
|
|
using System;
|
2020-11-12 15:06:25 +00:00
|
|
|
using System.Collections.Generic;
|
2020-10-01 17:56:59 +00:00
|
|
|
using System.ComponentModel;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
2020-11-12 15:06:25 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2020-10-01 17:56:59 +00:00
|
|
|
|
|
|
|
namespace Jellyfin.Api.ModelBinders
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Comma delimited array model binder.
|
|
|
|
/// Returns an empty array of specified type if there is no query parameter.
|
|
|
|
/// </summary>
|
|
|
|
public class CommaDelimitedArrayModelBinder : IModelBinder
|
|
|
|
{
|
2020-11-12 15:06:25 +00:00
|
|
|
private readonly ILogger<CommaDelimitedArrayModelBinder> _logger;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="CommaDelimitedArrayModelBinder"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="logger">Instance of the <see cref="ILogger{CommaDelimitedArrayModelBinder}"/> interface.</param>
|
|
|
|
public CommaDelimitedArrayModelBinder(ILogger<CommaDelimitedArrayModelBinder> logger)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2020-10-01 17:56:59 +00:00
|
|
|
/// <inheritdoc/>
|
|
|
|
public Task BindModelAsync(ModelBindingContext bindingContext)
|
|
|
|
{
|
|
|
|
var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
|
2020-10-29 22:11:40 +00:00
|
|
|
var elementType = bindingContext.ModelType.GetElementType() ?? bindingContext.ModelType.GenericTypeArguments[0];
|
2020-10-01 20:04:53 +00:00
|
|
|
var converter = TypeDescriptor.GetConverter(elementType);
|
2020-10-01 17:56:59 +00:00
|
|
|
|
2020-10-01 20:04:53 +00:00
|
|
|
if (valueProviderResult.Length > 1)
|
2020-10-01 17:56:59 +00:00
|
|
|
{
|
2020-11-12 15:06:25 +00:00
|
|
|
var typedValues = GetParsedResult(valueProviderResult.Values, elementType, converter);
|
|
|
|
bindingContext.Result = ModelBindingResult.Success(typedValues);
|
2020-10-01 17:56:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-01 20:04:53 +00:00
|
|
|
var value = valueProviderResult.FirstValue;
|
|
|
|
|
|
|
|
if (value != null)
|
|
|
|
{
|
2020-11-14 15:13:53 +00:00
|
|
|
var splitValues = value.Split(',', StringSplitOptions.RemoveEmptyEntries);
|
2020-11-12 15:06:25 +00:00
|
|
|
var typedValues = GetParsedResult(splitValues, elementType, converter);
|
2020-10-01 20:04:53 +00:00
|
|
|
bindingContext.Result = ModelBindingResult.Success(typedValues);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var emptyResult = Array.CreateInstance(elementType, 0);
|
|
|
|
bindingContext.Result = ModelBindingResult.Success(emptyResult);
|
|
|
|
}
|
2020-10-01 17:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
2020-11-12 15:06:25 +00:00
|
|
|
|
|
|
|
private Array GetParsedResult(IReadOnlyList<string> values, Type elementType, TypeConverter converter)
|
|
|
|
{
|
|
|
|
var parsedValues = new object?[values.Count];
|
|
|
|
var convertedCount = 0;
|
|
|
|
for (var i = 0; i < values.Count; i++)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-11-12 16:44:10 +00:00
|
|
|
parsedValues[i] = converter.ConvertFromString(values[i].Trim());
|
2020-11-12 15:06:25 +00:00
|
|
|
convertedCount++;
|
|
|
|
}
|
|
|
|
catch (FormatException e)
|
|
|
|
{
|
|
|
|
_logger.LogWarning(e, "Error converting value.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var typedValues = Array.CreateInstance(elementType, convertedCount);
|
|
|
|
var typedValueIndex = 0;
|
|
|
|
for (var i = 0; i < parsedValues.Length; i++)
|
|
|
|
{
|
|
|
|
if (parsedValues[i] != null)
|
|
|
|
{
|
|
|
|
typedValues.SetValue(parsedValues[i], typedValueIndex);
|
|
|
|
typedValueIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return typedValues;
|
|
|
|
}
|
2020-10-01 17:56:59 +00:00
|
|
|
}
|
|
|
|
}
|