PathExtensions.cs 4.7 KB

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