2
0

PathExtensions.cs 4.6 KB

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