| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ExportFileType.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.IO; |
| | | 10 | | using System.Linq; |
| | | 11 | | |
| | | 12 | | namespace MyNet.UI.ViewModels.Export; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Represents export file constraints and dialog filter information. |
| | | 16 | | /// </summary> |
| | | 17 | | public sealed class ExportFileType |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Initializes a new instance of the <see cref="ExportFileType"/> class. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="defaultExtension">The default extension used for generated file names (for example ".csv").</param> |
| | | 23 | | /// <param name="dialogFilter">The dialog filter string.</param> |
| | | 24 | | /// <param name="allowedExtensions">Optional allowed extensions used for destination validation.</param> |
| | | 25 | | public ExportFileType(string defaultExtension, string dialogFilter, IEnumerable<string>? allowedExtensions = null) |
| | | 26 | | { |
| | 15 | 27 | | ArgumentException.ThrowIfNullOrWhiteSpace(defaultExtension); |
| | 15 | 28 | | ArgumentException.ThrowIfNullOrWhiteSpace(dialogFilter); |
| | | 29 | | |
| | 15 | 30 | | DefaultExtension = NormalizeExtension(defaultExtension); |
| | | 31 | | DialogFilter = dialogFilter; |
| | 15 | 32 | | AllowedExtensions = [.. (allowedExtensions ?? [defaultExtension]).Select(NormalizeExtension).Distinct(StringComp |
| | 15 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Gets the default extension. |
| | | 37 | | /// </summary> |
| | | 38 | | public string DefaultExtension { get; } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets the dialog filter string. |
| | | 42 | | /// </summary> |
| | | 43 | | public string DialogFilter { get; } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets allowed extensions. |
| | | 47 | | /// </summary> |
| | | 48 | | public IReadOnlyCollection<string> AllowedExtensions { get; } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Determines whether the specified file path has an allowed extension. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name="path">The file path to validate.</param> |
| | | 54 | | /// <returns><see langword="true"/> when the extension is allowed; otherwise <see langword="false"/>.</returns> |
| | | 55 | | public bool IsValidPath(string path) |
| | | 56 | | { |
| | 12 | 57 | | if (string.IsNullOrWhiteSpace(path)) |
| | 3 | 58 | | return false; |
| | | 59 | | |
| | 9 | 60 | | var extension = NormalizeExtension(Path.GetExtension(path)); |
| | 9 | 61 | | return AllowedExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase); |
| | | 62 | | } |
| | | 63 | | |
| | 42 | 64 | | private static string NormalizeExtension(string extension) => string.IsNullOrWhiteSpace(extension) ? string.Empty : |
| | | 65 | | } |
| | | 66 | | |