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

83 lines
2.4 KiB

#pragma warning disable CS1591
using System;
using System.Collections.Concurrent;
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;
3 years ago
private readonly ConcurrentDictionary<string, FileSystemMetadata[]> _cache = new(StringComparer.Ordinal);
6 years ago
3 years ago
private readonly ConcurrentDictionary<string, FileSystemMetadata> _fileCache = new(StringComparer.Ordinal);
6 years ago
3 years ago
private readonly ConcurrentDictionary<string, List<string>> _filePathCache = new(StringComparer.Ordinal);
6 years ago
public DirectoryService(IFileSystem fileSystem)
6 years ago
{
_fileSystem = fileSystem;
}
public FileSystemMetadata[] GetFileSystemEntries(string path)
{
return _cache.GetOrAdd(path, static (p, fileSystem) => fileSystem.GetFileSystemEntries(p).ToArray(), _fileSystem);
6 years ago
}
public List<FileSystemMetadata> GetFiles(string path)
{
var list = new List<FileSystemMetadata>();
var items = GetFileSystemEntries(path);
for (var i = 0; i < items.Length; i++)
6 years ago
{
var item = items[i];
6 years ago
if (!item.IsDirectory)
{
list.Add(item);
}
}
6 years ago
return list;
}
4 years ago
public FileSystemMetadata? GetFile(string path)
6 years ago
{
4 years ago
if (!_fileCache.TryGetValue(path, out var result))
6 years ago
{
4 years ago
var file = _fileSystem.GetFileInfo(path);
if (file.Exists)
4 years ago
{
result = file;
4 years ago
_fileCache.TryAdd(path, result);
}
6 years ago
}
return result;
6 years ago
}
public IReadOnlyList<string> GetFilePaths(string path)
3 years ago
=> GetFilePaths(path, false);
public IReadOnlyList<string> GetFilePaths(string path, bool clearCache, bool sort = false)
6 years ago
{
if (clearCache)
6 years ago
{
_filePathCache.TryRemove(path, out _);
6 years ago
}
var filePaths = _filePathCache.GetOrAdd(path, static (p, fileSystem) => fileSystem.GetFilePaths(p).ToList(), _fileSystem);
if (sort)
{
filePaths.Sort();
}
return filePaths;
6 years ago
}
}
}