| | | 1 | | // <copyright file="SystemFileLocator.cs" company="Jérémy Ansel"> |
| | | 2 | | // Copyright (c) 2015, 2019 Jérémy Ansel |
| | | 3 | | // </copyright> |
| | | 4 | | // <license> |
| | | 5 | | // Licensed under the MIT license. See LICENSE.txt |
| | | 6 | | // </license> |
| | | 7 | | |
| | | 8 | | namespace JeremyAnsel.IO.Locator |
| | | 9 | | { |
| | | 10 | | using System; |
| | | 11 | | using System.Collections.Generic; |
| | | 12 | | using System.IO; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// A file system file locator. |
| | | 16 | | /// </summary> |
| | | 17 | | internal sealed class SystemFileLocator : IFileLocator |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// A file locator. |
| | | 21 | | /// </summary> |
| | | 22 | | private readonly IFileLocator? locator; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// The root path. |
| | | 26 | | /// </summary> |
| | | 27 | | private readonly string rootPath; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Initializes a new instance of the <see cref="SystemFileLocator"/> class. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="root">The root path.</param> |
| | 0 | 33 | | public SystemFileLocator(string root) |
| | | 34 | | { |
| | 0 | 35 | | if (!Directory.Exists(root)) |
| | | 36 | | { |
| | 0 | 37 | | throw new DirectoryNotFoundException(); |
| | | 38 | | } |
| | | 39 | | |
| | 0 | 40 | | this.rootPath = root; |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Initializes a new instance of the <see cref="SystemFileLocator"/> class. |
| | | 45 | | /// </summary> |
| | | 46 | | /// <param name="locator">A file locator.</param> |
| | | 47 | | /// <param name="root">The root path.</param> |
| | 0 | 48 | | public SystemFileLocator(IFileLocator? locator, string root) |
| | | 49 | | { |
| | 0 | 50 | | if (locator == null && !Directory.Exists(root)) |
| | | 51 | | { |
| | 0 | 52 | | throw new DirectoryNotFoundException(); |
| | | 53 | | } |
| | | 54 | | |
| | 0 | 55 | | this.locator = locator; |
| | 0 | 56 | | this.rootPath = root; |
| | 0 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Immediately releases the unmanaged resources used by the <see cref="ArchiveFileLocator"/> object. |
| | | 61 | | /// </summary> |
| | | 62 | | public void Dispose() |
| | | 63 | | { |
| | 0 | 64 | | GC.SuppressFinalize(this); |
| | 0 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Indicates whether the specified path exists. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <param name="path">A path.</param> |
| | | 71 | | /// <returns>A boolean.</returns> |
| | | 72 | | public bool Exists(string path) |
| | | 73 | | { |
| | 0 | 74 | | string fullPath = Path.Combine(this.rootPath, path); |
| | | 75 | | |
| | 0 | 76 | | if (this.locator != null) |
| | | 77 | | { |
| | 0 | 78 | | return this.locator.Exists(fullPath); |
| | | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | return File.Exists(fullPath); |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Open a file. |
| | | 86 | | /// </summary> |
| | | 87 | | /// <param name="path">A path.</param> |
| | | 88 | | /// <returns>A stream.</returns> |
| | | 89 | | public Stream Open(string path) |
| | | 90 | | { |
| | 0 | 91 | | string fullPath = Path.Combine(this.rootPath, path); |
| | | 92 | | |
| | 0 | 93 | | if (this.locator != null) |
| | | 94 | | { |
| | 0 | 95 | | return this.locator.Open(fullPath); |
| | | 96 | | } |
| | | 97 | | |
| | 0 | 98 | | return File.OpenRead(fullPath); |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Enumerate the files. |
| | | 103 | | /// </summary> |
| | | 104 | | /// <returns>An enumeration.</returns> |
| | | 105 | | public IEnumerable<string> EnumerateFiles() |
| | | 106 | | { |
| | 0 | 107 | | return this.EnumerateFiles(string.Empty); |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Enumerate the files. |
| | | 112 | | /// </summary> |
| | | 113 | | /// <param name="root">The root path.</param> |
| | | 114 | | /// <returns>An enumeration.</returns> |
| | | 115 | | public IEnumerable<string> EnumerateFiles(string root) |
| | | 116 | | { |
| | 0 | 117 | | string fullPath = Utilities.PathNormalize(Path.Combine(this.rootPath, root)); |
| | | 118 | | |
| | | 119 | | IEnumerable<string> results; |
| | | 120 | | |
| | 0 | 121 | | if (this.locator != null) |
| | | 122 | | { |
| | 0 | 123 | | results = this.locator.EnumerateFiles(fullPath); |
| | | 124 | | } |
| | | 125 | | else |
| | | 126 | | { |
| | 0 | 127 | | results = Directory.EnumerateFiles(fullPath, "*.*", SearchOption.AllDirectories); |
| | | 128 | | } |
| | | 129 | | |
| | 0 | 130 | | foreach (var file in results) |
| | | 131 | | { |
| | 0 | 132 | | yield return Utilities.PathNormalize(Utilities.PathNormalize(file)[fullPath.Length..]); |
| | | 133 | | } |
| | 0 | 134 | | } |
| | | 135 | | } |
| | | 136 | | } |