2020-08-22 15:56:24 -04:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 15:01:16 -05:00
|
|
|
using System;
|
2018-12-27 18:27:57 -05:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.Providers
|
|
|
|
{
|
|
|
|
public class DirectoryService : IDirectoryService
|
|
|
|
{
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
|
|
|
|
private readonly Dictionary<string, FileSystemMetadata[]> _cache = new Dictionary<string, FileSystemMetadata[]>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
private readonly Dictionary<string, FileSystemMetadata> _fileCache = new Dictionary<string, FileSystemMetadata>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
private readonly Dictionary<string, List<string>> _filePathCache = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
2019-09-10 16:37:53 -04:00
|
|
|
public DirectoryService(IFileSystem fileSystem)
|
2018-12-27 18:27:57 -05:00
|
|
|
{
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
}
|
|
|
|
|
|
|
|
public FileSystemMetadata[] GetFileSystemEntries(string path)
|
|
|
|
{
|
2019-01-17 14:24:39 -05:00
|
|
|
if (!_cache.TryGetValue(path, out FileSystemMetadata[] entries))
|
2018-12-27 18:27:57 -05:00
|
|
|
{
|
|
|
|
entries = _fileSystem.GetFileSystemEntries(path).ToArray();
|
|
|
|
|
|
|
|
_cache[path] = entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
return entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<FileSystemMetadata> GetFiles(string path)
|
|
|
|
{
|
|
|
|
var list = new List<FileSystemMetadata>();
|
|
|
|
var items = GetFileSystemEntries(path);
|
|
|
|
foreach (var item in items)
|
|
|
|
{
|
|
|
|
if (!item.IsDirectory)
|
|
|
|
{
|
|
|
|
list.Add(item);
|
|
|
|
}
|
|
|
|
}
|
2019-09-10 16:37:53 -04:00
|
|
|
|
2018-12-27 18:27:57 -05:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
public FileSystemMetadata GetFile(string path)
|
|
|
|
{
|
2019-01-17 14:24:39 -05:00
|
|
|
if (!_fileCache.TryGetValue(path, out FileSystemMetadata file))
|
2018-12-27 18:27:57 -05:00
|
|
|
{
|
|
|
|
file = _fileSystem.GetFileInfo(path);
|
|
|
|
|
|
|
|
if (file != null && file.Exists)
|
|
|
|
{
|
|
|
|
_fileCache[path] = file;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2020-02-23 04:53:51 -05:00
|
|
|
public IReadOnlyList<string> GetFilePaths(string path)
|
|
|
|
=> GetFilePaths(path, false);
|
2018-12-27 18:27:57 -05:00
|
|
|
|
2020-02-23 04:53:51 -05:00
|
|
|
public IReadOnlyList<string> GetFilePaths(string path, bool clearCache)
|
2018-12-27 18:27:57 -05:00
|
|
|
{
|
2019-01-17 14:24:39 -05:00
|
|
|
if (clearCache || !_filePathCache.TryGetValue(path, out List<string> result))
|
2018-12-27 18:27:57 -05:00
|
|
|
{
|
|
|
|
result = _fileSystem.GetFilePaths(path).ToList();
|
|
|
|
|
|
|
|
_filePathCache[path] = result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|