| | 1 | | // <copyright file="FileLocatorFactory.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 factory to create a file locator. |
| | 15 | | /// </summary> |
| | 16 | | /// <remarks> |
| | 17 | | /// The supported formats are: file system, Zip, Rar, Tar, 7Zip, GZip. |
| | 18 | | /// </remarks> |
| | 19 | | public static class FileLocatorFactory |
| | 20 | | { |
| | 21 | | /// <summary> |
| | 22 | | /// Creates a file locator. |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="root">The root path.</param> |
| | 25 | | /// <returns>A file locator.</returns> |
| | 26 | | public static IFileLocator Create(string? root) |
| | 27 | | { |
| 0 | 28 | | if (string.IsNullOrEmpty(root)) |
| | 29 | | { |
| 0 | 30 | | throw new ArgumentNullException(nameof(root)); |
| | 31 | | } |
| | 32 | |
|
| 0 | 33 | | if (Path.HasExtension(root)) |
| | 34 | | { |
| 0 | 35 | | return new ArchiveFileLocator(root!); |
| | 36 | | } |
| | 37 | |
|
| 0 | 38 | | return new SystemFileLocator(root!); |
| | 39 | | } |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Creates a file locator from a file locator. |
| | 43 | | /// </summary> |
| | 44 | | /// <param name="locator">The source file locator.</param> |
| | 45 | | /// <param name="root">The root path.</param> |
| | 46 | | /// <returns>A file locator.</returns> |
| | 47 | | public static IFileLocator Create(IFileLocator? locator, string? root) |
| | 48 | | { |
| 0 | 49 | | if (locator == null) |
| | 50 | | { |
| 0 | 51 | | throw new ArgumentNullException(nameof(locator)); |
| | 52 | | } |
| | 53 | |
|
| 0 | 54 | | if (root == null) |
| | 55 | | { |
| 0 | 56 | | throw new ArgumentNullException(nameof(root)); |
| | 57 | | } |
| | 58 | |
|
| 0 | 59 | | if (Path.HasExtension(root)) |
| | 60 | | { |
| 0 | 61 | | return new ArchiveFileLocator(locator, root); |
| | 62 | | } |
| | 63 | |
|
| 0 | 64 | | return new SystemFileLocator(locator, root); |
| | 65 | | } |
| | 66 | |
|
| | 67 | | /// <summary> |
| | 68 | | /// Creates a file locator from a stream. |
| | 69 | | /// </summary> |
| | 70 | | /// <param name="root">A stream.</param> |
| | 71 | | /// <returns>A file locator.</returns> |
| | 72 | | public static IFileLocator Create(Stream? root) |
| | 73 | | { |
| 0 | 74 | | if (root == null) |
| | 75 | | { |
| 0 | 76 | | throw new ArgumentNullException(nameof(root)); |
| | 77 | | } |
| | 78 | |
|
| 0 | 79 | | return new ArchiveFileLocator(root); |
| | 80 | | } |
| | 81 | | } |
| | 82 | | } |