| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="FileExtensionsAllowedAttribute.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.ComponentModel.DataAnnotations; |
| | | 10 | | using System.Globalization; |
| | | 11 | | using System.IO; |
| | | 12 | | using System.Linq; |
| | | 13 | | using MyNet.IO.FileExtensions; |
| | | 14 | | using MyNet.IO.Localization; |
| | | 15 | | |
| | | 16 | | namespace MyNet.IO.Attributes; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Validation attribute that restricts a file path property to a specific set of allowed file extensions. |
| | | 20 | | /// Extensions are normalized (case-insensitive, leading dot always added) so that both <c>txt</c> and <c>.txt</c> |
| | | 21 | | /// are treated identically. |
| | | 22 | | /// </summary> |
| | | 23 | | [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter)] |
| | | 24 | | public sealed class FileExtensionsAllowedAttribute : ValidationAttribute |
| | | 25 | | { |
| | | 26 | | private readonly HashSet<string> _extensions; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Initializes a new instance of the <see cref="FileExtensionsAllowedAttribute"/> class with the specified allowed |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="extensions">The allowed file extensions.</param> |
| | | 32 | | /// <exception cref="ArgumentNullException">Thrown if the <paramref name="extensions"/> array is null.</exception> |
| | | 33 | | /// <exception cref="ArgumentException">Thrown if the <paramref name="extensions"/> array is empty.</exception> |
| | 36 | 34 | | public FileExtensionsAllowedAttribute(params string[] extensions) |
| | | 35 | | { |
| | 36 | 36 | | ArgumentNullException.ThrowIfNull(extensions); |
| | | 37 | | |
| | 33 | 38 | | _extensions = extensions |
| | 33 | 39 | | .Select(Normalize) |
| | 33 | 40 | | .Where(static x => !string.IsNullOrEmpty(x)) |
| | 33 | 41 | | .ToHashSet(StringComparer.OrdinalIgnoreCase); |
| | | 42 | | |
| | 33 | 43 | | if (_extensions.Count == 0) |
| | 6 | 44 | | throw new ArgumentException("At least one extension must be provided.", nameof(extensions)); |
| | | 45 | | |
| | 27 | 46 | | ErrorMessageResourceName = nameof(InternalResources.FieldXMustContainsAllowedExtensionsYError); |
| | 27 | 47 | | ErrorMessageResourceType = typeof(InternalResources); |
| | 27 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Initializes a new instance of the <see cref="FileExtensionsAllowedAttribute"/> class with the specified allowed |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name="extensions">The allowed file extensions.</param> |
| | | 54 | | /// <exception cref="ArgumentNullException">Thrown if the <paramref name="extensions"/> array is null.</exception> |
| | | 55 | | /// <exception cref="ArgumentException">Thrown if the <paramref name="extensions"/> array is empty.</exception> |
| | | 56 | | public FileExtensionsAllowedAttribute(params FileExtension[] extensions) |
| | 3 | 57 | | : this(extensions.Select(x => x.Value).ToArray()) |
| | | 58 | | { |
| | 3 | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Gets or sets a value indicating whether null or empty file paths are considered valid. If set to <c>true</c>, nu |
| | | 63 | | /// </summary> |
| | | 64 | | public bool AllowEmpty { get; set; } = true; |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Gets the collection of allowed file extensions that are used for validation. The extensions are stored in a hash |
| | | 68 | | /// </summary> |
| | 12 | 69 | | public IReadOnlyCollection<string> Extensions => _extensions; |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Formats the error message to include the field name and the list of allowed extensions. The error message is con |
| | | 73 | | /// </summary> |
| | | 74 | | /// <param name="name">The name of the field being validated.</param> |
| | | 75 | | /// <returns>The formatted error message.</returns> |
| | | 76 | | public override string FormatErrorMessage(string name) |
| | 3 | 77 | | => string.Format(CultureInfo.CurrentCulture, ErrorMessageString, name, string.Join(" | ", _extensions)); |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Determines whether the specified value is valid based on the allowed file extensions. The method checks if the v |
| | | 81 | | /// </summary> |
| | | 82 | | /// <param name="value">The value to validate.</param> |
| | | 83 | | /// <returns><c>true</c> if the value is valid; otherwise, <c>false</c>.</returns> |
| | | 84 | | public override bool IsValid(object? value) |
| | | 85 | | { |
| | 33 | 86 | | if (value is null) |
| | 6 | 87 | | return AllowEmpty; |
| | | 88 | | |
| | 27 | 89 | | if (value is not string path) |
| | 3 | 90 | | return false; |
| | | 91 | | |
| | 24 | 92 | | if (string.IsNullOrWhiteSpace(path)) |
| | 6 | 93 | | return AllowEmpty; |
| | | 94 | | |
| | 18 | 95 | | if (_extensions.Contains("*")) |
| | 9 | 96 | | return true; |
| | | 97 | | |
| | 9 | 98 | | var extension = Normalize(Path.GetExtension(path)); |
| | | 99 | | |
| | 9 | 100 | | return _extensions.Contains(extension); |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Normalizes a file extension by trimming whitespace, converting it to lowercase, and ensuring it starts with a do |
| | | 105 | | /// </summary> |
| | | 106 | | /// <param name="extension">The file extension to normalize.</param> |
| | | 107 | | /// <returns>The normalized file extension.</returns> |
| | | 108 | | private static string Normalize(string extension) |
| | | 109 | | { |
| | 54 | 110 | | if (string.IsNullOrWhiteSpace(extension)) |
| | 6 | 111 | | return string.Empty; |
| | | 112 | | |
| | 48 | 113 | | extension = extension.Trim().ToLower(CultureInfo.CurrentCulture); |
| | | 114 | | |
| | 48 | 115 | | if (extension == "*") |
| | 3 | 116 | | return "*"; |
| | | 117 | | |
| | 45 | 118 | | if (!extension.StartsWith('.')) |
| | 27 | 119 | | extension = "." + extension; |
| | | 120 | | |
| | 45 | 121 | | return extension; |
| | | 122 | | } |
| | | 123 | | } |
| | | 124 | | |