| | | 1 | | // <copyright file="WritableFileLocatorFactory.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.Diagnostics.CodeAnalysis; |
| | | 12 | | using System.IO; |
| | | 13 | | using SharpCompress.Common; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// A factory to create a writable file locator. |
| | | 17 | | /// <para> |
| | | 18 | | /// The supported formats are: file system, Zip, Tar, GZip. |
| | | 19 | | /// </para> |
| | | 20 | | /// </summary> |
| | | 21 | | public static class WritableFileLocatorFactory |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Creates a writable file locator. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="root">The root path.</param> |
| | | 27 | | /// <returns>A writable file locator.</returns> |
| | | 28 | | [SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase", Justification = "Reviewed")] |
| | | 29 | | public static IWritableFileLocator Create(string? root) |
| | | 30 | | { |
| | 0 | 31 | | if (string.IsNullOrEmpty(root)) |
| | | 32 | | { |
| | 0 | 33 | | throw new ArgumentNullException(nameof(root)); |
| | | 34 | | } |
| | | 35 | | |
| | 0 | 36 | | string ext = Path.GetExtension(root); |
| | | 37 | | |
| | 0 | 38 | | if (!string.IsNullOrEmpty(ext)) |
| | | 39 | | { |
| | 0 | 40 | | return ext.ToLowerInvariant() switch |
| | 0 | 41 | | { |
| | 0 | 42 | | ".zip" => new ArchiveWritableFileLocator(root!, ArchiveType.Zip, CompressionType.Deflate), |
| | 0 | 43 | | ".gz" => new ArchiveWritableFileLocator(root!, ArchiveType.GZip, CompressionType.GZip), |
| | 0 | 44 | | _ => throw new NotSupportedException(), |
| | 0 | 45 | | }; |
| | | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | return new SystemWritableFileLocator(root!); |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Creates a writable file locator for an archive. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <param name="root">The root path.</param> |
| | | 55 | | /// <param name="archiveType">The archive type.</param> |
| | | 56 | | /// <param name="compressionType">The compression type.</param> |
| | | 57 | | /// <returns>A writable file locator.</returns> |
| | | 58 | | public static IWritableFileLocator CreateArchive(string? root, ArchiveType archiveType, CompressionType compress |
| | | 59 | | { |
| | 0 | 60 | | if (string.IsNullOrEmpty(root)) |
| | | 61 | | { |
| | 0 | 62 | | throw new ArgumentNullException(nameof(root)); |
| | | 63 | | } |
| | | 64 | | |
| | 0 | 65 | | return new ArchiveWritableFileLocator(root!, archiveType, compressionType); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Creates a writable file locator for an archive. |
| | | 70 | | /// </summary> |
| | | 71 | | /// <param name="root">The root path.</param> |
| | | 72 | | /// <param name="archiveType">The archive type.</param> |
| | | 73 | | /// <returns>A writable file locator.</returns> |
| | | 74 | | public static IWritableFileLocator CreateArchive(string? root, ArchiveType archiveType) |
| | | 75 | | { |
| | 0 | 76 | | if (string.IsNullOrEmpty(root)) |
| | | 77 | | { |
| | 0 | 78 | | throw new ArgumentNullException(nameof(root)); |
| | | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | return new ArchiveWritableFileLocator(root!, archiveType, CompressionType.Deflate); |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Creates a writable file locator for an archive from a stream. |
| | | 86 | | /// </summary> |
| | | 87 | | /// <param name="root">A stream</param> |
| | | 88 | | /// <param name="archiveType">The archive type.</param> |
| | | 89 | | /// <param name="compressionType">The compression type.</param> |
| | | 90 | | /// <returns>A writable file locator.</returns> |
| | | 91 | | public static IWritableFileLocator CreateArchive(Stream? root, ArchiveType archiveType, CompressionType compress |
| | | 92 | | { |
| | 0 | 93 | | if (root == null) |
| | | 94 | | { |
| | 0 | 95 | | throw new ArgumentNullException(nameof(root)); |
| | | 96 | | } |
| | | 97 | | |
| | 0 | 98 | | return new ArchiveWritableFileLocator(root, archiveType, compressionType); |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Creates a writable file locator for an archive from a stream. |
| | | 103 | | /// </summary> |
| | | 104 | | /// <param name="root">A stream.</param> |
| | | 105 | | /// <param name="archiveType">The archive type.</param> |
| | | 106 | | /// <returns>A writable file locator.</returns> |
| | | 107 | | public static IWritableFileLocator CreateArchive(Stream? root, ArchiveType archiveType) |
| | | 108 | | { |
| | 0 | 109 | | if (root == null) |
| | | 110 | | { |
| | 0 | 111 | | throw new ArgumentNullException(nameof(root)); |
| | | 112 | | } |
| | | 113 | | |
| | 0 | 114 | | return new ArchiveWritableFileLocator(root, archiveType, CompressionType.Deflate); |
| | | 115 | | } |
| | | 116 | | } |
| | | 117 | | } |