| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.ComponentModel; |
| | 4 | | using System.Threading.Tasks; |
| | 5 | | using Microsoft.AspNetCore.Mvc.ModelBinding; |
| | 6 | | using Microsoft.Extensions.Logging; |
| | 7 | |
|
| | 8 | | namespace Jellyfin.Api.ModelBinders; |
| | 9 | |
|
| | 10 | | /// <summary> |
| | 11 | | /// Comma delimited collection model binder. |
| | 12 | | /// Returns an empty array of specified type if there is no query parameter. |
| | 13 | | /// </summary> |
| | 14 | | public class CommaDelimitedCollectionModelBinder : IModelBinder |
| | 15 | | { |
| | 16 | | private readonly ILogger<CommaDelimitedCollectionModelBinder> _logger; |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Initializes a new instance of the <see cref="CommaDelimitedCollectionModelBinder"/> class. |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="logger">Instance of the <see cref="ILogger{CommaDelimitedCollectionModelBinder}"/> interface.</para |
| | 22 | | public CommaDelimitedCollectionModelBinder(ILogger<CommaDelimitedCollectionModelBinder> logger) |
| | 23 | | { |
| 139 | 24 | | _logger = logger; |
| 139 | 25 | | } |
| | 26 | |
|
| | 27 | | /// <inheritdoc/> |
| | 28 | | public Task BindModelAsync(ModelBindingContext bindingContext) |
| | 29 | | { |
| 139 | 30 | | var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); |
| 139 | 31 | | var elementType = bindingContext.ModelType.GetElementType() ?? bindingContext.ModelType.GenericTypeArguments[0]; |
| 139 | 32 | | var converter = TypeDescriptor.GetConverter(elementType); |
| | 33 | |
|
| 139 | 34 | | if (valueProviderResult.Length > 1) |
| | 35 | | { |
| 2 | 36 | | var typedValues = GetParsedResult(valueProviderResult.Values, elementType, converter); |
| 2 | 37 | | bindingContext.Result = ModelBindingResult.Success(typedValues); |
| | 38 | | } |
| | 39 | | else |
| | 40 | | { |
| 137 | 41 | | var value = valueProviderResult.FirstValue; |
| | 42 | |
|
| 137 | 43 | | if (value is not null) |
| | 44 | | { |
| 6 | 45 | | var splitValues = value.Split(',', StringSplitOptions.RemoveEmptyEntries); |
| 6 | 46 | | var typedValues = GetParsedResult(splitValues, elementType, converter); |
| 6 | 47 | | bindingContext.Result = ModelBindingResult.Success(typedValues); |
| | 48 | | } |
| | 49 | | else |
| | 50 | | { |
| 131 | 51 | | var emptyResult = Array.CreateInstance(elementType, 0); |
| 131 | 52 | | bindingContext.Result = ModelBindingResult.Success(emptyResult); |
| | 53 | | } |
| | 54 | | } |
| | 55 | |
|
| 139 | 56 | | return Task.CompletedTask; |
| | 57 | | } |
| | 58 | |
|
| | 59 | | private Array GetParsedResult(IReadOnlyList<string> values, Type elementType, TypeConverter converter) |
| | 60 | | { |
| 8 | 61 | | var parsedValues = new object?[values.Count]; |
| 8 | 62 | | var convertedCount = 0; |
| 46 | 63 | | for (var i = 0; i < values.Count; i++) |
| | 64 | | { |
| | 65 | | try |
| | 66 | | { |
| 15 | 67 | | parsedValues[i] = converter.ConvertFromString(values[i].Trim()); |
| 12 | 68 | | convertedCount++; |
| 12 | 69 | | } |
| 3 | 70 | | catch (FormatException e) |
| | 71 | | { |
| 3 | 72 | | _logger.LogDebug(e, "Error converting value."); |
| 3 | 73 | | } |
| | 74 | | } |
| | 75 | |
|
| 8 | 76 | | var typedValues = Array.CreateInstance(elementType, convertedCount); |
| 8 | 77 | | var typedValueIndex = 0; |
| 46 | 78 | | for (var i = 0; i < parsedValues.Length; i++) |
| | 79 | | { |
| 15 | 80 | | if (parsedValues[i] is not null) |
| | 81 | | { |
| 12 | 82 | | typedValues.SetValue(parsedValues[i], typedValueIndex); |
| 12 | 83 | | typedValueIndex++; |
| | 84 | | } |
| | 85 | | } |
| | 86 | |
|
| 8 | 87 | | return typedValues; |
| | 88 | | } |
| | 89 | | } |