PathExtensions.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Diagnostics.CodeAnalysis;
  3. using MediaBrowser.Common.Providers;
  4. namespace Emby.Server.Implementations.Library
  5. {
  6. /// <summary>
  7. /// Class providing extension methods for working with paths.
  8. /// </summary>
  9. public static class PathExtensions
  10. {
  11. /// <summary>
  12. /// Gets the attribute value.
  13. /// </summary>
  14. /// <param name="str">The STR.</param>
  15. /// <param name="attribute">The attrib.</param>
  16. /// <returns>System.String.</returns>
  17. /// <exception cref="ArgumentException"><paramref name="str" /> or <paramref name="attribute" /> is empty.</exception>
  18. public static string? GetAttributeValue(this ReadOnlySpan<char> str, ReadOnlySpan<char> attribute)
  19. {
  20. if (str.Length == 0)
  21. {
  22. throw new ArgumentException("String can't be empty.", nameof(str));
  23. }
  24. if (attribute.Length == 0)
  25. {
  26. throw new ArgumentException("String can't be empty.", nameof(attribute));
  27. }
  28. var openBracketIndex = str.IndexOf('[');
  29. var equalsIndex = str.IndexOf('=');
  30. var closingBracketIndex = str.IndexOf(']');
  31. while (openBracketIndex < equalsIndex && equalsIndex < closingBracketIndex)
  32. {
  33. if (str[(openBracketIndex + 1)..equalsIndex].Equals(attribute, StringComparison.OrdinalIgnoreCase))
  34. {
  35. return str[(equalsIndex + 1)..closingBracketIndex].Trim().ToString();
  36. }
  37. str = str[(closingBracketIndex+ 1)..];
  38. openBracketIndex = str.IndexOf('[');
  39. equalsIndex = str.IndexOf('=');
  40. closingBracketIndex = str.IndexOf(']');
  41. }
  42. // for imdbid we also accept pattern matching
  43. if (attribute.Equals("imdbid", StringComparison.OrdinalIgnoreCase))
  44. {
  45. var match = ProviderIdParsers.TryFindImdbId(str, out var imdbId);
  46. return match ? imdbId.ToString() : null;
  47. }
  48. return null;
  49. }
  50. /// <summary>
  51. /// Replaces a sub path with another sub path and normalizes the final path.
  52. /// </summary>
  53. /// <param name="path">The original path.</param>
  54. /// <param name="subPath">The original sub path.</param>
  55. /// <param name="newSubPath">The new sub path.</param>
  56. /// <param name="newPath">The result of the sub path replacement.</param>
  57. /// <returns>The path after replacing the sub path.</returns>
  58. /// <exception cref="ArgumentNullException"><paramref name="path" />, <paramref name="newSubPath" /> or <paramref name="newSubPath" /> is empty.</exception>
  59. public static bool TryReplaceSubPath(
  60. [NotNullWhen(true)] this string? path,
  61. [NotNullWhen(true)] string? subPath,
  62. [NotNullWhen(true)] string? newSubPath,
  63. [NotNullWhen(true)] out string? newPath)
  64. {
  65. newPath = null;
  66. if (string.IsNullOrEmpty(path)
  67. || string.IsNullOrEmpty(subPath)
  68. || string.IsNullOrEmpty(newSubPath)
  69. || subPath.Length > path.Length)
  70. {
  71. return false;
  72. }
  73. char oldDirectorySeparatorChar;
  74. char newDirectorySeparatorChar;
  75. // True normalization is still not possible https://github.com/dotnet/runtime/issues/2162
  76. // The reasoning behind this is that a forward slash likely means it's a Linux path and
  77. // so the whole path should be normalized to use / and vice versa for Windows (although Windows doesn't care much).
  78. if (newSubPath.Contains('/', StringComparison.Ordinal))
  79. {
  80. oldDirectorySeparatorChar = '\\';
  81. newDirectorySeparatorChar = '/';
  82. }
  83. else
  84. {
  85. oldDirectorySeparatorChar = '/';
  86. newDirectorySeparatorChar = '\\';
  87. }
  88. path = path.Replace(oldDirectorySeparatorChar, newDirectorySeparatorChar);
  89. subPath = subPath.Replace(oldDirectorySeparatorChar, newDirectorySeparatorChar);
  90. // We have to ensure that the sub path ends with a directory separator otherwise we'll get weird results
  91. // when the sub path matches a similar but in-complete subpath
  92. var oldSubPathEndsWithSeparator = subPath[^1] == newDirectorySeparatorChar;
  93. if (!path.StartsWith(subPath, StringComparison.OrdinalIgnoreCase))
  94. {
  95. return false;
  96. }
  97. if (path.Length > subPath.Length
  98. && !oldSubPathEndsWithSeparator
  99. && path[subPath.Length] != newDirectorySeparatorChar)
  100. {
  101. return false;
  102. }
  103. var newSubPathTrimmed = newSubPath.AsSpan().TrimEnd(newDirectorySeparatorChar);
  104. // Ensure that the path with the old subpath removed starts with a leading dir separator
  105. int idx = oldSubPathEndsWithSeparator ? subPath.Length - 1 : subPath.Length;
  106. newPath = string.Concat(newSubPathTrimmed, path.AsSpan(idx));
  107. return true;
  108. }
  109. }
  110. }