2014-02-15 09:36:09 -07:00
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
2014-02-11 14:41:01 -07:00
|
|
|
|
using System;
|
2014-02-08 15:38:02 -07:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.Providers
|
|
|
|
|
{
|
2014-02-10 11:39:41 -07:00
|
|
|
|
public interface IDirectoryService
|
|
|
|
|
{
|
|
|
|
|
List<FileSystemInfo> GetFileSystemEntries(string path);
|
|
|
|
|
IEnumerable<FileInfo> GetFiles(string path);
|
|
|
|
|
IEnumerable<DirectoryInfo> GetDirectories(string path);
|
|
|
|
|
FileInfo GetFile(string path);
|
|
|
|
|
DirectoryInfo GetDirectory(string path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class DirectoryService : IDirectoryService
|
2014-02-08 15:38:02 -07:00
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
2014-02-15 09:36:09 -07:00
|
|
|
|
private readonly ConcurrentDictionary<string, List<FileSystemInfo>> _cache = new ConcurrentDictionary<string, List<FileSystemInfo>>(StringComparer.OrdinalIgnoreCase);
|
2014-02-08 15:38:02 -07: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 14:41:01 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
entries = new DirectoryInfo(path).EnumerateFileSystemInfos("*", SearchOption.TopDirectoryOnly).ToList();
|
|
|
|
|
}
|
|
|
|
|
catch (DirectoryNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
entries = new List<FileSystemInfo>();
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-15 09:36:09 -07:00
|
|
|
|
_cache.TryAdd(path, entries);
|
2014-02-08 15:38:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return entries;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<FileInfo> GetFiles(string path)
|
|
|
|
|
{
|
|
|
|
|
return GetFileSystemEntries(path).OfType<FileInfo>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<DirectoryInfo> GetDirectories(string path)
|
|
|
|
|
{
|
|
|
|
|
return GetFileSystemEntries(path).OfType<DirectoryInfo>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public FileInfo GetFile(string path)
|
|
|
|
|
{
|
|
|
|
|
var directory = Path.GetDirectoryName(path);
|
|
|
|
|
var filename = Path.GetFileName(path);
|
|
|
|
|
|
|
|
|
|
return GetFiles(directory).FirstOrDefault(i => string.Equals(i.Name, filename, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public DirectoryInfo GetDirectory(string path)
|
|
|
|
|
{
|
|
|
|
|
var directory = Path.GetDirectoryName(path);
|
|
|
|
|
var name = Path.GetFileName(path);
|
|
|
|
|
|
|
|
|
|
return GetDirectories(directory).FirstOrDefault(i => string.Equals(i.Name, name, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|