PathExtensions.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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(
  56. [NotNullWhen(true)] this string? path,
  57. [NotNullWhen(true)] string? subPath,
  58. [NotNullWhen(true)] string? newSubPath,
  59. [NotNullWhen(true)] out string? newPath)
  60. {
  61. newPath = null;
  62. if (string.IsNullOrEmpty(path)
  63. || string.IsNullOrEmpty(subPath)
  64. || string.IsNullOrEmpty(newSubPath)
  65. || subPath.Length > path.Length)
  66. {
  67. return false;
  68. }
  69. char oldDirectorySeparatorChar;
  70. char newDirectorySeparatorChar;
  71. // True normalization is still not possible https://github.com/dotnet/runtime/issues/2162
  72. // The reasoning behind this is that a forward slash likely means it's a Linux path and
  73. // so the whole path should be normalized to use / and vice versa for Windows (although Windows doesn't care much).
  74. if (newSubPath.Contains('/', StringComparison.Ordinal))
  75. {
  76. oldDirectorySeparatorChar = '\\';
  77. newDirectorySeparatorChar = '/';
  78. }
  79. else
  80. {
  81. oldDirectorySeparatorChar = '/';
  82. newDirectorySeparatorChar = '\\';
  83. }
  84. path = path.Replace(oldDirectorySeparatorChar, newDirectorySeparatorChar);
  85. subPath = subPath.Replace(oldDirectorySeparatorChar, newDirectorySeparatorChar);
  86. // We have to ensure that the sub path ends with a directory separator otherwise we'll get weird results
  87. // when the sub path matches a similar but in-complete subpath
  88. var oldSubPathEndsWithSeparator = subPath[^1] == newDirectorySeparatorChar;
  89. if (!path.StartsWith(subPath, StringComparison.OrdinalIgnoreCase)
  90. || (!oldSubPathEndsWithSeparator && path[subPath.Length] != newDirectorySeparatorChar))
  91. {
  92. return false;
  93. }
  94. var newSubPathTrimmed = newSubPath.AsSpan().TrimEnd(newDirectorySeparatorChar);
  95. // Ensure that the path with the old subpath removed starts with a leading dir separator
  96. int idx = oldSubPathEndsWithSeparator ? subPath.Length - 1 : subPath.Length;
  97. newPath = string.Concat(newSubPathTrimmed, path.AsSpan(idx));
  98. return true;
  99. }
  100. }
  101. }