| | | 1 | | using Anichron.Core; |
| | | 2 | | using System.IO.Abstractions; |
| | | 3 | | |
| | | 4 | | namespace Anichron.Infrastructure.Storage; |
| | | 5 | | |
| | | 6 | | public interface IProxyStore |
| | | 7 | | { |
| | | 8 | | bool RootExists(); |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Opens a proxy for reading by its stored path, relative to the proxy root. Returns |
| | | 12 | | /// <see langword="null"/> when the file is missing or the path resolves outside the root. |
| | | 13 | | /// </summary> |
| | | 14 | | Stream? OpenRead(string proxyPath); |
| | | 15 | | } |
| | | 16 | | |
| | | 17 | | public sealed class ProxyStore : IProxyStore |
| | | 18 | | { |
| | | 19 | | private readonly IFileSystem fileSystem; |
| | | 20 | | |
| | | 21 | | public ProxyStore() |
| | 0 | 22 | | : this(new FileSystem()) |
| | 0 | 23 | | { |
| | 0 | 24 | | } |
| | | 25 | | |
| | 30 | 26 | | internal ProxyStore(IFileSystem fileSystem) => this.fileSystem = fileSystem; |
| | | 27 | | |
| | 2 | 28 | | public bool RootExists() => fileSystem.Directory.Exists(StoragePath.ProxiesRoot); |
| | | 29 | | |
| | | 30 | | public Stream? OpenRead(string proxyPath) |
| | 13 | 31 | | { |
| | 13 | 32 | | if (StoragePath.ResolveProxy(proxyPath) is not { } fullPath) |
| | 7 | 33 | | return null; |
| | | 34 | | |
| | | 35 | | try |
| | 6 | 36 | | { |
| | 6 | 37 | | return fileSystem.FileStream.New(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read); |
| | | 38 | | } |
| | | 39 | | // Stryker disable once Block |
| | 3 | 40 | | catch (Exception ex) when (ex is FileNotFoundException or DirectoryNotFoundException) |
| | 2 | 41 | | { |
| | 2 | 42 | | return null; |
| | | 43 | | } |
| | 12 | 44 | | } |
| | | 45 | | } |