jellyfin/MediaBrowser.Controller/Providers/DirectoryService.cs

120 lines
3.5 KiB
C#
Raw Normal View History

using MediaBrowser.Model.Logging;
2014-02-11 14:41:01 -07:00
using System;
using System.Collections.Concurrent;
2014-02-08 15:38:02 -07:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
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;
private readonly IFileSystem _fileSystem;
2014-02-08 15:38:02 -07:00
private readonly Dictionary<string, FileSystemMetadata[]> _cache = new Dictionary<string, FileSystemMetadata[]>(StringComparer.OrdinalIgnoreCase);
2014-02-08 15:38:02 -07:00
private readonly Dictionary<string, FileSystemMetadata> _fileCache = new Dictionary<string, FileSystemMetadata>(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, List<string>> _filePathCache = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
2016-08-06 14:10:18 -07:00
public DirectoryService(ILogger logger, IFileSystem fileSystem)
2014-02-08 15:38:02 -07:00
{
_logger = logger;
_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
{
}
public FileSystemMetadata[] GetFileSystemEntries(string path)
2014-02-08 15:38:02 -07:00
{
2014-12-28 10:59:40 -07:00
if (string.IsNullOrWhiteSpace(path))
{
throw new ArgumentNullException("path");
}
FileSystemMetadata[] entries;
2014-02-08 15:38:02 -07:00
if (!_cache.TryGetValue(path, out entries))
{
//_logger.Debug("Getting files for " + path);
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)
entries = _fileSystem.GetFileSystemEntries(path).ToArray();
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
{
entries = new FileSystemMetadata[] { };
2014-02-11 14:41:01 -07:00
}
//_cache.TryAdd(path, entries);
_cache[path] = entries;
2014-02-08 15:38:02 -07:00
}
return entries;
}
2017-08-24 12:52:19 -07:00
public List<FileSystemMetadata> GetFiles(string path)
2014-05-07 22:04:39 -07:00
{
2017-08-24 12:52:19 -07:00
var list = new List<FileSystemMetadata>();
var items = GetFileSystemEntries(path);
2017-08-24 12:52:19 -07:00
foreach (var item in items)
{
if (!item.IsDirectory)
{
list.Add(item);
}
}
return list;
2014-02-08 15:38:02 -07:00
}
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);
_fileCache[path] = file;
2016-08-06 14:10:18 -07:00
}
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
}
public List<string> GetFilePaths(string path)
{
return GetFilePaths(path, false);
}
public List<string> GetFilePaths(string path, bool clearCache)
{
List<string> result;
if (clearCache || !_filePathCache.TryGetValue(path, out result))
{
result = _fileSystem.GetFilePaths(path).ToList();
_filePathCache[path] = result;
}
return result;
}
2014-02-08 15:38:02 -07:00
}
}