Backport pull request #11570 from jellyfin/release-10.9.z

Fix absolute path checking on windows

Original-merge: 6689d837d6dcfa0925efdbd9c76a7e1fe4f7cc54

Merged-by: crobibero <cody@robibe.ro>

Backported-by: Joshua M. Boniface <joshua@boniface.me>
This commit is contained in:
gnattu 2024-05-13 12:47:35 -04:00 committed by Joshua M. Boniface
parent 3564b00fc0
commit 5ac518b02a
2 changed files with 28 additions and 22 deletions

View File

@ -80,12 +80,14 @@ namespace Emby.Server.Implementations.IO
public virtual string MakeAbsolutePath(string folderPath, string filePath) public virtual string MakeAbsolutePath(string folderPath, string filePath)
{ {
// path is actually a stream // path is actually a stream
if (string.IsNullOrWhiteSpace(filePath) || filePath.Contains("://", StringComparison.Ordinal)) if (string.IsNullOrWhiteSpace(filePath))
{ {
return filePath; return filePath;
} }
if (filePath.Length > 3 && filePath[1] == ':' && filePath[2] == '/') var isAbsolutePath = Path.IsPathRooted(filePath) && (!OperatingSystem.IsWindows() || filePath[0] != '\\');
if (isAbsolutePath)
{ {
// absolute local path // absolute local path
return filePath; return filePath;
@ -97,17 +99,10 @@ namespace Emby.Server.Implementations.IO
return filePath; return filePath;
} }
var firstChar = filePath[0];
if (firstChar == '/')
{
// for this we don't really know
return filePath;
}
var filePathSpan = filePath.AsSpan(); var filePathSpan = filePath.AsSpan();
// relative path // relative path on windows
if (firstChar == '\\') if (filePath[0] == '\\')
{ {
filePathSpan = filePathSpan.Slice(1); filePathSpan = filePathSpan.Slice(1);
} }

View File

@ -20,26 +20,37 @@ namespace Jellyfin.Server.Implementations.Tests.IO
_sut = _fixture.Create<ManagedFileSystem>(); _sut = _fixture.Create<ManagedFileSystem>();
} }
[Theory] [SkippableTheory]
[InlineData("/Volumes/Library/Sample/Music/Playlists/", "../Beethoven/Misc/Moonlight Sonata.mp3", "/Volumes/Library/Sample/Music/Beethoven/Misc/Moonlight Sonata.mp3")] [InlineData("/Volumes/Library/Sample/Music/Playlists/", "../Beethoven/Misc/Moonlight Sonata.mp3", "/Volumes/Library/Sample/Music/Beethoven/Misc/Moonlight Sonata.mp3")]
[InlineData("/Volumes/Library/Sample/Music/Playlists/", "../../Beethoven/Misc/Moonlight Sonata.mp3", "/Volumes/Library/Sample/Beethoven/Misc/Moonlight Sonata.mp3")] [InlineData("/Volumes/Library/Sample/Music/Playlists/", "../../Beethoven/Misc/Moonlight Sonata.mp3", "/Volumes/Library/Sample/Beethoven/Misc/Moonlight Sonata.mp3")]
[InlineData("/Volumes/Library/Sample/Music/Playlists/", "Beethoven/Misc/Moonlight Sonata.mp3", "/Volumes/Library/Sample/Music/Playlists/Beethoven/Misc/Moonlight Sonata.mp3")] [InlineData("/Volumes/Library/Sample/Music/Playlists/", "Beethoven/Misc/Moonlight Sonata.mp3", "/Volumes/Library/Sample/Music/Playlists/Beethoven/Misc/Moonlight Sonata.mp3")]
public void MakeAbsolutePathCorrectlyHandlesRelativeFilePaths( [InlineData("/Volumes/Library/Sample/Music/Playlists/", "/mnt/Beethoven/Misc/Moonlight Sonata.mp3", "/mnt/Beethoven/Misc/Moonlight Sonata.mp3")]
public void MakeAbsolutePathCorrectlyHandlesRelativeFilePathsOnUnixLike(
string folderPath, string folderPath,
string filePath, string filePath,
string expectedAbsolutePath) string expectedAbsolutePath)
{ {
Skip.If(OperatingSystem.IsWindows());
var generatedPath = _sut.MakeAbsolutePath(folderPath, filePath);
Assert.Equal(expectedAbsolutePath, generatedPath);
}
[SkippableTheory]
[InlineData(@"C:\\Volumes\Library\Sample\Music\Playlists\", @"..\Beethoven\Misc\Moonlight Sonata.mp3", @"C:\Volumes\Library\Sample\Music\Beethoven\Misc\Moonlight Sonata.mp3")]
[InlineData(@"C:\\Volumes\Library\Sample\Music\Playlists\", @"..\..\Beethoven\Misc\Moonlight Sonata.mp3", @"C:\Volumes\Library\Sample\Beethoven\Misc\Moonlight Sonata.mp3")]
[InlineData(@"C:\\Volumes\Library\Sample\Music\Playlists\", @"Beethoven\Misc\Moonlight Sonata.mp3", @"C:\Volumes\Library\Sample\Music\Playlists\Beethoven\Misc\Moonlight Sonata.mp3")]
[InlineData(@"C:\\Volumes\Library\Sample\Music\Playlists\", @"D:\\Beethoven\Misc\Moonlight Sonata.mp3", @"D:\\Beethoven\Misc\Moonlight Sonata.mp3")]
public void MakeAbsolutePathCorrectlyHandlesRelativeFilePathsOnWindows(
string folderPath,
string filePath,
string expectedAbsolutePath)
{
Skip.If(!OperatingSystem.IsWindows());
var generatedPath = _sut.MakeAbsolutePath(folderPath, filePath); var generatedPath = _sut.MakeAbsolutePath(folderPath, filePath);
if (OperatingSystem.IsWindows()) Assert.Equal(expectedAbsolutePath, generatedPath);
{
var expectedWindowsPath = expectedAbsolutePath.Replace('/', '\\');
Assert.Equal(expectedWindowsPath, generatedPath.Split(':')[1]);
}
else
{
Assert.Equal(expectedAbsolutePath, generatedPath);
}
} }
[Theory] [Theory]