PathExtensions.cs 4.7 KB

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