| | 1 | | // <copyright file="DiscFileLocatorFactory.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.DiscLocator |
| | 9 | | { |
| | 10 | | using System; |
| | 11 | | using System.IO; |
| | 12 | | using IO.Locator; |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// A factory to create a disc file locator. |
| | 16 | | /// </summary> |
| | 17 | | /// <remarks> |
| | 18 | | /// The supported formats are: Iso, Udf. |
| | 19 | | /// </remarks> |
| | 20 | | public static class DiscFileLocatorFactory |
| | 21 | | { |
| | 22 | | /// <summary> |
| | 23 | | /// Creates a disc file locator from a file. |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="root">The root path.</param> |
| | 26 | | /// <returns>A file locator.</returns> |
| | 27 | | public static IFileLocator Create(string root) |
| | 28 | | { |
| 0 | 29 | | if (string.IsNullOrEmpty(root)) |
| | 30 | | { |
| 0 | 31 | | throw new ArgumentNullException(nameof(root)); |
| | 32 | | } |
| | 33 | |
|
| 0 | 34 | | if (DiscFileLocatorFactory.IsDisc(root, IsoFileLocator.IsIsoFile)) |
| | 35 | | { |
| 0 | 36 | | return new IsoFileLocator(root); |
| | 37 | | } |
| | 38 | |
|
| 0 | 39 | | if (DiscFileLocatorFactory.IsDisc(root, UdfFileLocator.IsUdfFile)) |
| | 40 | | { |
| 0 | 41 | | return new UdfFileLocator(root); |
| | 42 | | } |
| | 43 | |
|
| 0 | 44 | | throw new NotSupportedException(); |
| | 45 | | } |
| | 46 | |
|
| | 47 | | /// <summary> |
| | 48 | | /// Creates a disc file locator from a locator. |
| | 49 | | /// </summary> |
| | 50 | | /// <param name="locator">The locator.</param> |
| | 51 | | /// <param name="root">The root path.</param> |
| | 52 | | /// <returns>A file locator.</returns> |
| | 53 | | public static IFileLocator Create(IFileLocator locator, string root) |
| | 54 | | { |
| 0 | 55 | | if (locator == null) |
| | 56 | | { |
| 0 | 57 | | throw new ArgumentNullException(nameof(locator)); |
| | 58 | | } |
| | 59 | |
|
| 0 | 60 | | if (string.IsNullOrEmpty(root)) |
| | 61 | | { |
| 0 | 62 | | throw new ArgumentNullException(nameof(root)); |
| | 63 | | } |
| | 64 | |
|
| 0 | 65 | | if (DiscFileLocatorFactory.IsDisc(locator, root, IsoFileLocator.IsIsoFile)) |
| | 66 | | { |
| 0 | 67 | | return new IsoFileLocator(locator, root); |
| | 68 | | } |
| | 69 | |
|
| 0 | 70 | | if (DiscFileLocatorFactory.IsDisc(locator, root, UdfFileLocator.IsUdfFile)) |
| | 71 | | { |
| 0 | 72 | | return new UdfFileLocator(locator, root); |
| | 73 | | } |
| | 74 | |
|
| 0 | 75 | | throw new NotSupportedException(); |
| | 76 | | } |
| | 77 | |
|
| | 78 | | /// <summary> |
| | 79 | | /// Creates a disc file locator from a stream. |
| | 80 | | /// </summary> |
| | 81 | | /// <param name="root">The stream.</param> |
| | 82 | | /// <returns>A file locator.</returns> |
| | 83 | | public static IFileLocator Create(Stream root) |
| | 84 | | { |
| 2 | 85 | | if (root == null) |
| | 86 | | { |
| 0 | 87 | | throw new ArgumentNullException(nameof(root)); |
| | 88 | | } |
| | 89 | |
|
| 2 | 90 | | if (IsoFileLocator.IsIsoFile(root)) |
| | 91 | | { |
| 0 | 92 | | root.Seek(0, SeekOrigin.Begin); |
| 0 | 93 | | return new IsoFileLocator(root); |
| | 94 | | } |
| | 95 | |
|
| 2 | 96 | | if (UdfFileLocator.IsUdfFile(root)) |
| | 97 | | { |
| 0 | 98 | | root.Seek(0, SeekOrigin.Begin); |
| 0 | 99 | | return new UdfFileLocator(root); |
| | 100 | | } |
| | 101 | |
|
| 2 | 102 | | throw new NotSupportedException(); |
| | 103 | | } |
| | 104 | |
|
| | 105 | | /// <summary> |
| | 106 | | /// Detects if the specified root is a disc. |
| | 107 | | /// </summary> |
| | 108 | | /// <param name="root">The root.</param> |
| | 109 | | /// <param name="detect">The detection function.</param> |
| | 110 | | /// <returns>A boolean.</returns> |
| | 111 | | private static bool IsDisc(string root, Func<Stream, bool> detect) |
| | 112 | | { |
| 0 | 113 | | using (var file = File.OpenRead(root)) |
| | 114 | | { |
| 0 | 115 | | return detect(file); |
| | 116 | | } |
| 0 | 117 | | } |
| | 118 | |
|
| | 119 | | /// <summary> |
| | 120 | | /// Detects if the specified root from a locator is a disc. |
| | 121 | | /// </summary> |
| | 122 | | /// <param name="locator">The locator.</param> |
| | 123 | | /// <param name="root">The root.</param> |
| | 124 | | /// <param name="detect">The detection function.</param> |
| | 125 | | /// <returns>A boolean.</returns> |
| | 126 | | private static bool IsDisc(IFileLocator locator, string root, Func<Stream, bool> detect) |
| | 127 | | { |
| 0 | 128 | | using (var file = locator.Open(root)) |
| | 129 | | { |
| 0 | 130 | | return detect(file); |
| | 131 | | } |
| 0 | 132 | | } |
| | 133 | | } |
| | 134 | | } |
| | 135 | |
|