| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="RegistryRecentFileRepository.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 System.Runtime.Versioning; |
| | | 12 | | using System.Threading.Tasks; |
| | | 13 | | using MyNet.IO.FileHistory; |
| | | 14 | | using MyNet.IO.Registry; |
| | | 15 | | |
| | | 16 | | namespace MyNet.Platform.Windows.Registry.FileHistory; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Implementation of <see cref="IRecentFileRepository"/> that uses the Windows Registry to store recent file entries. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="registryService">The registry service used to interact with the Windows Registry.</param> |
| | | 22 | | /// <param name="options">The options for configuring the recent files repository.</param> |
| | | 23 | | [SupportedOSPlatform("windows")] |
| | | 24 | | public sealed class RegistryRecentFileRepository(IRegistryService registryService, RecentFilesOptions options) : IRecent |
| | | 25 | | { |
| | 30 | 26 | | private readonly HashSet<string> _supportedExtensions = |
| | 30 | 27 | | [with(options.SupportedExtensions.Select(NormalizeExtension), StringComparer.OrdinalIgnoreCase)]; |
| | | 28 | | |
| | | 29 | | /// <inheritdoc/> |
| | | 30 | | public Task<IReadOnlyList<RecentFile>> GetAllAsync() |
| | | 31 | | { |
| | 9 | 32 | | var result = new List<RecentFile>(); |
| | | 33 | | |
| | 54 | 34 | | foreach (var entries in _supportedExtensions.Select(GetExtensionRegistryPath).Select(registryService.GetAll<Regi |
| | | 35 | | { |
| | 18 | 36 | | result.AddRange(entries.Select(x => MapToRecentFile(x.Item))); |
| | | 37 | | } |
| | | 38 | | |
| | 9 | 39 | | return Task.FromResult<IReadOnlyList<RecentFile>>(result); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <inheritdoc/> |
| | | 43 | | public async Task<RecentFile?> AddAsync(RecentFile file) |
| | | 44 | | { |
| | 39 | 45 | | var extension = GetExtension(file.Path); |
| | | 46 | | |
| | 39 | 47 | | if (extension is null) |
| | 0 | 48 | | return null; |
| | | 49 | | |
| | 39 | 50 | | if (!_supportedExtensions.Contains(extension)) |
| | 3 | 51 | | return null; |
| | | 52 | | |
| | 36 | 53 | | var existing = FindByPath(file.Path); |
| | | 54 | | |
| | 36 | 55 | | if (existing is not null) |
| | | 56 | | { |
| | 3 | 57 | | existing = existing with { LastAccessedAt = DateTimeOffset.UtcNow }; |
| | | 58 | | |
| | 3 | 59 | | return await UpdateAsync(existing).ConfigureAwait(false); |
| | | 60 | | } |
| | | 61 | | |
| | 33 | 62 | | var entry = MapToRegistryRecentFileEntry(file); |
| | | 63 | | |
| | 33 | 64 | | var registryEntry = new RegistryEntry<RegistryRecentFileEntry>(GetExtensionRegistryPath(extension), entry); |
| | | 65 | | |
| | 33 | 66 | | registryService.AddOrUpdate(registryEntry); |
| | | 67 | | |
| | 33 | 68 | | await CleanupAsync(extension).ConfigureAwait(false); |
| | | 69 | | |
| | 33 | 70 | | return file; |
| | 39 | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <inheritdoc/> |
| | | 74 | | public Task<RecentFile?> UpdateAsync(RecentFile file) |
| | | 75 | | { |
| | 9 | 76 | | var existing = FindRegistryEntry(file.Path); |
| | | 77 | | |
| | 9 | 78 | | if (existing is null) |
| | 3 | 79 | | return Task.FromResult<RecentFile?>(null); |
| | | 80 | | |
| | 6 | 81 | | registryService.AddOrUpdate(new RegistryEntry<RegistryRecentFileEntry>(existing.Path, MapToRegistryRecentFileEnt |
| | | 82 | | |
| | 6 | 83 | | return Task.FromResult<RecentFile?>(file); |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <inheritdoc/> |
| | | 87 | | public Task<bool> RemoveAsync(string path) |
| | | 88 | | { |
| | 6 | 89 | | var existing = FindRegistryEntry(path); |
| | | 90 | | |
| | 6 | 91 | | if (existing is null) |
| | 3 | 92 | | return Task.FromResult(false); |
| | | 93 | | |
| | 3 | 94 | | registryService.Remove(existing.Path); |
| | | 95 | | |
| | 3 | 96 | | return Task.FromResult(true); |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | /// <inheritdoc/> |
| | | 100 | | public Task ClearAsync() |
| | | 101 | | { |
| | 18 | 102 | | foreach (var entry in _supportedExtensions.Select(GetExtensionRegistryPath).Select(registryService.GetAll<Regist |
| | | 103 | | { |
| | 6 | 104 | | registryService.Remove(entry.Path); |
| | | 105 | | } |
| | | 106 | | |
| | 3 | 107 | | return Task.CompletedTask; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Maps a <see cref="RegistryRecentFileEntry"/> to a <see cref="RecentFile"/> instance. |
| | | 112 | | /// </summary> |
| | | 113 | | /// <param name="entry">The registry entry to map.</param> |
| | | 114 | | /// <returns>A <see cref="RecentFile"/> instance.</returns> |
| | | 115 | | private static RecentFile MapToRecentFile(RegistryRecentFileEntry entry) |
| | 15 | 116 | | => new() |
| | 15 | 117 | | { |
| | 15 | 118 | | Name = entry.Name, |
| | 15 | 119 | | Path = entry.Path, |
| | 15 | 120 | | IsPinned = entry.IsPinned, |
| | 15 | 121 | | IsRecovered = entry.IsRecovered, |
| | 15 | 122 | | LastAccessedAt = DateTimeOffset.FromFileTime( |
| | 15 | 123 | | entry.LastAccessUtc) |
| | 15 | 124 | | }; |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// Maps a <see cref="RecentFile"/> to a <see cref="RegistryRecentFileEntry"/> instance for storage in the registry. |
| | | 128 | | /// </summary> |
| | | 129 | | /// <param name="file">The recent file to map.</param> |
| | | 130 | | /// <returns>A <see cref="RegistryRecentFileEntry"/> instance.</returns> |
| | | 131 | | private static RegistryRecentFileEntry MapToRegistryRecentFileEntry(RecentFile file) |
| | 39 | 132 | | => new() |
| | 39 | 133 | | { |
| | 39 | 134 | | Name = file.Name, |
| | 39 | 135 | | Path = file.Path, |
| | 39 | 136 | | IsPinned = file.IsPinned, |
| | 39 | 137 | | IsRecovered = file.IsRecovered, |
| | 39 | 138 | | LastAccessUtc = |
| | 39 | 139 | | file.LastAccessedAt?.UtcDateTime.ToFileTimeUtc() ?? 0 |
| | 39 | 140 | | }; |
| | | 141 | | |
| | | 142 | | /// <summary> |
| | | 143 | | /// Normalizes a file extension by trimming whitespace and leading dots, ensuring consistent formatting for registry |
| | | 144 | | /// </summary> |
| | | 145 | | /// <param name="extension">The file extension to normalize.</param> |
| | | 146 | | /// <returns>The normalized file extension.</returns> |
| | 150 | 147 | | private static string NormalizeExtension(string extension) => extension.Trim().TrimStart('.'); |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// Extracts and normalizes the file extension from a given file path. If the path does not have an extension, retur |
| | | 151 | | /// </summary> |
| | | 152 | | /// <param name="path">The file path to extract the extension from.</param> |
| | | 153 | | /// <returns>The normalized file extension, or null if the path does not have an extension.</returns> |
| | | 154 | | private static string? GetExtension(string path) |
| | | 155 | | { |
| | 90 | 156 | | var extension = Path.GetExtension(path); |
| | | 157 | | |
| | 90 | 158 | | return string.IsNullOrWhiteSpace(extension) |
| | 90 | 159 | | ? null |
| | 90 | 160 | | : NormalizeExtension(extension); |
| | | 161 | | } |
| | | 162 | | |
| | | 163 | | /// <summary> |
| | | 164 | | /// Cleans up old entries for a given file extension, ensuring that the number of recent file entries does not excee |
| | | 165 | | /// </summary> |
| | | 166 | | /// <param name="extension">The file extension for which to clean up old entries.</param> |
| | | 167 | | /// <returns>A task representing the asynchronous cleanup operation.</returns> |
| | | 168 | | private Task CleanupAsync(string extension) |
| | | 169 | | { |
| | 33 | 170 | | if (options.MaxEntries <= 0) |
| | 0 | 171 | | return Task.CompletedTask; |
| | | 172 | | |
| | 33 | 173 | | var entries = registryService |
| | 33 | 174 | | .GetAll<RegistryRecentFileEntry>(GetExtensionRegistryPath(extension)) |
| | 33 | 175 | | .ToList(); |
| | | 176 | | |
| | 33 | 177 | | var removable = entries |
| | 33 | 178 | | .Where(x => x.Item is { IsPinned: false, IsRecovered: false }) |
| | 33 | 179 | | .OrderByDescending(x => x.Item.LastAccessUtc) |
| | 33 | 180 | | .Skip(options.MaxEntries) |
| | 33 | 181 | | .ToList(); |
| | | 182 | | |
| | 72 | 183 | | foreach (var item in removable) |
| | 3 | 184 | | registryService.Remove(item.Path); |
| | | 185 | | |
| | 33 | 186 | | return Task.CompletedTask; |
| | | 187 | | } |
| | | 188 | | |
| | | 189 | | /// <summary> |
| | | 190 | | /// Constructs the registry path for a given file extension by combining the base path from options with the normali |
| | | 191 | | /// </summary> |
| | | 192 | | /// <param name="extension">The file extension for which to construct the registry path.</param> |
| | | 193 | | /// <returns>The constructed registry path for the specified file extension.</returns> |
| | 141 | 194 | | private RegistryPath GetExtensionRegistryPath(string extension) => RegistryPath.Combine(options.BasePath, extension) |
| | | 195 | | |
| | | 196 | | /// <summary> |
| | | 197 | | /// Finds a registry entry for a given file path by searching through the registry entries for the corresponding fil |
| | | 198 | | /// </summary> |
| | | 199 | | /// <param name="path">The file path for which to find the registry entry.</param> |
| | | 200 | | /// <returns>The registry entry for the specified file path, or null if no match is found.</returns> |
| | | 201 | | private RegistryEntry<RegistryRecentFileEntry>? FindRegistryEntry(string path) |
| | | 202 | | { |
| | 51 | 203 | | var extension = GetExtension(path); |
| | | 204 | | |
| | 51 | 205 | | return extension is null |
| | 51 | 206 | | ? null |
| | 51 | 207 | | : registryService |
| | 51 | 208 | | .GetAll<RegistryRecentFileEntry>(GetExtensionRegistryPath(extension)) |
| | 51 | 209 | | .FirstOrDefault(x => |
| | 51 | 210 | | string.Equals( |
| | 51 | 211 | | x.Item.Path, |
| | 51 | 212 | | path, |
| | 51 | 213 | | StringComparison.OrdinalIgnoreCase)); |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | /// <summary> |
| | | 217 | | /// Finds a recent file entry by its file path by first locating the corresponding registry entry and then mapping i |
| | | 218 | | /// </summary> |
| | | 219 | | /// <param name="path">The file path for which to find the recent file entry.</param> |
| | | 220 | | /// <returns>The recent file entry for the specified path, or null if no match is found.</returns> |
| | 36 | 221 | | private RecentFile? FindByPath(string path) => FindRegistryEntry(path)?.Item is { } item ? MapToRecentFile(item) : n |
| | | 222 | | } |
| | | 223 | | |