jellyfin/MediaBrowser.Controller/Providers/DirectoryService.cs

95 lines
2.8 KiB
C#
Raw Normal View History

using System;
2018-12-27 16:27:57 -07:00
using System.Collections.Generic;
using System.Linq;
using MediaBrowser.Model.IO;
using Microsoft.Extensions.Logging;
2018-12-27 16:27:57 -07:00
namespace MediaBrowser.Controller.Providers
{
public class DirectoryService : IDirectoryService
{
private readonly ILogger _logger;
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);
public DirectoryService(ILogger logger, IFileSystem fileSystem)
{
_logger = logger;
_fileSystem = fileSystem;
}
public FileSystemMetadata[] GetFileSystemEntries(string path)
{
if (!_cache.TryGetValue(path, out var entries))
2018-12-27 16:27:57 -07:00
{
//_logger.LogDebug("Getting files for " + path);
2018-12-27 16:27:57 -07:00
entries = _fileSystem.GetFileSystemEntries(path).ToArray();
//_cache.TryAdd(path, entries);
_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);
}
}
return list;
}
public FileSystemMetadata GetFile(string path)
{
if (!_fileCache.TryGetValue(path, out var file))
2018-12-27 16:27:57 -07:00
{
file = _fileSystem.GetFileInfo(path);
if (file != null && file.Exists)
{
//_fileCache.TryAdd(path, file);
_fileCache[path] = file;
}
else
{
return null;
}
}
return file;
//return _fileSystem.GetFileInfo(path);
}
public List<string> GetFilePaths(string path)
{
return GetFilePaths(path, false);
}
public List<string> GetFilePaths(string path, bool clearCache)
{
if (clearCache || !_filePathCache.TryGetValue(path, out var result))
2018-12-27 16:27:57 -07:00
{
result = _fileSystem.GetFilePaths(path).ToList();
_filePathCache[path] = result;
}
return result;
}
}
}