| | | 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 | | { |
| | 284 | 22 | | if (str.Length == 0) |
| | | 23 | | { |
| | 2 | 24 | | throw new ArgumentException("String can't be empty.", nameof(str)); |
| | | 25 | | } |
| | | 26 | | |
| | 282 | 27 | | if (attribute.Length == 0) |
| | | 28 | | { |
| | 1 | 29 | | throw new ArgumentException("String can't be empty.", nameof(attribute)); |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | // Allow tmdb as an alias for tmdbid, tvdb for tvdbid, etc. |
| | | 33 | | // The code below only supports aliases for attributes in the form of "<alias>id". |
| | | 34 | | ReadOnlySpan<char> shortAttr = attribute switch |
| | | 35 | | { |
| | 377 | 36 | | _ when attribute.Equals("tmdbid", StringComparison.OrdinalIgnoreCase) => "tmdb", |
| | 235 | 37 | | _ when attribute.Equals("tvdbid", StringComparison.OrdinalIgnoreCase) => "tvdb", |
| | 187 | 38 | | _ when attribute.Equals("imdbid", StringComparison.OrdinalIgnoreCase) => "imdb", |
| | 83 | 39 | | _ => ReadOnlySpan<char>.Empty |
| | | 40 | | }; |
| | | 41 | | |
| | 1048 | 42 | | for (int strIndex = 0, attributeIndex = 0; attributeIndex > -1;) |
| | | 43 | | { |
| | | 44 | | // We may want to use imdbid pattern matching later, so we don't want to modify the original 'str'. |
| | 316 | 45 | | var subStr = str[strIndex..]; |
| | 316 | 46 | | int attributeEnd = 0; |
| | | 47 | | |
| | 316 | 48 | | if (shortAttr.Length > 0) |
| | | 49 | | { |
| | | 50 | | // If we are using an alias it should be shorter (and a prefix), so let's search for that. |
| | 233 | 51 | | attributeIndex = subStr.IndexOf(shortAttr, StringComparison.OrdinalIgnoreCase); |
| | 233 | 52 | | attributeEnd = attributeIndex + shortAttr.Length; |
| | | 53 | | } |
| | | 54 | | else |
| | | 55 | | { |
| | 83 | 56 | | attributeIndex = subStr.IndexOf(attribute, StringComparison.OrdinalIgnoreCase); |
| | 83 | 57 | | attributeEnd = attributeIndex + attribute.Length; |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | // The next iteration should start at the end of the attribute we just found. |
| | | 61 | | // If attributeIndex < 0, the loop will end and strIndex won't be used again. |
| | 316 | 62 | | strIndex += attributeEnd; |
| | | 63 | | |
| | 316 | 64 | | if (attributeIndex > 0) |
| | | 65 | | { |
| | 136 | 66 | | var attributeOpener = subStr[attributeIndex - 1]; |
| | 136 | 67 | | var attributeCloser = attributeOpener switch |
| | 136 | 68 | | { |
| | 87 | 69 | | '[' => ']', |
| | 25 | 70 | | '(' => ')', |
| | 24 | 71 | | '{' => '}', |
| | 0 | 72 | | _ => '\0' |
| | 136 | 73 | | }; |
| | | 74 | | |
| | 136 | 75 | | if (attributeCloser != '\0') |
| | | 76 | | { |
| | 136 | 77 | | if (shortAttr.Length > 0 |
| | 136 | 78 | | && attributeEnd + 1 < subStr.Length |
| | 136 | 79 | | && (subStr[attributeEnd] is 'i' or 'I') |
| | 136 | 80 | | && (subStr[attributeEnd + 1] is 'd' or 'D')) |
| | | 81 | | { |
| | | 82 | | // We were searching for a shortened attribute, but it's followed by "id" - let's skip it. |
| | 78 | 83 | | attributeEnd += 2; |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | // attributeEnd points at '='. |
| | | 87 | | // We need at least 1 more character and the closing bracket after that. |
| | 136 | 88 | | if (attributeEnd + 2 < subStr.Length && (subStr[attributeEnd] is '=' or '-')) |
| | | 89 | | { |
| | 129 | 90 | | var closingIndex = subStr[attributeEnd..].IndexOf(attributeCloser); |
| | | 91 | | |
| | | 92 | | // Must be at least 1 character before the closing bracket. |
| | 129 | 93 | | if (closingIndex > 1) |
| | | 94 | | { |
| | 115 | 95 | | var trimmed = subStr[(attributeEnd + 1)..(attributeEnd + closingIndex)].Trim(); |
| | | 96 | | |
| | 115 | 97 | | if (trimmed.Length > 0) |
| | | 98 | | { |
| | 111 | 99 | | return trimmed.ToString(); |
| | | 100 | | } |
| | | 101 | | } |
| | | 102 | | } |
| | | 103 | | } |
| | | 104 | | } |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | // for imdbid we also accept pattern matching |
| | 170 | 108 | | if (attribute.Equals("imdbid", StringComparison.OrdinalIgnoreCase)) |
| | | 109 | | { |
| | 26 | 110 | | var match = ProviderIdParsers.TryFindImdbId(str, out var imdbId); |
| | 26 | 111 | | return match ? imdbId.ToString() : null; |
| | | 112 | | } |
| | | 113 | | |
| | 144 | 114 | | return null; |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// Replaces a sub path with another sub path and normalizes the final path. |
| | | 119 | | /// </summary> |
| | | 120 | | /// <param name="path">The original path.</param> |
| | | 121 | | /// <param name="subPath">The original sub path.</param> |
| | | 122 | | /// <param name="newSubPath">The new sub path.</param> |
| | | 123 | | /// <param name="newPath">The result of the sub path replacement.</param> |
| | | 124 | | /// <returns>The path after replacing the sub path.</returns> |
| | | 125 | | /// <exception cref="ArgumentNullException"><paramref name="path" />, <paramref name="newSubPath" /> or <paramre |
| | | 126 | | public static bool TryReplaceSubPath( |
| | | 127 | | [NotNullWhen(true)] this string? path, |
| | | 128 | | [NotNullWhen(true)] string? subPath, |
| | | 129 | | [NotNullWhen(true)] string? newSubPath, |
| | | 130 | | [NotNullWhen(true)] out string? newPath) |
| | | 131 | | { |
| | 17 | 132 | | newPath = null; |
| | | 133 | | |
| | 17 | 134 | | if (string.IsNullOrEmpty(path) |
| | 17 | 135 | | || string.IsNullOrEmpty(subPath) |
| | 17 | 136 | | || string.IsNullOrEmpty(newSubPath) |
| | 17 | 137 | | || subPath.Length > path.Length) |
| | | 138 | | { |
| | 8 | 139 | | return false; |
| | | 140 | | } |
| | | 141 | | |
| | 9 | 142 | | subPath = subPath.NormalizePath(out var newDirectorySeparatorChar); |
| | 9 | 143 | | path = path.NormalizePath(newDirectorySeparatorChar); |
| | | 144 | | |
| | | 145 | | // We have to ensure that the sub path ends with a directory separator otherwise we'll get weird results |
| | | 146 | | // when the sub path matches a similar but in-complete subpath |
| | 9 | 147 | | var oldSubPathEndsWithSeparator = subPath[^1] == newDirectorySeparatorChar; |
| | 9 | 148 | | if (!path.StartsWith(subPath, StringComparison.OrdinalIgnoreCase)) |
| | | 149 | | { |
| | 1 | 150 | | return false; |
| | | 151 | | } |
| | | 152 | | |
| | 8 | 153 | | if (path.Length > subPath.Length |
| | 8 | 154 | | && !oldSubPathEndsWithSeparator |
| | 8 | 155 | | && path[subPath.Length] != newDirectorySeparatorChar) |
| | | 156 | | { |
| | 0 | 157 | | return false; |
| | | 158 | | } |
| | | 159 | | |
| | 8 | 160 | | var newSubPathTrimmed = newSubPath.AsSpan().TrimEnd(newDirectorySeparatorChar); |
| | | 161 | | // Ensure that the path with the old subpath removed starts with a leading dir separator |
| | 8 | 162 | | int idx = oldSubPathEndsWithSeparator ? subPath.Length - 1 : subPath.Length; |
| | 8 | 163 | | newPath = string.Concat(newSubPathTrimmed, path.AsSpan(idx)); |
| | | 164 | | |
| | 8 | 165 | | return true; |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | /// <summary> |
| | | 169 | | /// Retrieves the full resolved path and normalizes path separators to the <see cref="Path.DirectorySeparatorCha |
| | | 170 | | /// </summary> |
| | | 171 | | /// <param name="path">The path to canonicalize.</param> |
| | | 172 | | /// <returns>The fully expanded, normalized path.</returns> |
| | | 173 | | public static string Canonicalize(this string path) |
| | | 174 | | { |
| | 15 | 175 | | return Path.GetFullPath(path).NormalizePath(); |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | /// <summary> |
| | | 179 | | /// Normalizes the path's directory separator character to the currently defined <see cref="Path.DirectorySepara |
| | | 180 | | /// </summary> |
| | | 181 | | /// <param name="path">The path to normalize.</param> |
| | | 182 | | /// <returns>The normalized path string or <see langword="null"/> if the input path is null or empty.</returns> |
| | | 183 | | [return: NotNullIfNotNull(nameof(path))] |
| | | 184 | | public static string? NormalizePath(this string? path) |
| | | 185 | | { |
| | 30 | 186 | | return path.NormalizePath(Path.DirectorySeparatorChar); |
| | | 187 | | } |
| | | 188 | | |
| | | 189 | | /// <summary> |
| | | 190 | | /// Normalizes the path's directory separator character. |
| | | 191 | | /// </summary> |
| | | 192 | | /// <param name="path">The path to normalize.</param> |
| | | 193 | | /// <param name="separator">The separator character the path now uses or <see langword="null"/>.</param> |
| | | 194 | | /// <returns>The normalized path string or <see langword="null"/> if the input path is null or empty.</returns> |
| | | 195 | | [return: NotNullIfNotNull(nameof(path))] |
| | | 196 | | public static string? NormalizePath(this string? path, out char separator) |
| | | 197 | | { |
| | 12 | 198 | | if (string.IsNullOrEmpty(path)) |
| | | 199 | | { |
| | 0 | 200 | | separator = default; |
| | 0 | 201 | | return path; |
| | | 202 | | } |
| | | 203 | | |
| | 12 | 204 | | var newSeparator = '\\'; |
| | | 205 | | |
| | | 206 | | // True normalization is still not possible https://github.com/dotnet/runtime/issues/2162 |
| | | 207 | | // The reasoning behind this is that a forward slash likely means it's a Linux path and |
| | | 208 | | // so the whole path should be normalized to use / and vice versa for Windows (although Windows doesn't care |
| | 12 | 209 | | if (path.Contains('/', StringComparison.Ordinal)) |
| | | 210 | | { |
| | 11 | 211 | | newSeparator = '/'; |
| | | 212 | | } |
| | | 213 | | |
| | 12 | 214 | | separator = newSeparator; |
| | | 215 | | |
| | 12 | 216 | | return path.NormalizePath(newSeparator); |
| | | 217 | | } |
| | | 218 | | |
| | | 219 | | /// <summary> |
| | | 220 | | /// Normalizes the path's directory separator character to the specified character. |
| | | 221 | | /// </summary> |
| | | 222 | | /// <param name="path">The path to normalize.</param> |
| | | 223 | | /// <param name="newSeparator">The replacement directory separator character. Must be a valid directory separato |
| | | 224 | | /// <returns>The normalized path.</returns> |
| | | 225 | | /// <exception cref="ArgumentException">Thrown if the new separator character is not a directory separator.</exc |
| | | 226 | | [return: NotNullIfNotNull(nameof(path))] |
| | | 227 | | public static string? NormalizePath(this string? path, char newSeparator) |
| | | 228 | | { |
| | | 229 | | const char Bs = '\\'; |
| | | 230 | | const char Fs = '/'; |
| | | 231 | | |
| | 67 | 232 | | if (!(newSeparator == Bs || newSeparator == Fs)) |
| | | 233 | | { |
| | 1 | 234 | | throw new ArgumentException("The character must be a directory separator."); |
| | | 235 | | } |
| | | 236 | | |
| | 66 | 237 | | if (string.IsNullOrEmpty(path)) |
| | | 238 | | { |
| | 3 | 239 | | return path; |
| | | 240 | | } |
| | | 241 | | |
| | 63 | 242 | | return newSeparator == Bs ? path.Replace(Fs, newSeparator) : path.Replace(Bs, newSeparator); |
| | | 243 | | } |
| | | 244 | | } |
| | | 245 | | } |