2014-03-12 12:56:12 -07:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2014-02-11 14:41:01 -07:00
|
|
|
|
using System;
|
2014-03-12 12:56:12 -07:00
|
|
|
|
using System.Collections.Concurrent;
|
2014-02-08 15:38:02 -07:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2017-05-25 23:48:54 -07:00
|
|
|
|
|
2016-10-25 12:02:04 -07:00
|
|
|
|
using MediaBrowser.Controller.IO;
|
|
|
|
|
using MediaBrowser.Model.IO;
|
2014-02-08 15:38:02 -07:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.Providers
|
|
|
|
|
{
|
2014-02-10 11:39:41 -07:00
|
|
|
|
public class DirectoryService : IDirectoryService
|
2014-02-08 15:38:02 -07:00
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
2015-09-13 14:32:02 -07:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2014-02-08 15:38:02 -07:00
|
|
|
|
|
2015-10-03 20:38:46 -07:00
|
|
|
|
private readonly ConcurrentDictionary<string, Dictionary<string, FileSystemMetadata>> _cache =
|
|
|
|
|
new ConcurrentDictionary<string, Dictionary<string, FileSystemMetadata>>(StringComparer.OrdinalIgnoreCase);
|
2014-02-08 15:38:02 -07:00
|
|
|
|
|
2016-08-06 14:10:18 -07:00
|
|
|
|
private readonly ConcurrentDictionary<string, FileSystemMetadata> _fileCache =
|
|
|
|
|
new ConcurrentDictionary<string, FileSystemMetadata>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
|
|
public DirectoryService(ILogger logger, IFileSystem fileSystem)
|
2014-02-08 15:38:02 -07:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
2015-09-13 14:32:02 -07:00
|
|
|
|
_fileSystem = fileSystem;
|
2014-02-08 15:38:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-05 21:48:00 -07:00
|
|
|
|
public DirectoryService(IFileSystem fileSystem)
|
2015-09-13 14:32:02 -07:00
|
|
|
|
: this(new NullLogger(), fileSystem)
|
2014-10-25 11:32:58 -07:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-03 20:38:46 -07:00
|
|
|
|
public IEnumerable<FileSystemMetadata> GetFileSystemEntries(string path)
|
2014-05-07 22:04:39 -07:00
|
|
|
|
{
|
|
|
|
|
return GetFileSystemEntries(path, false);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-03 20:38:46 -07:00
|
|
|
|
public Dictionary<string, FileSystemMetadata> GetFileSystemDictionary(string path)
|
2014-10-25 11:32:58 -07:00
|
|
|
|
{
|
|
|
|
|
return GetFileSystemDictionary(path, false);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-03 20:38:46 -07:00
|
|
|
|
private Dictionary<string, FileSystemMetadata> GetFileSystemDictionary(string path, bool clearCache)
|
2014-02-08 15:38:02 -07:00
|
|
|
|
{
|
2014-12-28 10:59:40 -07:00
|
|
|
|
if (string.IsNullOrWhiteSpace(path))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("path");
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-03 20:38:46 -07:00
|
|
|
|
Dictionary<string, FileSystemMetadata> entries;
|
2014-02-08 15:38:02 -07:00
|
|
|
|
|
2014-05-07 22:04:39 -07:00
|
|
|
|
if (clearCache)
|
|
|
|
|
{
|
2015-10-03 20:38:46 -07:00
|
|
|
|
Dictionary<string, FileSystemMetadata> removed;
|
2014-05-07 22:04:39 -07:00
|
|
|
|
|
|
|
|
|
_cache.TryRemove(path, out removed);
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-08 15:38:02 -07:00
|
|
|
|
if (!_cache.TryGetValue(path, out entries))
|
|
|
|
|
{
|
|
|
|
|
//_logger.Debug("Getting files for " + path);
|
|
|
|
|
|
2015-10-03 20:38:46 -07:00
|
|
|
|
entries = new Dictionary<string, FileSystemMetadata>(StringComparer.OrdinalIgnoreCase);
|
2017-05-04 11:14:45 -07:00
|
|
|
|
|
2014-02-11 14:41:01 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
2015-08-15 13:01:40 -07:00
|
|
|
|
// using EnumerateFileSystemInfos doesn't handle reparse points (symlinks)
|
2017-05-04 11:14:45 -07:00
|
|
|
|
var list = _fileSystem.GetFileSystemEntries(path)
|
2016-02-29 09:23:30 -07:00
|
|
|
|
.ToList();
|
2014-10-25 11:32:58 -07:00
|
|
|
|
|
|
|
|
|
// Seeing dupes on some users file system for some reason
|
|
|
|
|
foreach (var item in list)
|
|
|
|
|
{
|
|
|
|
|
entries[item.FullName] = item;
|
|
|
|
|
}
|
2014-02-11 14:41:01 -07:00
|
|
|
|
}
|
2016-10-25 12:02:04 -07:00
|
|
|
|
catch (IOException)
|
2014-02-11 14:41:01 -07:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-04 11:14:45 -07:00
|
|
|
|
//var group = entries.ToLookup(i => _fileSystem.GetDirectoryName(i.FullName)).ToList();
|
2014-10-25 11:32:58 -07:00
|
|
|
|
|
2014-02-15 09:36:09 -07:00
|
|
|
|
_cache.TryAdd(path, entries);
|
2014-02-08 15:38:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return entries;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-03 20:38:46 -07:00
|
|
|
|
private IEnumerable<FileSystemMetadata> GetFileSystemEntries(string path, bool clearCache)
|
2014-10-25 11:32:58 -07:00
|
|
|
|
{
|
|
|
|
|
return GetFileSystemDictionary(path, clearCache).Values;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-03 20:38:46 -07:00
|
|
|
|
public IEnumerable<FileSystemMetadata> GetFiles(string path)
|
2014-02-08 15:38:02 -07:00
|
|
|
|
{
|
2014-05-07 22:04:39 -07:00
|
|
|
|
return GetFiles(path, false);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-03 20:38:46 -07:00
|
|
|
|
public IEnumerable<FileSystemMetadata> GetFiles(string path, bool clearCache)
|
2014-05-07 22:04:39 -07:00
|
|
|
|
{
|
2015-11-12 13:51:39 -07:00
|
|
|
|
return GetFileSystemEntries(path, clearCache).Where(i => !i.IsDirectory);
|
2014-02-08 15:38:02 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-28 23:26:48 -07:00
|
|
|
|
public IEnumerable<string> GetFilePaths(string path)
|
|
|
|
|
{
|
|
|
|
|
return _fileSystem.GetFilePaths(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<string> GetFilePaths(string path, bool clearCache)
|
|
|
|
|
{
|
|
|
|
|
return _fileSystem.GetFilePaths(path);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-03 20:38:46 -07:00
|
|
|
|
public FileSystemMetadata GetFile(string path)
|
2014-02-08 15:38:02 -07:00
|
|
|
|
{
|
2016-08-06 14:10:18 -07:00
|
|
|
|
FileSystemMetadata file;
|
|
|
|
|
if (!_fileCache.TryGetValue(path, out file))
|
|
|
|
|
{
|
|
|
|
|
file = _fileSystem.GetFileInfo(path);
|
|
|
|
|
|
2017-07-21 12:05:52 -07:00
|
|
|
|
if (file != null && file.Exists)
|
2016-08-06 14:10:18 -07:00
|
|
|
|
{
|
|
|
|
|
_fileCache.TryAdd(path, file);
|
|
|
|
|
}
|
2017-07-21 12:05:52 -07:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2016-08-06 14:10:18 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return file;
|
|
|
|
|
//return _fileSystem.GetFileInfo(path);
|
2014-02-08 15:38:02 -07:00
|
|
|
|
}
|
2014-11-17 19:48:22 -07:00
|
|
|
|
|
2015-10-03 20:38:46 -07:00
|
|
|
|
public IEnumerable<FileSystemMetadata> GetDirectories(string path)
|
2014-11-17 19:48:22 -07:00
|
|
|
|
{
|
2015-11-12 13:51:39 -07:00
|
|
|
|
return GetFileSystemEntries(path, false).Where(i => i.IsDirectory);
|
2014-11-17 19:48:22 -07:00
|
|
|
|
}
|
2014-02-08 15:38:02 -07:00
|
|
|
|
}
|
|
|
|
|
}
|