jellyfin/MediaBrowser.Controller/Providers/DirectoryService.cs

65 lines
2.0 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
{
List<FileSystemInfo> GetFileSystemEntries(string path);
IEnumerable<FileSystemInfo> GetFiles(string path);
FileSystemInfo GetFile(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-02-15 16:36:09 +00:00
private readonly ConcurrentDictionary<string, List<FileSystemInfo>> _cache = new ConcurrentDictionary<string, List<FileSystemInfo>>(StringComparer.OrdinalIgnoreCase);
2014-02-08 22:38:02 +00:00
public DirectoryService(ILogger logger)
{
_logger = logger;
}
public List<FileSystemInfo> GetFileSystemEntries(string path)
{
List<FileSystemInfo> entries;
if (!_cache.TryGetValue(path, out entries))
{
//_logger.Debug("Getting files for " + path);
2014-02-11 21:41:01 +00:00
try
{
entries = new DirectoryInfo(path).EnumerateFileSystemInfos("*", SearchOption.TopDirectoryOnly).ToList();
}
catch (DirectoryNotFoundException)
{
entries = new List<FileSystemInfo>();
}
2014-02-15 16:36:09 +00:00
_cache.TryAdd(path, entries);
2014-02-08 22:38:02 +00:00
}
return entries;
}
public IEnumerable<FileSystemInfo> GetFiles(string path)
2014-02-08 22:38:02 +00:00
{
return GetFileSystemEntries(path).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);
var filename = Path.GetFileName(path);
return GetFiles(directory).FirstOrDefault(i => string.Equals(i.Name, filename, StringComparison.OrdinalIgnoreCase));
}
}
}