| | | 1 | | using System; |
| | | 2 | | using System.Diagnostics.CodeAnalysis; |
| | | 3 | | using System.IO; |
| | | 4 | | using MediaBrowser.Common.Providers; |
| | | 5 | | |
| | | 6 | | namespace Emby.Server.Implementations.Library |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Class providing extension methods for working with paths. |
| | | 10 | | /// </summary> |
| | | 11 | | public static class PathExtensions |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Gets the attribute value. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="str">The STR.</param> |
| | | 17 | | /// <param name="attribute">The attrib.</param> |
| | | 18 | | /// <returns>System.String.</returns> |
| | | 19 | | /// <exception cref="ArgumentException"><paramref name="str" /> or <paramref name="attribute" /> is empty.</exce |
| | | 20 | | public static string? GetAttributeValue(this ReadOnlySpan<char> str, ReadOnlySpan<char> attribute) |
| | | 21 | | { |
| | 42 | 22 | | if (str.Length == 0) |
| | | 23 | | { |
| | 2 | 24 | | throw new ArgumentException("String can't be empty.", nameof(str)); |
| | | 25 | | } |
| | | 26 | | |
| | 40 | 27 | | if (attribute.Length == 0) |
| | | 28 | | { |
| | 1 | 29 | | throw new ArgumentException("String can't be empty.", nameof(attribute)); |
| | | 30 | | } |
| | | 31 | | |
| | 39 | 32 | | var attributeIndex = str.IndexOf(attribute, StringComparison.OrdinalIgnoreCase); |
| | | 33 | | |
| | | 34 | | // Must be at least 3 characters after the attribute =, ], any character, |
| | | 35 | | // then we offset it by 1, because we want the index and not length. |
| | 39 | 36 | | var maxIndex = str.Length - attribute.Length - 2; |
| | 52 | 37 | | while (attributeIndex > -1 && attributeIndex < maxIndex) |
| | | 38 | | { |
| | 39 | 39 | | var attributeEnd = attributeIndex + attribute.Length; |
| | 39 | 40 | | if (attributeIndex > 0) |
| | | 41 | | { |
| | 36 | 42 | | var attributeOpener = str[attributeIndex - 1]; |
| | 36 | 43 | | var attributeCloser = attributeOpener switch |
| | 36 | 44 | | { |
| | 21 | 45 | | '[' => ']', |
| | 7 | 46 | | '(' => ')', |
| | 8 | 47 | | '{' => '}', |
| | 0 | 48 | | _ => '\0' |
| | 36 | 49 | | }; |
| | 36 | 50 | | if (attributeCloser != '\0' && (str[attributeEnd] == '=' || str[attributeEnd] == '-')) |
| | | 51 | | { |
| | 33 | 52 | | var closingIndex = str[attributeEnd..].IndexOf(attributeCloser); |
| | | 53 | | |
| | | 54 | | // Must be at least 1 character before the closing bracket. |
| | 33 | 55 | | if (closingIndex > 1) |
| | | 56 | | { |
| | 26 | 57 | | return str[(attributeEnd + 1)..(attributeEnd + closingIndex)].Trim().ToString(); |
| | | 58 | | } |
| | | 59 | | } |
| | | 60 | | } |
| | | 61 | | |
| | 13 | 62 | | str = str[attributeEnd..]; |
| | 13 | 63 | | attributeIndex = str.IndexOf(attribute, StringComparison.OrdinalIgnoreCase); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | // for imdbid we also accept pattern matching |
| | 13 | 67 | | if (attribute.Equals("imdbid", StringComparison.OrdinalIgnoreCase)) |
| | | 68 | | { |
| | 2 | 69 | | var match = ProviderIdParsers.TryFindImdbId(str, out var imdbId); |
| | 2 | 70 | | return match ? imdbId.ToString() : null; |
| | | 71 | | } |
| | | 72 | | |
| | 11 | 73 | | return null; |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Replaces a sub path with another sub path and normalizes the final path. |
| | | 78 | | /// </summary> |
| | | 79 | | /// <param name="path">The original path.</param> |
| | | 80 | | /// <param name="subPath">The original sub path.</param> |
| | | 81 | | /// <param name="newSubPath">The new sub path.</param> |
| | | 82 | | /// <param name="newPath">The result of the sub path replacement.</param> |
| | | 83 | | /// <returns>The path after replacing the sub path.</returns> |
| | | 84 | | /// <exception cref="ArgumentNullException"><paramref name="path" />, <paramref name="newSubPath" /> or <paramre |
| | | 85 | | public static bool TryReplaceSubPath( |
| | | 86 | | [NotNullWhen(true)] this string? path, |
| | | 87 | | [NotNullWhen(true)] string? subPath, |
| | | 88 | | [NotNullWhen(true)] string? newSubPath, |
| | | 89 | | [NotNullWhen(true)] out string? newPath) |
| | | 90 | | { |
| | 17 | 91 | | newPath = null; |
| | | 92 | | |
| | 17 | 93 | | if (string.IsNullOrEmpty(path) |
| | 17 | 94 | | || string.IsNullOrEmpty(subPath) |
| | 17 | 95 | | || string.IsNullOrEmpty(newSubPath) |
| | 17 | 96 | | || subPath.Length > path.Length) |
| | | 97 | | { |
| | 8 | 98 | | return false; |
| | | 99 | | } |
| | | 100 | | |
| | 9 | 101 | | subPath = subPath.NormalizePath(out var newDirectorySeparatorChar); |
| | 9 | 102 | | path = path.NormalizePath(newDirectorySeparatorChar); |
| | | 103 | | |
| | | 104 | | // We have to ensure that the sub path ends with a directory separator otherwise we'll get weird results |
| | | 105 | | // when the sub path matches a similar but in-complete subpath |
| | 9 | 106 | | var oldSubPathEndsWithSeparator = subPath[^1] == newDirectorySeparatorChar; |
| | 9 | 107 | | if (!path.StartsWith(subPath, StringComparison.OrdinalIgnoreCase)) |
| | | 108 | | { |
| | 1 | 109 | | return false; |
| | | 110 | | } |
| | | 111 | | |
| | 8 | 112 | | if (path.Length > subPath.Length |
| | 8 | 113 | | && !oldSubPathEndsWithSeparator |
| | 8 | 114 | | && path[subPath.Length] != newDirectorySeparatorChar) |
| | | 115 | | { |
| | 0 | 116 | | return false; |
| | | 117 | | } |
| | | 118 | | |
| | 8 | 119 | | var newSubPathTrimmed = newSubPath.AsSpan().TrimEnd(newDirectorySeparatorChar); |
| | | 120 | | // Ensure that the path with the old subpath removed starts with a leading dir separator |
| | 8 | 121 | | int idx = oldSubPathEndsWithSeparator ? subPath.Length - 1 : subPath.Length; |
| | 8 | 122 | | newPath = string.Concat(newSubPathTrimmed, path.AsSpan(idx)); |
| | | 123 | | |
| | 8 | 124 | | return true; |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | /// <summary> |
| | | 128 | | /// Retrieves the full resolved path and normalizes path separators to the <see cref="Path.DirectorySeparatorCha |
| | | 129 | | /// </summary> |
| | | 130 | | /// <param name="path">The path to canonicalize.</param> |
| | | 131 | | /// <returns>The fully expanded, normalized path.</returns> |
| | | 132 | | public static string Canonicalize(this string path) |
| | | 133 | | { |
| | 15 | 134 | | return Path.GetFullPath(path).NormalizePath(); |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | /// <summary> |
| | | 138 | | /// Normalizes the path's directory separator character to the currently defined <see cref="Path.DirectorySepara |
| | | 139 | | /// </summary> |
| | | 140 | | /// <param name="path">The path to normalize.</param> |
| | | 141 | | /// <returns>The normalized path string or <see langword="null"/> if the input path is null or empty.</returns> |
| | | 142 | | [return: NotNullIfNotNull(nameof(path))] |
| | | 143 | | public static string? NormalizePath(this string? path) |
| | | 144 | | { |
| | 30 | 145 | | return path.NormalizePath(Path.DirectorySeparatorChar); |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | /// <summary> |
| | | 149 | | /// Normalizes the path's directory separator character. |
| | | 150 | | /// </summary> |
| | | 151 | | /// <param name="path">The path to normalize.</param> |
| | | 152 | | /// <param name="separator">The separator character the path now uses or <see langword="null"/>.</param> |
| | | 153 | | /// <returns>The normalized path string or <see langword="null"/> if the input path is null or empty.</returns> |
| | | 154 | | [return: NotNullIfNotNull(nameof(path))] |
| | | 155 | | public static string? NormalizePath(this string? path, out char separator) |
| | | 156 | | { |
| | 12 | 157 | | if (string.IsNullOrEmpty(path)) |
| | | 158 | | { |
| | 0 | 159 | | separator = default; |
| | 0 | 160 | | return path; |
| | | 161 | | } |
| | | 162 | | |
| | 12 | 163 | | var newSeparator = '\\'; |
| | | 164 | | |
| | | 165 | | // True normalization is still not possible https://github.com/dotnet/runtime/issues/2162 |
| | | 166 | | // The reasoning behind this is that a forward slash likely means it's a Linux path and |
| | | 167 | | // so the whole path should be normalized to use / and vice versa for Windows (although Windows doesn't care |
| | 12 | 168 | | if (path.Contains('/', StringComparison.Ordinal)) |
| | | 169 | | { |
| | 11 | 170 | | newSeparator = '/'; |
| | | 171 | | } |
| | | 172 | | |
| | 12 | 173 | | separator = newSeparator; |
| | | 174 | | |
| | 12 | 175 | | return path.NormalizePath(newSeparator); |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | /// <summary> |
| | | 179 | | /// Normalizes the path's directory separator character to the specified character. |
| | | 180 | | /// </summary> |
| | | 181 | | /// <param name="path">The path to normalize.</param> |
| | | 182 | | /// <param name="newSeparator">The replacement directory separator character. Must be a valid directory separato |
| | | 183 | | /// <returns>The normalized path.</returns> |
| | | 184 | | /// <exception cref="ArgumentException">Thrown if the new separator character is not a directory separator.</exc |
| | | 185 | | [return: NotNullIfNotNull(nameof(path))] |
| | | 186 | | public static string? NormalizePath(this string? path, char newSeparator) |
| | | 187 | | { |
| | | 188 | | const char Bs = '\\'; |
| | | 189 | | const char Fs = '/'; |
| | | 190 | | |
| | 67 | 191 | | if (!(newSeparator == Bs || newSeparator == Fs)) |
| | | 192 | | { |
| | 1 | 193 | | throw new ArgumentException("The character must be a directory separator."); |
| | | 194 | | } |
| | | 195 | | |
| | 66 | 196 | | if (string.IsNullOrEmpty(path)) |
| | | 197 | | { |
| | 3 | 198 | | return path; |
| | | 199 | | } |
| | | 200 | | |
| | 63 | 201 | | return newSeparator == Bs ? path.Replace(Fs, newSeparator) : path.Replace(Bs, newSeparator); |
| | | 202 | | } |
| | | 203 | | } |
| | | 204 | | } |