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

119 lines
3.6 KiB

using MediaBrowser.Model.Logging;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using CommonIO;
namespace MediaBrowser.Controller.Providers
{
public class DirectoryService : IDirectoryService
{
private readonly ILogger _logger;
private readonly IFileSystem _fileSystem;
private readonly ConcurrentDictionary<string, Dictionary<string, FileSystemMetadata>> _cache =
new ConcurrentDictionary<string, Dictionary<string, FileSystemMetadata>>(StringComparer.OrdinalIgnoreCase);
public DirectoryService(ILogger logger, IFileSystem fileSystem)
{
_logger = logger;
_fileSystem = fileSystem;
}
public DirectoryService(IFileSystem fileSystem)
: this(new NullLogger(), fileSystem)
10 years ago
{
}
public IEnumerable<FileSystemMetadata> GetFileSystemEntries(string path)
{
return GetFileSystemEntries(path, false);
}
public Dictionary<string, FileSystemMetadata> GetFileSystemDictionary(string path)
10 years ago
{
return GetFileSystemDictionary(path, false);
}
private Dictionary<string, FileSystemMetadata> GetFileSystemDictionary(string path, bool clearCache)
{
10 years ago
if (string.IsNullOrWhiteSpace(path))
{
throw new ArgumentNullException("path");
}
Dictionary<string, FileSystemMetadata> entries;
if (clearCache)
{
Dictionary<string, FileSystemMetadata> removed;
_cache.TryRemove(path, out removed);
}
if (!_cache.TryGetValue(path, out entries))
{
//_logger.Debug("Getting files for " + path);
entries = new Dictionary<string, FileSystemMetadata>(StringComparer.OrdinalIgnoreCase);
10 years ago
try
{
// using EnumerateFileSystemInfos doesn't handle reparse points (symlinks)
var list = _fileSystem.GetFileSystemEntries(path)
.ToList();
10 years ago
// Seeing dupes on some users file system for some reason
foreach (var item in list)
{
entries[item.FullName] = item;
}
}
catch (DirectoryNotFoundException)
{
}
10 years ago
//var group = entries.ToLookup(i => Path.GetDirectoryName(i.FullName)).ToList();
_cache.TryAdd(path, entries);
}
return entries;
}
private IEnumerable<FileSystemMetadata> GetFileSystemEntries(string path, bool clearCache)
10 years ago
{
return GetFileSystemDictionary(path, clearCache).Values;
}
public IEnumerable<FileSystemMetadata> GetFiles(string path)
{
return GetFiles(path, false);
}
public IEnumerable<FileSystemMetadata> GetFiles(string path, bool clearCache)
{
return GetFileSystemEntries(path, clearCache).Where(i => !i.IsDirectory);
}
public FileSystemMetadata GetFile(string path)
{
var directory = Path.GetDirectoryName(path);
10 years ago
var dict = GetFileSystemDictionary(directory, false);
FileSystemMetadata entry;
10 years ago
dict.TryGetValue(path, out entry);
return entry;
}
public IEnumerable<FileSystemMetadata> GetDirectories(string path)
{
return GetFileSystemEntries(path, false).Where(i => i.IsDirectory);
}
}
}