| | 1 | | // <copyright file="SystemWritableFileLocator.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.IO; |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// A writable file system file locator. |
| | 15 | | /// </summary> |
| | 16 | | internal sealed class SystemWritableFileLocator : IWritableFileLocator |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// The root path. |
| | 20 | | /// </summary> |
| | 21 | | private readonly string rootPath; |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Initializes a new instance of the <see cref="SystemWritableFileLocator"/> class. |
| | 25 | | /// </summary> |
| | 26 | | /// <param name="root">The root path.</param> |
| 0 | 27 | | public SystemWritableFileLocator(string root) |
| | 28 | | { |
| 0 | 29 | | if (!Directory.Exists(root)) |
| | 30 | | { |
| 0 | 31 | | throw new DirectoryNotFoundException(); |
| | 32 | | } |
| | 33 | |
|
| 0 | 34 | | this.rootPath = root; |
| 0 | 35 | | } |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Immediately releases the unmanaged resources used by the <see cref="ArchiveWritableFileLocator"/> object. |
| | 39 | | /// </summary> |
| | 40 | | public void Dispose() |
| | 41 | | { |
| 0 | 42 | | GC.SuppressFinalize(this); |
| 0 | 43 | | } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Create a file. |
| | 47 | | /// </summary> |
| | 48 | | /// <param name="path">A path.</param> |
| | 49 | | public void Create(string path) |
| | 50 | | { |
| 0 | 51 | | if (string.IsNullOrEmpty(path)) |
| | 52 | | { |
| 0 | 53 | | throw new ArgumentNullException(nameof(path)); |
| | 54 | | } |
| | 55 | |
|
| 0 | 56 | | string fullPath = Utilities.PathNormalize(Path.Combine(this.rootPath, path)); |
| | 57 | |
|
| 0 | 58 | | if (Directory.Exists(fullPath) || File.Exists(fullPath)) |
| | 59 | | { |
| 0 | 60 | | throw new ArgumentOutOfRangeException(nameof(path)); |
| | 61 | | } |
| | 62 | |
|
| 0 | 63 | | string dirPath = Path.GetDirectoryName(fullPath)!; |
| | 64 | |
|
| 0 | 65 | | if (!Directory.Exists(dirPath)) |
| | 66 | | { |
| 0 | 67 | | Directory.CreateDirectory(dirPath); |
| | 68 | | } |
| | 69 | |
|
| 0 | 70 | | using (File.Create(fullPath)) |
| | 71 | | { |
| 0 | 72 | | } |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | /// <summary> |
| | 76 | | /// Write a file. |
| | 77 | | /// </summary> |
| | 78 | | /// <param name="path">A path.</param> |
| | 79 | | /// <param name="data">The data.</param> |
| | 80 | | public void Write(string path, Stream? data) |
| | 81 | | { |
| 0 | 82 | | if (string.IsNullOrEmpty(path)) |
| | 83 | | { |
| 0 | 84 | | throw new ArgumentNullException(nameof(path)); |
| | 85 | | } |
| | 86 | |
|
| 0 | 87 | | if (data == null) |
| | 88 | | { |
| 0 | 89 | | throw new ArgumentNullException(nameof(data)); |
| | 90 | | } |
| | 91 | |
|
| 0 | 92 | | this.Create(path); |
| | 93 | |
|
| 0 | 94 | | using (var file = File.OpenWrite(path)) |
| | 95 | | { |
| 0 | 96 | | data.CopyTo(file); |
| 0 | 97 | | } |
| 0 | 98 | | } |
| | 99 | |
|
| | 100 | | /// <summary> |
| | 101 | | /// Write the files from a file locator. |
| | 102 | | /// </summary> |
| | 103 | | /// <param name="locator">A file locator.</param> |
| | 104 | | public void WriteAll(IFileLocator? locator) |
| | 105 | | { |
| 0 | 106 | | this.WriteAll(locator, string.Empty); |
| 0 | 107 | | } |
| | 108 | |
|
| | 109 | | /// <summary> |
| | 110 | | /// Write the files from a file locator. |
| | 111 | | /// </summary> |
| | 112 | | /// <param name="locator">A file locator.</param> |
| | 113 | | /// <param name="root">The root path.</param> |
| | 114 | | public void WriteAll(IFileLocator? locator, string root) |
| | 115 | | { |
| 0 | 116 | | if (locator == null) |
| | 117 | | { |
| 0 | 118 | | throw new ArgumentNullException(nameof(locator)); |
| | 119 | | } |
| | 120 | |
|
| 0 | 121 | | foreach (var file in locator.EnumerateFiles(root)) |
| | 122 | | { |
| 0 | 123 | | using var stream = locator.Open(file); |
| 0 | 124 | | this.Write(file, stream); |
| | 125 | | } |
| 0 | 126 | | } |
| | 127 | | } |
| | 128 | | } |