| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="FileExtensionExtensions.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 | | using MyNet.IO.FileExtensions; |
| | | 12 | | using MyNet.Primitives; |
| | | 13 | | |
| | | 14 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 15 | | namespace MyNet.IO; |
| | | 16 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 17 | | |
| | | 18 | | public static class FileExtensionExtensions |
| | | 19 | | { |
| | | 20 | | extension(FileExtensionGroup group) |
| | | 21 | | { |
| | | 22 | | /// <summary> |
| | | 23 | | /// Converts the <see cref="FileExtensionGroup"/> to a <see cref="FileFilter"/> that can be used in file dialogs |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="translate">A function to translate the group's key.</param> |
| | | 26 | | /// <returns>A <see cref="FileFilter"/> representing the group's extensions.</returns> |
| | | 27 | | public FileFilter ToFilter(Func<string, string?>? translate = null) |
| | | 28 | | { |
| | 9 | 29 | | var pattern = string.Join(";", group.Extensions.Select(e => $"*{e.Value}")); |
| | | 30 | | |
| | 9 | 31 | | var title = translate?.Invoke(group.Key) ?? group.Key.OrEmpty(); |
| | | 32 | | |
| | 9 | 33 | | if (!string.IsNullOrWhiteSpace(pattern) && pattern != "*.*") |
| | 9 | 34 | | title = $"{title} ({pattern})"; |
| | | 35 | | |
| | 9 | 36 | | return new(title, pattern); |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Determines whether the specified file path has a valid extension based on the extensions defined in the <see |
| | | 41 | | /// </summary> |
| | | 42 | | /// <param name="filePath">The file path to check.</param> |
| | | 43 | | /// <returns>True if the file has a valid extension; otherwise, false.</returns> |
| | | 44 | | public bool IsValidFile(string filePath) |
| | | 45 | | { |
| | 9 | 46 | | if (string.IsNullOrWhiteSpace(filePath)) |
| | 3 | 47 | | return false; |
| | | 48 | | |
| | 6 | 49 | | var ext = Path.GetExtension(filePath); |
| | 6 | 50 | | return group.Extensions.Any(e => |
| | 6 | 51 | | string.Equals(e.Value, ext, StringComparison.OrdinalIgnoreCase)); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Concatenates the extensions of the current <see cref="FileExtensionGroup"/> with those of another <see cref= |
| | | 56 | | /// </summary> |
| | | 57 | | /// <param name="second">The second <see cref="FileExtensionGroup"/> to concatenate with the current group.</par |
| | | 58 | | /// <param name="key">The key for the new <see cref="FileExtensionGroup"/>. If not provided, the key of the curr |
| | | 59 | | /// <returns>A new <see cref="FileExtensionGroup"/> that combines the extensions from both groups.</returns> |
| | | 60 | | public FileExtensionGroup Concat( |
| | | 61 | | FileExtensionGroup second, |
| | | 62 | | string? key = null) |
| | 3 | 63 | | => new(key ?? group.Key, group.Extensions.Concat(second.Extensions)); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | extension(IEnumerable<FileExtensionGroup> groups) |
| | | 67 | | { |
| | | 68 | | /// <summary> |
| | | 69 | | /// Converts a collection of <see cref="FileExtensionGroup"/> items to a filter string that can be used in file |
| | | 70 | | /// </summary> |
| | | 71 | | /// <param name="translate">A function to translate the group's key.</param> |
| | | 72 | | /// <returns>A filter string that can be used in file dialogs.</returns> |
| | | 73 | | public string ToFilterString(Func<string, string?>? translate = null) |
| | 3 | 74 | | => string.Join("|", |
| | 3 | 75 | | groups.SelectMany(g => |
| | 3 | 76 | | { |
| | 3 | 77 | | var (title, pattern) = g.ToFilter(translate); |
| | 3 | 78 | | return new[] { title, pattern }; |
| | 3 | 79 | | })); |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | extension(IEnumerable<string> files) |
| | | 83 | | { |
| | | 84 | | /// <summary> |
| | | 85 | | /// Filters a collection of file paths based on the extensions defined in a <see cref="FileExtensionGroup"/>. Th |
| | | 86 | | /// </summary> |
| | | 87 | | /// <param name="group">The file extension group to filter by.</param> |
| | | 88 | | /// <returns>A collection of file paths that have valid extensions according to the specified group.</returns> |
| | | 89 | | public IEnumerable<string> FilterByExtensions(FileExtensionGroup group) |
| | | 90 | | { |
| | 3 | 91 | | var allowed = group.Extensions |
| | 3 | 92 | | .Select(x => x.Value) |
| | 3 | 93 | | .ToHashSet(StringComparer.OrdinalIgnoreCase); |
| | | 94 | | |
| | 3 | 95 | | return files.Where(file => |
| | 3 | 96 | | { |
| | 3 | 97 | | var ext = Path.GetExtension(file); |
| | 3 | 98 | | return allowed.Contains(ext); |
| | 3 | 99 | | }); |
| | | 100 | | } |
| | | 101 | | } |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Represents a file filter that can be used in file dialogs. The <see cref="FileFilter"/> record contains a title and |
| | | 106 | | /// </summary> |
| | | 107 | | /// <param name="Title">The title of the file filter.</param> |
| | | 108 | | /// <param name="Pattern">The pattern of the file filter.</param> |
| | | 109 | | public sealed record FileFilter(string Title, string Pattern); |
| | | 110 | | |