mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-04-24 14:08:44 -04:00
Merge pull request #5381 from cvium/fix-network-substitution
This commit is contained in:
commit
497ea57fd2
3 changed files with 92 additions and 58 deletions
|
@ -2776,6 +2776,7 @@ namespace Emby.Server.Implementations.Library
|
||||||
|
|
||||||
public string GetPathAfterNetworkSubstitution(string path, BaseItem ownerItem)
|
public string GetPathAfterNetworkSubstitution(string path, BaseItem ownerItem)
|
||||||
{
|
{
|
||||||
|
string newPath;
|
||||||
if (ownerItem != null)
|
if (ownerItem != null)
|
||||||
{
|
{
|
||||||
var libraryOptions = GetLibraryOptions(ownerItem);
|
var libraryOptions = GetLibraryOptions(ownerItem);
|
||||||
|
@ -2783,15 +2784,9 @@ namespace Emby.Server.Implementations.Library
|
||||||
{
|
{
|
||||||
foreach (var pathInfo in libraryOptions.PathInfos)
|
foreach (var pathInfo in libraryOptions.PathInfos)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(pathInfo.Path) || string.IsNullOrWhiteSpace(pathInfo.NetworkPath))
|
if (path.TryReplaceSubPath(pathInfo.Path, pathInfo.NetworkPath, out newPath))
|
||||||
{
|
{
|
||||||
continue;
|
return newPath;
|
||||||
}
|
|
||||||
|
|
||||||
var substitutionResult = SubstitutePathInternal(path, pathInfo.Path, pathInfo.NetworkPath);
|
|
||||||
if (substitutionResult.Item2)
|
|
||||||
{
|
|
||||||
return substitutionResult.Item1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2800,24 +2795,16 @@ namespace Emby.Server.Implementations.Library
|
||||||
var metadataPath = _configurationManager.Configuration.MetadataPath;
|
var metadataPath = _configurationManager.Configuration.MetadataPath;
|
||||||
var metadataNetworkPath = _configurationManager.Configuration.MetadataNetworkPath;
|
var metadataNetworkPath = _configurationManager.Configuration.MetadataNetworkPath;
|
||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(metadataPath) && !string.IsNullOrWhiteSpace(metadataNetworkPath))
|
if (path.TryReplaceSubPath(metadataPath, metadataNetworkPath, out newPath))
|
||||||
{
|
{
|
||||||
var metadataSubstitutionResult = SubstitutePathInternal(path, metadataPath, metadataNetworkPath);
|
return newPath;
|
||||||
if (metadataSubstitutionResult.Item2)
|
|
||||||
{
|
|
||||||
return metadataSubstitutionResult.Item1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var map in _configurationManager.Configuration.PathSubstitutions)
|
foreach (var map in _configurationManager.Configuration.PathSubstitutions)
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrWhiteSpace(map.From))
|
if (path.TryReplaceSubPath(map.From, map.To, out newPath))
|
||||||
{
|
{
|
||||||
var substitutionResult = SubstitutePathInternal(path, map.From, map.To);
|
return newPath;
|
||||||
if (substitutionResult.Item2)
|
|
||||||
{
|
|
||||||
return substitutionResult.Item1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2826,47 +2813,12 @@ namespace Emby.Server.Implementations.Library
|
||||||
|
|
||||||
public string SubstitutePath(string path, string from, string to)
|
public string SubstitutePath(string path, string from, string to)
|
||||||
{
|
{
|
||||||
return SubstitutePathInternal(path, from, to).Item1;
|
if (path.TryReplaceSubPath(from, to, out var newPath))
|
||||||
}
|
|
||||||
|
|
||||||
private Tuple<string, bool> SubstitutePathInternal(string path, string from, string to)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(path))
|
|
||||||
{
|
{
|
||||||
throw new ArgumentNullException(nameof(path));
|
return newPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(from))
|
return path;
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(from));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(to))
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(to));
|
|
||||||
}
|
|
||||||
|
|
||||||
from = from.Trim();
|
|
||||||
to = to.Trim();
|
|
||||||
|
|
||||||
var newPath = path.Replace(from, to, StringComparison.OrdinalIgnoreCase);
|
|
||||||
var changed = false;
|
|
||||||
|
|
||||||
if (!string.Equals(newPath, path, StringComparison.Ordinal))
|
|
||||||
{
|
|
||||||
if (to.IndexOf('/', StringComparison.Ordinal) != -1)
|
|
||||||
{
|
|
||||||
newPath = newPath.Replace('\\', '/');
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
newPath = newPath.Replace('/', '\\');
|
|
||||||
}
|
|
||||||
|
|
||||||
changed = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Tuple<string, bool>(newPath, changed);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetExtraTypeFromFilename(Video item)
|
private void SetExtraTypeFromFilename(Video item)
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
#nullable enable
|
#nullable enable
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using System.IO;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace Emby.Server.Implementations.Library
|
namespace Emby.Server.Implementations.Library
|
||||||
|
@ -47,5 +49,59 @@ namespace Emby.Server.Implementations.Library
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
/// <param name="newPath">The result of the sub path replacement</param>
|
||||||
|
/// <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>
|
||||||
|
public static bool TryReplaceSubPath(this string path, string subPath, string newSubPath, [NotNullWhen(true)] out string? newPath)
|
||||||
|
{
|
||||||
|
newPath = null;
|
||||||
|
|
||||||
|
if (path.Length == 0 || subPath.Length == 0 || newSubPath.Length == 0 || subPath.Length > path.Length)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 = '\\';
|
||||||
|
}
|
||||||
|
|
||||||
|
path = path.Replace(oldDirectorySeparatorChar, newDirectorySeparatorChar);
|
||||||
|
subPath = subPath.Replace(oldDirectorySeparatorChar, newDirectorySeparatorChar);
|
||||||
|
|
||||||
|
// 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
|
||||||
|
var oldSubPathEndsWithSeparator = subPath[^1] == newDirectorySeparatorChar;
|
||||||
|
if (!path.StartsWith(subPath, StringComparison.OrdinalIgnoreCase)
|
||||||
|
|| (!oldSubPathEndsWithSeparator && path[subPath.Length] != newDirectorySeparatorChar))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,5 +24,31 @@ namespace Jellyfin.Server.Implementations.Tests.Library
|
||||||
{
|
{
|
||||||
Assert.Throws<ArgumentException>(() => PathExtensions.GetAttributeValue(input, attribute));
|
Assert.Throws<ArgumentException>(() => PathExtensions.GetAttributeValue(input, attribute));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("C:/Users/jeff/myfile.mkv", "C:/Users/jeff", "/home/jeff", "/home/jeff/myfile.mkv")]
|
||||||
|
[InlineData("C:/Users/jeff/myfile.mkv", "C:/Users/jeff/", "/home/jeff", "/home/jeff/myfile.mkv")]
|
||||||
|
[InlineData("/home/jeff/music/jeff's band/consistently inconsistent.mp3", "/home/jeff/music/jeff's band", "/home/not jeff", "/home/not jeff/consistently inconsistent.mp3")]
|
||||||
|
[InlineData("C:\\Users\\jeff\\myfile.mkv", "C:\\Users/jeff", "/home/jeff", "/home/jeff/myfile.mkv")]
|
||||||
|
[InlineData("C:\\Users\\jeff\\myfile.mkv", "C:\\Users/jeff", "/home/jeff/", "/home/jeff/myfile.mkv")]
|
||||||
|
[InlineData("C:\\Users\\jeff\\myfile.mkv", "C:\\Users/jeff/", "/home/jeff/", "/home/jeff/myfile.mkv")]
|
||||||
|
[InlineData("C:\\Users\\jeff\\myfile.mkv", "C:\\Users/jeff/", "/", "/myfile.mkv")]
|
||||||
|
public void TryReplaceSubPath_ValidArgs_Correct(string path, string subPath, string newSubPath, string? expectedResult)
|
||||||
|
{
|
||||||
|
Assert.True(PathExtensions.TryReplaceSubPath(path, subPath, newSubPath, out var result));
|
||||||
|
Assert.Equal(expectedResult, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[InlineData("", "", "")]
|
||||||
|
[InlineData("/my/path", "", "")]
|
||||||
|
[InlineData("", "/another/path", "")]
|
||||||
|
[InlineData("", "", "/new/subpath")]
|
||||||
|
[InlineData("/home/jeff/music/jeff's band/consistently inconsistent.mp3", "/home/jeff/music/not jeff's band", "/home/not jeff")]
|
||||||
|
public void TryReplaceSubPath_InvalidInput_ReturnsFalseAndNull(string path, string subPath, string newSubPath)
|
||||||
|
{
|
||||||
|
Assert.False(PathExtensions.TryReplaceSubPath(path, subPath, newSubPath, out var result));
|
||||||
|
Assert.Null(result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue