2020-08-22 12:56:24 -07:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 13:01:16 -07:00
|
|
|
using System;
|
2020-09-27 17:02:10 -07:00
|
|
|
using System.Collections.Concurrent;
|
2018-12-27 16:27:57 -07:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.Providers
|
|
|
|
{
|
|
|
|
public class DirectoryService : IDirectoryService
|
|
|
|
{
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
|
2020-09-27 17:02:10 -07:00
|
|
|
private readonly ConcurrentDictionary<string, FileSystemMetadata[]> _cache = new ConcurrentDictionary<string, FileSystemMetadata[]>(StringComparer.OrdinalIgnoreCase);
|
2018-12-27 16:27:57 -07:00
|
|
|
|
2020-09-27 17:02:10 -07:00
|
|
|
private readonly ConcurrentDictionary<string, FileSystemMetadata> _fileCache = new ConcurrentDictionary<string, FileSystemMetadata>(StringComparer.OrdinalIgnoreCase);
|
2018-12-27 16:27:57 -07:00
|
|
|
|
2020-09-27 17:02:10 -07:00
|
|
|
private readonly ConcurrentDictionary<string, List<string>> _filePathCache = new ConcurrentDictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
|
2018-12-27 16:27:57 -07:00
|
|
|
|
2019-09-10 13:37:53 -07:00
|
|
|
public DirectoryService(IFileSystem fileSystem)
|
2018-12-27 16:27:57 -07:00
|
|
|
{
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
}
|
|
|
|
|
|
|
|
public FileSystemMetadata[] GetFileSystemEntries(string path)
|
|
|
|
{
|
2020-09-27 17:24:12 -07:00
|
|
|
return _cache.GetOrAdd(path, p => _fileSystem.GetFileSystemEntries(p).ToArray());
|
2018-12-27 16:27:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<FileSystemMetadata> GetFiles(string path)
|
|
|
|
{
|
|
|
|
var list = new List<FileSystemMetadata>();
|
|
|
|
var items = GetFileSystemEntries(path);
|
|
|
|
foreach (var item in items)
|
|
|
|
{
|
|
|
|
if (!item.IsDirectory)
|
|
|
|
{
|
|
|
|
list.Add(item);
|
|
|
|
}
|
|
|
|
}
|
2019-09-10 13:37:53 -07:00
|
|
|
|
2018-12-27 16:27:57 -07:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
public FileSystemMetadata GetFile(string path)
|
|
|
|
{
|
2020-09-27 17:24:12 -07:00
|
|
|
var result = _fileCache.GetOrAdd(path, p =>
|
2018-12-27 16:27:57 -07:00
|
|
|
{
|
2020-09-27 17:02:10 -07:00
|
|
|
var file = _fileSystem.GetFileInfo(path);
|
|
|
|
return (file != null && file.Exists) ? file : null;
|
|
|
|
});
|
2018-12-27 16:27:57 -07:00
|
|
|
|
2020-09-27 17:02:10 -07:00
|
|
|
if (result == null)
|
|
|
|
{
|
|
|
|
// lets not store null results in the cache
|
2020-09-27 17:24:12 -07:00
|
|
|
_fileCache.TryRemove(path, out _);
|
2018-12-27 16:27:57 -07:00
|
|
|
}
|
|
|
|
|
2020-09-27 17:02:10 -07:00
|
|
|
return result;
|
2018-12-27 16:27:57 -07:00
|
|
|
}
|
|
|
|
|
2020-02-23 02:53:51 -07:00
|
|
|
public IReadOnlyList<string> GetFilePaths(string path)
|
|
|
|
=> GetFilePaths(path, false);
|
2018-12-27 16:27:57 -07:00
|
|
|
|
2020-02-23 02:53:51 -07:00
|
|
|
public IReadOnlyList<string> GetFilePaths(string path, bool clearCache)
|
2018-12-27 16:27:57 -07:00
|
|
|
{
|
2020-09-27 17:02:10 -07:00
|
|
|
if (clearCache)
|
2018-12-27 16:27:57 -07:00
|
|
|
{
|
2020-09-27 17:24:12 -07:00
|
|
|
_filePathCache.TryRemove(path, out _);
|
2018-12-27 16:27:57 -07:00
|
|
|
}
|
|
|
|
|
2020-09-27 17:24:12 -07:00
|
|
|
return _filePathCache.GetOrAdd(path, p => _fileSystem.GetFilePaths(path).ToList());
|
2018-12-27 16:27:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|