PathExtensions.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 string str, string 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. string srch = "[" + attribute + "=";
  29. int start = str.IndexOf(srch, StringComparison.OrdinalIgnoreCase);
  30. if (start != -1)
  31. {
  32. start += srch.Length;
  33. int end = str.IndexOf(']', start);
  34. return str.Substring(start, end - start);
  35. }
  36. // for imdbid we also accept pattern matching
  37. if (string.Equals(attribute, "imdbid", StringComparison.OrdinalIgnoreCase))
  38. {
  39. var match = ProviderIdParsers.TryFindImdbId(str, out var imdbId);
  40. return match ? imdbId.ToString() : null;
  41. }
  42. return null;
  43. }
  44. /// <summary>
  45. /// Replaces a sub path with another sub path and normalizes the final path.
  46. /// </summary>
  47. /// <param name="path">The original path.</param>
  48. /// <param name="subPath">The original sub path.</param>
  49. /// <param name="newSubPath">The new sub path.</param>
  50. /// <param name="newPath">The result of the sub path replacement</param>
  51. /// <returns>The path after replacing the sub path.</returns>
  52. /// <exception cref="ArgumentNullException"><paramref name="path" />, <paramref name="newSubPath" /> or <paramref name="newSubPath" /> is empty.</exception>
  53. public static bool TryReplaceSubPath(
  54. [NotNullWhen(true)] this string? path,
  55. [NotNullWhen(true)] string? subPath,
  56. [NotNullWhen(true)] string? newSubPath,
  57. [NotNullWhen(true)] out string? newPath)
  58. {
  59. newPath = null;
  60. if (string.IsNullOrEmpty(path)
  61. || string.IsNullOrEmpty(subPath)
  62. || string.IsNullOrEmpty(newSubPath)
  63. || subPath.Length > path.Length)
  64. {
  65. return false;
  66. }
  67. char oldDirectorySeparatorChar;
  68. char newDirectorySeparatorChar;
  69. // True normalization is still not possible https://github.com/dotnet/runtime/issues/2162
  70. // The reasoning behind this is that a forward slash likely means it's a Linux path and
  71. // so the whole path should be normalized to use / and vice versa for Windows (although Windows doesn't care much).
  72. if (newSubPath.Contains('/', StringComparison.Ordinal))
  73. {
  74. oldDirectorySeparatorChar = '\\';
  75. newDirectorySeparatorChar = '/';
  76. }
  77. else
  78. {
  79. oldDirectorySeparatorChar = '/';
  80. newDirectorySeparatorChar = '\\';
  81. }
  82. path = path.Replace(oldDirectorySeparatorChar, newDirectorySeparatorChar);
  83. subPath = subPath.Replace(oldDirectorySeparatorChar, newDirectorySeparatorChar);
  84. // We have to ensure that the sub path ends with a directory separator otherwise we'll get weird results
  85. // when the sub path matches a similar but in-complete subpath
  86. var oldSubPathEndsWithSeparator = subPath[^1] == newDirectorySeparatorChar;
  87. if (!path.StartsWith(subPath, StringComparison.OrdinalIgnoreCase))
  88. {
  89. return false;
  90. }
  91. if (path.Length > subPath.Length
  92. && !oldSubPathEndsWithSeparator
  93. && path[subPath.Length] != newDirectorySeparatorChar)
  94. {
  95. return false;
  96. }
  97. var newSubPathTrimmed = newSubPath.AsSpan().TrimEnd(newDirectorySeparatorChar);
  98. // Ensure that the path with the old subpath removed starts with a leading dir separator
  99. int idx = oldSubPathEndsWithSeparator ? subPath.Length - 1 : subPath.Length;
  100. newPath = string.Concat(newSubPathTrimmed, path.AsSpan(idx));
  101. return true;
  102. }
  103. }
  104. }