diff --git a/Emby.Server.Implementations/Library/ResolverHelper.cs b/Emby.Server.Implementations/Library/ResolverHelper.cs index 52be76217..c9e3a4daf 100644 --- a/Emby.Server.Implementations/Library/ResolverHelper.cs +++ b/Emby.Server.Implementations/Library/ResolverHelper.cs @@ -39,7 +39,7 @@ namespace Emby.Server.Implementations.Library item.GetParents().Any(i => i.IsLocked); // Make sure DateCreated and DateModified have values - var fileInfo = directoryService.GetFile(item.Path); + var fileInfo = directoryService.GetFileSystemEntry(item.Path); if (fileInfo is null) { return false; diff --git a/MediaBrowser.Controller/Providers/DirectoryService.cs b/MediaBrowser.Controller/Providers/DirectoryService.cs index 56b07ebae..474f09dc5 100644 --- a/MediaBrowser.Controller/Providers/DirectoryService.cs +++ b/MediaBrowser.Controller/Providers/DirectoryService.cs @@ -61,11 +61,23 @@ namespace MediaBrowser.Controller.Providers } public FileSystemMetadata? GetFile(string path) + { + var entry = GetFileSystemEntry(path); + return entry is not null && !entry.IsDirectory ? entry : null; + } + + public FileSystemMetadata? GetDirectory(string path) + { + var entry = GetFileSystemEntry(path); + return entry is not null && entry.IsDirectory ? entry : null; + } + + public FileSystemMetadata? GetFileSystemEntry(string path) { if (!_fileCache.TryGetValue(path, out var result)) { - var file = _fileSystem.GetFileInfo(path); - if (file.Exists) + var file = _fileSystem.GetFileSystemInfo(path); + if (file?.Exists ?? false) { result = file; _fileCache.TryAdd(path, result); diff --git a/MediaBrowser.Controller/Providers/IDirectoryService.cs b/MediaBrowser.Controller/Providers/IDirectoryService.cs index a3c06cde5..1babf73af 100644 --- a/MediaBrowser.Controller/Providers/IDirectoryService.cs +++ b/MediaBrowser.Controller/Providers/IDirectoryService.cs @@ -15,6 +15,10 @@ namespace MediaBrowser.Controller.Providers FileSystemMetadata? GetFile(string path); + FileSystemMetadata? GetDirectory(string path); + + FileSystemMetadata? GetFileSystemEntry(string path); + IReadOnlyList GetFilePaths(string path); IReadOnlyList GetFilePaths(string path, bool clearCache, bool sort = false); diff --git a/tests/Jellyfin.Controller.Tests/DirectoryServiceTests.cs b/tests/Jellyfin.Controller.Tests/DirectoryServiceTests.cs index 46439aecb..83a023384 100644 --- a/tests/Jellyfin.Controller.Tests/DirectoryServiceTests.cs +++ b/tests/Jellyfin.Controller.Tests/DirectoryServiceTests.cs @@ -79,6 +79,21 @@ namespace Jellyfin.Controller.Tests Assert.Equal(_lowerCaseFileSystemMetadata.Where(f => !f.IsDirectory), lowerCaseResult); } + [Fact] + public void GetDirectories_GivenPathsWithDifferentCasing_ReturnsCorrectDirectories() + { + var fileSystemMock = new Mock(); + fileSystemMock.Setup(f => f.GetFileSystemEntries(It.Is(x => x == UpperCasePath), false)).Returns(_upperCaseFileSystemMetadata); + fileSystemMock.Setup(f => f.GetFileSystemEntries(It.Is(x => x == LowerCasePath), false)).Returns(_lowerCaseFileSystemMetadata); + var directoryService = new DirectoryService(fileSystemMock.Object); + + var upperCaseResult = directoryService.GetDirectories(UpperCasePath); + var lowerCaseResult = directoryService.GetDirectories(LowerCasePath); + + Assert.Equal(_upperCaseFileSystemMetadata.Where(f => f.IsDirectory), upperCaseResult); + Assert.Equal(_lowerCaseFileSystemMetadata.Where(f => f.IsDirectory), lowerCaseResult); + } + [Fact] public void GetFile_GivenFilePathsWithDifferentCasing_ReturnsCorrectFile() { @@ -95,15 +110,52 @@ namespace Jellyfin.Controller.Tests Exists = false }; var fileSystemMock = new Mock(); - fileSystemMock.Setup(f => f.GetFileInfo(It.Is(x => x == upperCasePath))).Returns(upperCaseFileSystemMetadata); - fileSystemMock.Setup(f => f.GetFileInfo(It.Is(x => x == lowerCasePath))).Returns(lowerCaseFileSystemMetadata); + fileSystemMock.Setup(f => f.GetFileSystemInfo(It.Is(x => x == upperCasePath))).Returns(upperCaseFileSystemMetadata); + fileSystemMock.Setup(f => f.GetFileSystemInfo(It.Is(x => x == lowerCasePath))).Returns(lowerCaseFileSystemMetadata); var directoryService = new DirectoryService(fileSystemMock.Object); - var lowerCaseResult = directoryService.GetFile(lowerCasePath); - var upperCaseResult = directoryService.GetFile(upperCasePath); + var lowerCaseDirResult = directoryService.GetDirectory(lowerCasePath); + var lowerCaseFileResult = directoryService.GetFile(lowerCasePath); + var upperCaseDirResult = directoryService.GetDirectory(upperCasePath); + var upperCaseFileResult = directoryService.GetFile(upperCasePath); - Assert.Equal(lowerCaseFileSystemMetadata, lowerCaseResult); - Assert.Null(upperCaseResult); + Assert.Null(lowerCaseDirResult); + Assert.Equal(lowerCaseFileSystemMetadata, lowerCaseFileResult); + Assert.Null(upperCaseDirResult); + Assert.Null(upperCaseFileResult); + } + + [Fact] + public void GetDirectory_GivenFilePathsWithDifferentCasing_ReturnsCorrectDirectory() + { + const string lowerCasePath = "/music/someartist/Lyrics"; + var lowerCaseFileSystemMetadata = new FileSystemMetadata + { + FullName = lowerCasePath, + IsDirectory = true, + Exists = true + }; + const string upperCasePath = "/music/SOMEARTIST/LYRICS"; + var upperCaseFileSystemMetadata = new FileSystemMetadata + { + FullName = upperCasePath, + IsDirectory = true, + Exists = false + }; + var fileSystemMock = new Mock(); + fileSystemMock.Setup(f => f.GetFileSystemInfo(It.Is(x => x == upperCasePath))).Returns(upperCaseFileSystemMetadata); + fileSystemMock.Setup(f => f.GetFileSystemInfo(It.Is(x => x == lowerCasePath))).Returns(lowerCaseFileSystemMetadata); + var directoryService = new DirectoryService(fileSystemMock.Object); + + var lowerCaseDirResult = directoryService.GetDirectory(lowerCasePath); + var lowerCaseFileResult = directoryService.GetFile(lowerCasePath); + var upperCaseDirResult = directoryService.GetDirectory(upperCasePath); + var upperCaseFileResult = directoryService.GetFile(upperCasePath); + + Assert.Equal(lowerCaseFileSystemMetadata, lowerCaseDirResult); + Assert.Null(lowerCaseFileResult); + Assert.Null(upperCaseDirResult); + Assert.Null(upperCaseFileResult); } [Fact] @@ -122,11 +174,11 @@ namespace Jellyfin.Controller.Tests }; var fileSystemMock = new Mock(); - fileSystemMock.Setup(f => f.GetFileInfo(It.Is(x => x == path))).Returns(cachedFileSystemMetadata); + fileSystemMock.Setup(f => f.GetFileSystemInfo(It.Is(x => x == path))).Returns(cachedFileSystemMetadata); var directoryService = new DirectoryService(fileSystemMock.Object); var result = directoryService.GetFile(path); - fileSystemMock.Setup(f => f.GetFileInfo(It.Is(x => x == path))).Returns(newFileSystemMetadata); + fileSystemMock.Setup(f => f.GetFileSystemInfo(It.Is(x => x == path))).Returns(newFileSystemMetadata); var secondResult = directoryService.GetFile(path); Assert.Equal(cachedFileSystemMetadata, result);