2014-03-12 19:56:12 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2014-02-11 21:41:01 +00:00
|
|
|
|
using System;
|
2014-03-12 19:56:12 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
2014-02-08 22:38:02 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2017-05-26 06:48:54 +00:00
|
|
|
|
|
2016-10-25 19:02:04 +00:00
|
|
|
|
using MediaBrowser.Controller.IO;
|
|
|
|
|
using MediaBrowser.Model.IO;
|
2014-02-08 22:38:02 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.Providers
|
|
|
|
|
{
|
2014-02-10 18:39:41 +00:00
|
|
|
|
public class DirectoryService : IDirectoryService
|
2014-02-08 22:38:02 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
2017-08-09 19:56:38 +00:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2014-02-08 22:38:02 +00:00
|
|
|
|
|
2017-08-09 19:56:38 +00:00
|
|
|
|
private readonly ConcurrentDictionary<string, FileSystemMetadata[]> _cache =
|
|
|
|
|
new ConcurrentDictionary<string, FileSystemMetadata[]>(StringComparer.OrdinalIgnoreCase);
|
2014-02-08 22:38:02 +00:00
|
|
|
|
|
2016-08-06 21:10:18 +00:00
|
|
|
|
private readonly ConcurrentDictionary<string, FileSystemMetadata> _fileCache =
|
|
|
|
|
new ConcurrentDictionary<string, FileSystemMetadata>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
|
|
public DirectoryService(ILogger logger, IFileSystem fileSystem)
|
2014-02-08 22:38:02 +00:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
2017-08-09 19:56:38 +00:00
|
|
|
|
_fileSystem = fileSystem;
|
2014-02-08 22:38:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-06 04:48:00 +00:00
|
|
|
|
public DirectoryService(IFileSystem fileSystem)
|
2015-09-13 21:32:02 +00:00
|
|
|
|
: this(new NullLogger(), fileSystem)
|
2014-10-25 18:32:58 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-09 19:56:38 +00:00
|
|
|
|
public FileSystemMetadata[] GetFileSystemEntries(string path)
|
2014-05-08 05:04:39 +00:00
|
|
|
|
{
|
|
|
|
|
return GetFileSystemEntries(path, false);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-09 19:56:38 +00:00
|
|
|
|
private FileSystemMetadata[] GetFileSystemEntries(string path, bool clearCache)
|
2014-02-08 22:38:02 +00:00
|
|
|
|
{
|
2014-12-28 17:59:40 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(path))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("path");
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-09 19:56:38 +00:00
|
|
|
|
FileSystemMetadata[] entries;
|
2014-02-08 22:38:02 +00:00
|
|
|
|
|
2014-05-08 05:04:39 +00:00
|
|
|
|
if (clearCache)
|
|
|
|
|
{
|
2017-08-09 19:56:38 +00:00
|
|
|
|
FileSystemMetadata[] 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-02-11 21:41:01 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2015-08-15 20:01:40 +00:00
|
|
|
|
// using EnumerateFileSystemInfos doesn't handle reparse points (symlinks)
|
2017-08-09 19:56:38 +00:00
|
|
|
|
entries = _fileSystem.GetFileSystemEntries(path).ToArray();
|
2014-02-11 21:41:01 +00:00
|
|
|
|
}
|
2016-10-25 19:02:04 +00:00
|
|
|
|
catch (IOException)
|
2014-02-11 21:41:01 +00:00
|
|
|
|
{
|
2017-08-09 19:56:38 +00:00
|
|
|
|
entries = new FileSystemMetadata[] { };
|
2014-02-11 21:41:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-15 16:36:09 +00:00
|
|
|
|
_cache.TryAdd(path, entries);
|
2014-02-08 22:38:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return entries;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-04 03:38:46 +00:00
|
|
|
|
public IEnumerable<FileSystemMetadata> GetFiles(string path)
|
2014-02-08 22:38:02 +00:00
|
|
|
|
{
|
2014-05-08 05:04:39 +00:00
|
|
|
|
return GetFiles(path, false);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-04 03:38:46 +00:00
|
|
|
|
public IEnumerable<FileSystemMetadata> GetFiles(string path, bool clearCache)
|
2014-05-08 05:04:39 +00:00
|
|
|
|
{
|
2015-11-12 20:51:39 +00:00
|
|
|
|
return GetFileSystemEntries(path, clearCache).Where(i => !i.IsDirectory);
|
2014-02-08 22:38:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-04 03:38:46 +00:00
|
|
|
|
public FileSystemMetadata GetFile(string path)
|
2014-02-08 22:38:02 +00:00
|
|
|
|
{
|
2016-08-06 21:10:18 +00:00
|
|
|
|
FileSystemMetadata file;
|
|
|
|
|
if (!_fileCache.TryGetValue(path, out file))
|
|
|
|
|
{
|
|
|
|
|
file = _fileSystem.GetFileInfo(path);
|
|
|
|
|
|
2017-07-21 19:05:52 +00:00
|
|
|
|
if (file != null && file.Exists)
|
2016-08-06 21:10:18 +00:00
|
|
|
|
{
|
|
|
|
|
_fileCache.TryAdd(path, file);
|
|
|
|
|
}
|
2017-07-21 19:05:52 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2016-08-06 21:10:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return file;
|
|
|
|
|
//return _fileSystem.GetFileInfo(path);
|
2014-02-08 22:38:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|