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