jellyfin-server/MediaBrowser.Controller/Providers/DirectoryService.cs

114 lines
3.5 KiB
C#
Raw Normal View History

using MediaBrowser.Model.Logging;
2014-02-11 21:41:01 +00:00
using System;
using System.Collections.Concurrent;
2014-02-08 22:38:02 +00:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace MediaBrowser.Controller.Providers
{
2014-02-10 18:39:41 +00:00
public interface IDirectoryService
{
2014-10-25 18:32:58 +00:00
IEnumerable<FileSystemInfo> GetFileSystemEntries(string path);
IEnumerable<FileSystemInfo> GetFiles(string path);
2014-05-08 05:04:39 +00:00
IEnumerable<FileSystemInfo> GetFiles(string path, bool clearCache);
FileSystemInfo GetFile(string path);
2014-10-25 18:32:58 +00:00
Dictionary<string, FileSystemInfo> GetFileSystemDictionary(string path);
2014-02-10 18:39:41 +00:00
}
public class DirectoryService : IDirectoryService
2014-02-08 22:38:02 +00:00
{
private readonly ILogger _logger;
2014-10-25 18:32:58 +00:00
private readonly ConcurrentDictionary<string, Dictionary<string,FileSystemInfo>> _cache =
new ConcurrentDictionary<string, Dictionary<string, FileSystemInfo>>(StringComparer.OrdinalIgnoreCase);
2014-02-08 22:38:02 +00:00
public DirectoryService(ILogger logger)
{
_logger = logger;
}
2014-10-25 18:32:58 +00:00
public DirectoryService()
: this(new NullLogger())
{
}
public IEnumerable<FileSystemInfo> GetFileSystemEntries(string path)
2014-05-08 05:04:39 +00:00
{
return GetFileSystemEntries(path, false);
}
2014-10-25 18:32:58 +00:00
public Dictionary<string, FileSystemInfo> GetFileSystemDictionary(string path)
{
return GetFileSystemDictionary(path, false);
}
private Dictionary<string, FileSystemInfo> GetFileSystemDictionary(string path, bool clearCache)
2014-02-08 22:38:02 +00:00
{
2014-10-25 18:32:58 +00:00
Dictionary<string, FileSystemInfo> entries;
2014-02-08 22:38:02 +00:00
2014-05-08 05:04:39 +00:00
if (clearCache)
{
2014-10-25 18:32:58 +00:00
Dictionary<string, FileSystemInfo> removed;
2014-05-08 05:04:39 +00:00
_cache.TryRemove(path, out removed);
}
2014-02-08 22:38:02 +00:00
if (!_cache.TryGetValue(path, out entries))
{
//_logger.Debug("Getting files for " + path);
2014-10-25 18:32:58 +00:00
entries = new Dictionary<string, FileSystemInfo>(StringComparer.OrdinalIgnoreCase);
2014-02-11 21:41:01 +00:00
try
{
2014-10-25 18:32:58 +00:00
var list = new DirectoryInfo(path)
.EnumerateFileSystemInfos("*", SearchOption.TopDirectoryOnly);
// Seeing dupes on some users file system for some reason
foreach (var item in list)
{
entries[item.FullName] = item;
}
2014-02-11 21:41:01 +00:00
}
catch (DirectoryNotFoundException)
{
}
2014-10-25 18:32:58 +00:00
//var group = entries.ToLookup(i => Path.GetDirectoryName(i.FullName)).ToList();
2014-02-15 16:36:09 +00:00
_cache.TryAdd(path, entries);
2014-02-08 22:38:02 +00:00
}
return entries;
}
2014-10-25 18:32:58 +00:00
private IEnumerable<FileSystemInfo> GetFileSystemEntries(string path, bool clearCache)
{
return GetFileSystemDictionary(path, clearCache).Values;
}
public IEnumerable<FileSystemInfo> GetFiles(string path)
2014-02-08 22:38:02 +00:00
{
2014-05-08 05:04:39 +00:00
return GetFiles(path, false);
}
public IEnumerable<FileSystemInfo> GetFiles(string path, bool clearCache)
{
return GetFileSystemEntries(path, clearCache).Where(i => (i.Attributes & FileAttributes.Directory) != FileAttributes.Directory);
2014-02-08 22:38:02 +00:00
}
public FileSystemInfo GetFile(string path)
2014-02-08 22:38:02 +00:00
{
var directory = Path.GetDirectoryName(path);
2014-10-25 18:32:58 +00:00
var dict = GetFileSystemDictionary(directory, false);
FileSystemInfo entry;
dict.TryGetValue(path, out entry);
return entry;
2014-02-08 22:38:02 +00:00
}
}
}