Use IFileSystem

This commit is contained in:
Cody Robibero 2022-02-28 16:16:25 -07:00
parent 1d367712bb
commit fc5c6c0404
12 changed files with 111 additions and 50 deletions

View File

@ -704,6 +704,12 @@ namespace Emby.Server.Implementations.IO
return Directory.EnumerateFileSystemEntries(path, "*", GetEnumerationOptions(recursive));
}
/// <inheritdoc />
public virtual bool Exists(string path)
{
return Directory.Exists(path) || File.Exists(path);
}
private EnumerationOptions GetEnumerationOptions(bool recursive)
{
return new EnumerationOptions

View File

@ -887,7 +887,7 @@ namespace MediaBrowser.Controller.Entities
return Name;
}
public string GetInternalMetadataPath()
public virtual string GetInternalMetadataPath()
{
var basePath = ConfigurationManager.ApplicationPaths.InternalMetadataPath;

View File

@ -79,9 +79,5 @@ namespace MediaBrowser.Controller.Providers
return filePaths;
}
/// <inheritdoc />
public bool PathExists(string path)
=> Directory.Exists(path);
}
}

View File

@ -16,12 +16,5 @@ namespace MediaBrowser.Controller.Providers
IReadOnlyList<string> GetFilePaths(string path);
IReadOnlyList<string> GetFilePaths(string path, bool clearCache, bool sort = false);
/// <summary>
/// Does the path exist.
/// </summary>
/// <param name="path">The path.</param>
/// <returns>Whether the path exists.</returns>
bool PathExists(string path);
}
}

View File

@ -200,5 +200,12 @@ namespace MediaBrowser.Model.IO
void SetAttributes(string path, bool isHidden, bool readOnly);
IEnumerable<FileSystemMetadata> GetDrives();
/// <summary>
/// Determines whether the directory or file exists.
/// </summary>
/// <param name="path">The path.</param>
/// <returns>Whether the path exists.</returns>
bool Exists(string path);
}
}

View File

@ -3,6 +3,7 @@ using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.IO;
namespace MediaBrowser.Providers.MediaInfo
{
@ -16,13 +17,20 @@ namespace MediaBrowser.Providers.MediaInfo
/// </summary>
/// <param name="localizationManager">The localization manager.</param>
/// <param name="mediaEncoder">The media encoder.</param>
/// <param name="fileSystem">The file system.</param>
/// <param name="namingOptions">The <see cref="NamingOptions"/> object containing FileExtensions, MediaDefaultFlags, MediaForcedFlags and MediaFlagDelimiters.</param>
public AudioResolver(
ILocalizationManager localizationManager,
IMediaEncoder mediaEncoder,
IFileSystem fileSystem,
NamingOptions namingOptions)
: base(localizationManager, mediaEncoder, namingOptions, DlnaProfileType.Audio)
{
: base(
localizationManager,
mediaEncoder,
fileSystem,
namingOptions,
DlnaProfileType.Audio)
{
}
}
}

View File

@ -19,9 +19,9 @@ using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Controller.Subtitles;
using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.MediaInfo;
using Microsoft.Extensions.Logging;
@ -58,11 +58,12 @@ namespace MediaBrowser.Providers.MediaInfo
ISubtitleManager subtitleManager,
IChapterManager chapterManager,
ILibraryManager libraryManager,
IFileSystem fileSystem,
NamingOptions namingOptions)
{
_logger = logger;
_audioResolver = new AudioResolver(localization, mediaEncoder, namingOptions);
_subtitleResolver = new SubtitleResolver(localization, mediaEncoder, namingOptions);
_audioResolver = new AudioResolver(localization, mediaEncoder, fileSystem, namingOptions);
_subtitleResolver = new SubtitleResolver(localization, mediaEncoder, fileSystem, namingOptions);
_videoProber = new FFProbeVideoInfo(
_logger,
mediaSourceManager,

View File

@ -14,6 +14,7 @@ using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.MediaInfo;
namespace MediaBrowser.Providers.MediaInfo
@ -43,6 +44,8 @@ namespace MediaBrowser.Providers.MediaInfo
/// </summary>
private readonly IMediaEncoder _mediaEncoder;
private readonly IFileSystem _fileSystem;
/// <summary>
/// The <see cref="NamingOptions"/> instance.
/// </summary>
@ -58,15 +61,18 @@ namespace MediaBrowser.Providers.MediaInfo
/// </summary>
/// <param name="localizationManager">The localization manager.</param>
/// <param name="mediaEncoder">The media encoder.</param>
/// <param name="fileSystem">The file system.</param>
/// <param name="namingOptions">The <see cref="NamingOptions"/> object containing FileExtensions, MediaDefaultFlags, MediaForcedFlags and MediaFlagDelimiters.</param>
/// <param name="type">The <see cref="DlnaProfileType"/> of the parsed file.</param>
protected MediaInfoResolver(
ILocalizationManager localizationManager,
IMediaEncoder mediaEncoder,
IFileSystem fileSystem,
NamingOptions namingOptions,
DlnaProfileType type)
{
_mediaEncoder = mediaEncoder;
_fileSystem = fileSystem;
_namingOptions = namingOptions;
_type = type;
_externalPathParser = new ExternalPathParser(namingOptions, localizationManager, _type);
@ -148,7 +154,7 @@ namespace MediaBrowser.Providers.MediaInfo
// Check if video folder exists
string folder = video.ContainingFolderPath;
if (!directoryService.PathExists(folder))
if (!_fileSystem.Exists(folder))
{
return Array.Empty<ExternalPathParserResult>();
}
@ -157,7 +163,7 @@ namespace MediaBrowser.Providers.MediaInfo
var files = directoryService.GetFilePaths(folder, clearCache).ToList();
var internalMetadataPath = video.GetInternalMetadataPath();
if (directoryService.PathExists(internalMetadataPath))
if (_fileSystem.Exists(internalMetadataPath))
{
files.AddRange(directoryService.GetFilePaths(internalMetadataPath, clearCache));
}

View File

@ -3,6 +3,7 @@ using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.IO;
namespace MediaBrowser.Providers.MediaInfo
{
@ -16,13 +17,20 @@ namespace MediaBrowser.Providers.MediaInfo
/// </summary>
/// <param name="localizationManager">The localization manager.</param>
/// <param name="mediaEncoder">The media encoder.</param>
/// <param name="fileSystem">The file system.</param>
/// <param name="namingOptions">The <see cref="NamingOptions"/> object containing FileExtensions, MediaDefaultFlags, MediaForcedFlags and MediaFlagDelimiters.</param>
public SubtitleResolver(
ILocalizationManager localizationManager,
IMediaEncoder mediaEncoder,
IFileSystem fileSystem,
NamingOptions namingOptions)
: base(localizationManager, mediaEncoder, namingOptions, DlnaProfileType.Subtitle)
{
: base(
localizationManager,
mediaEncoder,
fileSystem,
namingOptions,
DlnaProfileType.Subtitle)
{
}
}
}

View File

@ -11,6 +11,7 @@ using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.IO;
using MediaBrowser.Providers.MediaInfo;
using Moq;
using Xunit;
@ -45,7 +46,13 @@ public class AudioResolverTests
}
}));
_audioResolver = new AudioResolver(localizationManager, mediaEncoder.Object, new NamingOptions());
var fileSystem = new Mock<IFileSystem>(MockBehavior.Strict);
fileSystem.Setup(fs => fs.Exists(It.IsRegex(MediaInfoResolverTests.VideoDirectoryRegex)))
.Returns(true);
fileSystem.Setup(fs => fs.Exists(It.IsRegex(MediaInfoResolverTests.MetadataDirectoryRegex)))
.Returns(true);
_audioResolver = new AudioResolver(localizationManager, mediaEncoder.Object, fileSystem.Object, new NamingOptions());
}
[Theory]

View File

@ -15,6 +15,7 @@ using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.MediaInfo;
using MediaBrowser.Providers.MediaInfo;
using Moq;
@ -25,9 +26,9 @@ namespace Jellyfin.Providers.Tests.MediaInfo;
public class MediaInfoResolverTests
{
public const string VideoDirectoryPath = "Test Data/Video";
private const string VideoDirectoryRegex = @"Test Data[/\\]Video";
private const string MetadataDirectoryPath = "library/00/00000000000000000000000000000000";
private const string MetadataDirectoryRegex = @"library.*";
public const string VideoDirectoryRegex = @"Test Data[/\\]Video";
public const string MetadataDirectoryPath = "library/00/00000000000000000000000000000000";
public const string MetadataDirectoryRegex = @"library.*";
private readonly ILocalizationManager _localizationManager;
private readonly MediaInfoResolver _subtitleResolver;
@ -61,7 +62,15 @@ public class MediaInfoResolverTests
}
}));
_subtitleResolver = new SubtitleResolver(_localizationManager, mediaEncoder.Object, new NamingOptions());
var fileSystem = new Mock<IFileSystem>(MockBehavior.Strict);
fileSystem.Setup(fs => fs.Exists(It.IsAny<string>()))
.Returns(false);
fileSystem.Setup(fs => fs.Exists(It.IsRegex(VideoDirectoryRegex)))
.Returns(true);
fileSystem.Setup(fs => fs.Exists(It.IsRegex(MetadataDirectoryRegex)))
.Returns(true);
_subtitleResolver = new SubtitleResolver(_localizationManager, mediaEncoder.Object, fileSystem.Object, new NamingOptions());
}
[Fact]
@ -88,24 +97,35 @@ public class MediaInfoResolverTests
{
BaseItem.MediaSourceManager = Mock.Of<IMediaSourceManager>();
var video = new Movie
string containingFolderPath, metadataPath;
if (metadataDirectory)
{
Path = VideoDirectoryPath + "/My.Video.mkv"
};
containingFolderPath = VideoDirectoryPath;
metadataPath = "invalid";
}
else
{
containingFolderPath = "invalid";
metadataPath = MetadataDirectoryPath;
}
var video = new Mock<Movie>();
video.Setup(m => m.Path)
.Returns(VideoDirectoryPath + "/My.Video.mkv");
video.Setup(m => m.ContainingFolderPath)
.Returns(containingFolderPath);
video.Setup(m => m.GetInternalMetadataPath())
.Returns(metadataPath);
string pathNotFoundRegex = metadataDirectory ? MetadataDirectoryRegex : VideoDirectoryRegex;
var directoryService = new Mock<IDirectoryService>(MockBehavior.Strict);
// any path other than test target exists and provides an empty listing
directoryService.Setup(ds => ds.PathExists(It.IsAny<string>()))
.Returns(true);
directoryService.Setup(ds => ds.GetFilePaths(It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>()))
.Returns(Array.Empty<string>());
directoryService.Setup(ds => ds.PathExists(It.IsRegex(pathNotFoundRegex)))
.Returns(false);
_subtitleResolver.GetExternalFiles(video, directoryService.Object, false);
_subtitleResolver.GetExternalFiles(video.Object, directoryService.Object, false);
directoryService.Verify(
ds => ds.GetFilePaths(It.IsRegex(pathNotFoundRegex), It.IsAny<bool>(), It.IsAny<bool>()),
@ -176,12 +196,11 @@ public class MediaInfoResolverTests
var directoryService = new Mock<IDirectoryService>(MockBehavior.Strict);
directoryService.Setup(ds => ds.GetFilePaths(It.IsAny<string>(), It.IsAny<bool>(), It.IsAny<bool>()))
.Returns(Array.Empty<string>());
directoryService.Setup(ds => ds.PathExists(It.IsAny<string>()))
.Returns(true);
var mediaEncoder = Mock.Of<IMediaEncoder>(MockBehavior.Strict);
var fileSystem = Mock.Of<IFileSystem>();
var subtitleResolver = new SubtitleResolver(_localizationManager, mediaEncoder, new NamingOptions());
var subtitleResolver = new SubtitleResolver(_localizationManager, mediaEncoder, fileSystem, new NamingOptions());
var streams = await subtitleResolver.GetExternalStreamsAsync(video, 0, directoryService.Object, false, CancellationToken.None);
@ -280,7 +299,13 @@ public class MediaInfoResolverTests
MediaStreams = inputStreams.ToList()
}));
var subtitleResolver = new SubtitleResolver(_localizationManager, mediaEncoder.Object, new NamingOptions());
var fileSystem = new Mock<IFileSystem>(MockBehavior.Strict);
fileSystem.Setup(fs => fs.Exists(It.IsRegex(VideoDirectoryRegex)))
.Returns(true);
fileSystem.Setup(fs => fs.Exists(It.IsRegex(MetadataDirectoryRegex)))
.Returns(true);
var subtitleResolver = new SubtitleResolver(_localizationManager, mediaEncoder.Object, fileSystem.Object, new NamingOptions());
var directoryService = GetDirectoryServiceForExternalFile(file);
var streams = await subtitleResolver.GetExternalStreamsAsync(video, 0, directoryService, false, CancellationToken.None);
@ -327,10 +352,6 @@ public class MediaInfoResolverTests
.Returns(files);
directoryService.Setup(ds => ds.GetFilePaths(It.IsRegex(MetadataDirectoryRegex), It.IsAny<bool>(), It.IsAny<bool>()))
.Returns(Array.Empty<string>());
directoryService.Setup(ds => ds.PathExists(It.IsRegex(MetadataDirectoryRegex)))
.Returns(true);
directoryService.Setup(ds => ds.PathExists(It.IsRegex(VideoDirectoryRegex)))
.Returns(true);
List<MediaStream> GenerateMediaStreams()
{
@ -350,7 +371,13 @@ public class MediaInfoResolverTests
MediaStreams = GenerateMediaStreams()
}));
var subtitleResolver = new SubtitleResolver(_localizationManager, mediaEncoder.Object, new NamingOptions());
var fileSystem = new Mock<IFileSystem>(MockBehavior.Strict);
fileSystem.Setup(fs => fs.Exists(It.IsRegex(VideoDirectoryRegex)))
.Returns(true);
fileSystem.Setup(fs => fs.Exists(It.IsRegex(MetadataDirectoryRegex)))
.Returns(true);
var subtitleResolver = new SubtitleResolver(_localizationManager, mediaEncoder.Object, fileSystem.Object, new NamingOptions());
int startIndex = 1;
var streams = await subtitleResolver.GetExternalStreamsAsync(video, startIndex, directoryService.Object, false, CancellationToken.None);
@ -402,11 +429,6 @@ public class MediaInfoResolverTests
.Returns(Array.Empty<string>());
}
directoryService.Setup(ds => ds.PathExists(It.IsRegex(MetadataDirectoryRegex)))
.Returns(true);
directoryService.Setup(ds => ds.PathExists(It.IsRegex(VideoDirectoryRegex)))
.Returns(true);
return directoryService.Object;
}
}

View File

@ -11,6 +11,7 @@ using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.IO;
using MediaBrowser.Providers.MediaInfo;
using Moq;
using Xunit;
@ -45,7 +46,13 @@ public class SubtitleResolverTests
}
}));
_subtitleResolver = new SubtitleResolver(localizationManager, mediaEncoder.Object, new NamingOptions());
var fileSystem = new Mock<IFileSystem>(MockBehavior.Strict);
fileSystem.Setup(fs => fs.Exists(It.IsRegex(MediaInfoResolverTests.VideoDirectoryRegex)))
.Returns(true);
fileSystem.Setup(fs => fs.Exists(It.IsRegex(MediaInfoResolverTests.MetadataDirectoryRegex)))
.Returns(true);
_subtitleResolver = new SubtitleResolver(localizationManager, mediaEncoder.Object, fileSystem.Object, new NamingOptions());
}
[Theory]