2019-01-13 14:54:44 -05:00
|
|
|
using System;
|
2021-03-05 08:09:23 -05:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2021-03-16 16:11:34 -04:00
|
|
|
using MediaBrowser.Common.Providers;
|
2014-12-01 07:43:34 -05:00
|
|
|
|
2016-11-03 02:37:52 -04:00
|
|
|
namespace Emby.Server.Implementations.Library
|
2014-12-01 07:43:34 -05:00
|
|
|
{
|
2019-11-01 13:38:54 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Class providing extension methods for working with paths.
|
|
|
|
/// </summary>
|
2014-12-01 07:43:34 -05:00
|
|
|
public static class PathExtensions
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the attribute value.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="str">The STR.</param>
|
2020-04-19 09:18:28 -04:00
|
|
|
/// <param name="attribute">The attrib.</param>
|
2014-12-01 07:43:34 -05:00
|
|
|
/// <returns>System.String.</returns>
|
2020-04-21 04:18:26 -04:00
|
|
|
/// <exception cref="ArgumentException"><paramref name="str" /> or <paramref name="attribute" /> is empty.</exception>
|
2021-11-27 18:10:43 -05:00
|
|
|
public static string? GetAttributeValue(this ReadOnlySpan<char> str, ReadOnlySpan<char> attribute)
|
2014-12-01 07:43:34 -05:00
|
|
|
{
|
2020-04-19 09:18:28 -04:00
|
|
|
if (str.Length == 0)
|
2014-12-01 07:43:34 -05:00
|
|
|
{
|
2020-04-19 09:18:28 -04:00
|
|
|
throw new ArgumentException("String can't be empty.", nameof(str));
|
2014-12-01 07:43:34 -05:00
|
|
|
}
|
|
|
|
|
2020-04-19 09:18:28 -04:00
|
|
|
if (attribute.Length == 0)
|
2014-12-01 07:43:34 -05:00
|
|
|
{
|
2020-04-19 09:18:28 -04:00
|
|
|
throw new ArgumentException("String can't be empty.", nameof(attribute));
|
2014-12-01 07:43:34 -05:00
|
|
|
}
|
|
|
|
|
2021-11-27 18:10:43 -05:00
|
|
|
var openBracketIndex = str.IndexOf('[');
|
2021-12-07 19:10:27 -05:00
|
|
|
var attributeIndex = str.IndexOf(attribute);
|
2021-11-27 18:10:43 -05:00
|
|
|
var closingBracketIndex = str.IndexOf(']');
|
2021-12-07 19:10:27 -05:00
|
|
|
while (openBracketIndex < attributeIndex && attributeIndex < closingBracketIndex)
|
2014-12-01 07:43:34 -05:00
|
|
|
{
|
2021-12-07 19:10:27 -05:00
|
|
|
if (openBracketIndex + 1 == attributeIndex
|
|
|
|
&& str[attributeIndex + attribute.Length] == '=')
|
2021-11-27 18:10:43 -05:00
|
|
|
{
|
2021-12-07 19:10:27 -05:00
|
|
|
return str[(attributeIndex + attribute.Length + 1)..closingBracketIndex].Trim().ToString();
|
2021-11-27 18:10:43 -05:00
|
|
|
}
|
|
|
|
|
2021-12-07 19:10:27 -05:00
|
|
|
str = str[(closingBracketIndex + 1)..];
|
2021-11-27 18:10:43 -05:00
|
|
|
openBracketIndex = str.IndexOf('[');
|
2021-12-07 19:10:27 -05:00
|
|
|
attributeIndex = str.IndexOf(attribute);
|
2021-11-27 18:10:43 -05:00
|
|
|
closingBracketIndex = str.IndexOf(']');
|
2014-12-01 07:43:34 -05:00
|
|
|
}
|
2019-11-01 13:38:54 -04:00
|
|
|
|
2015-01-02 09:29:20 -05:00
|
|
|
// for imdbid we also accept pattern matching
|
2021-11-27 18:10:43 -05:00
|
|
|
if (attribute.Equals("imdbid", StringComparison.OrdinalIgnoreCase))
|
2015-01-02 09:29:20 -05:00
|
|
|
{
|
2021-03-26 12:06:01 -04:00
|
|
|
var match = ProviderIdParsers.TryFindImdbId(str, out var imdbId);
|
|
|
|
return match ? imdbId.ToString() : null;
|
2015-01-02 09:29:20 -05:00
|
|
|
}
|
2015-01-04 09:34:15 -05:00
|
|
|
|
2014-12-01 07:43:34 -05:00
|
|
|
return null;
|
|
|
|
}
|
2021-03-05 08:09:23 -05:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Replaces a sub path with another sub path and normalizes the final path.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="path">The original path.</param>
|
|
|
|
/// <param name="subPath">The original sub path.</param>
|
|
|
|
/// <param name="newSubPath">The new sub path.</param>
|
2021-10-02 13:59:58 -04:00
|
|
|
/// <param name="newPath">The result of the sub path replacement.</param>
|
2021-03-05 08:09:23 -05:00
|
|
|
/// <returns>The path after replacing the sub path.</returns>
|
|
|
|
/// <exception cref="ArgumentNullException"><paramref name="path" />, <paramref name="newSubPath" /> or <paramref name="newSubPath" /> is empty.</exception>
|
2021-03-08 19:28:21 -05:00
|
|
|
public static bool TryReplaceSubPath(
|
|
|
|
[NotNullWhen(true)] this string? path,
|
|
|
|
[NotNullWhen(true)] string? subPath,
|
|
|
|
[NotNullWhen(true)] string? newSubPath,
|
|
|
|
[NotNullWhen(true)] out string? newPath)
|
2021-03-05 08:09:23 -05:00
|
|
|
{
|
2021-03-06 13:07:02 -05:00
|
|
|
newPath = null;
|
2021-03-05 08:09:23 -05:00
|
|
|
|
2021-03-08 19:28:21 -05:00
|
|
|
if (string.IsNullOrEmpty(path)
|
|
|
|
|| string.IsNullOrEmpty(subPath)
|
|
|
|
|| string.IsNullOrEmpty(newSubPath)
|
|
|
|
|| subPath.Length > path.Length)
|
2021-03-05 08:09:23 -05:00
|
|
|
{
|
2021-03-06 13:07:02 -05:00
|
|
|
return false;
|
2021-03-05 08:09:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
char oldDirectorySeparatorChar;
|
|
|
|
char newDirectorySeparatorChar;
|
|
|
|
// True normalization is still not possible https://github.com/dotnet/runtime/issues/2162
|
|
|
|
// The reasoning behind this is that a forward slash likely means it's a Linux path and
|
|
|
|
// so the whole path should be normalized to use / and vice versa for Windows (although Windows doesn't care much).
|
|
|
|
if (newSubPath.Contains('/', StringComparison.Ordinal))
|
|
|
|
{
|
|
|
|
oldDirectorySeparatorChar = '\\';
|
|
|
|
newDirectorySeparatorChar = '/';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
oldDirectorySeparatorChar = '/';
|
|
|
|
newDirectorySeparatorChar = '\\';
|
|
|
|
}
|
|
|
|
|
2021-03-06 13:07:02 -05:00
|
|
|
path = path.Replace(oldDirectorySeparatorChar, newDirectorySeparatorChar);
|
|
|
|
subPath = subPath.Replace(oldDirectorySeparatorChar, newDirectorySeparatorChar);
|
2021-03-05 08:09:23 -05:00
|
|
|
|
|
|
|
// We have to ensure that the sub path ends with a directory separator otherwise we'll get weird results
|
|
|
|
// when the sub path matches a similar but in-complete subpath
|
2021-03-06 08:01:37 -05:00
|
|
|
var oldSubPathEndsWithSeparator = subPath[^1] == newDirectorySeparatorChar;
|
2021-05-05 10:43:20 -04:00
|
|
|
if (!path.StartsWith(subPath, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path.Length > subPath.Length
|
|
|
|
&& !oldSubPathEndsWithSeparator
|
|
|
|
&& path[subPath.Length] != newDirectorySeparatorChar)
|
2021-03-05 08:09:23 -05:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-06 08:01:37 -05:00
|
|
|
var newSubPathTrimmed = newSubPath.AsSpan().TrimEnd(newDirectorySeparatorChar);
|
|
|
|
// Ensure that the path with the old subpath removed starts with a leading dir separator
|
|
|
|
int idx = oldSubPathEndsWithSeparator ? subPath.Length - 1 : subPath.Length;
|
|
|
|
newPath = string.Concat(newSubPathTrimmed, path.AsSpan(idx));
|
|
|
|
|
2021-03-05 08:09:23 -05:00
|
|
|
return true;
|
|
|
|
}
|
2014-12-01 07:43:34 -05:00
|
|
|
}
|
|
|
|
}
|