You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/MediaBrowser.Controller/Providers/DirectoryService.cs

85 lines
2.5 KiB

using System;
6 years ago
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);
public DirectoryService(IFileSystem fileSystem)
6 years ago
{
_fileSystem = fileSystem;
}
public FileSystemMetadata[] GetFileSystemEntries(string path)
{
if (!_cache.TryGetValue(path, out FileSystemMetadata[] entries))
6 years ago
{
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);
}
}
6 years ago
return list;
}
public FileSystemMetadata GetFile(string path)
{
if (!_fileCache.TryGetValue(path, out FileSystemMetadata file))
6 years ago
{
file = _fileSystem.GetFileInfo(path);
if (file != null && file.Exists)
{
_fileCache[path] = file;
}
else
{
return null;
}
}
return file;
}
public IReadOnlyList<string> GetFilePaths(string path)
=> GetFilePaths(path, false);
6 years ago
public IReadOnlyList<string> GetFilePaths(string path, bool clearCache)
6 years ago
{
if (clearCache || !_filePathCache.TryGetValue(path, out List<string> result))
6 years ago
{
result = _fileSystem.GetFilePaths(path).ToList();
_filePathCache[path] = result;
}
return result;
}
}
}