2020-08-22 15:56:24 -04:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 15:01:16 -05:00
|
|
|
using System;
|
2020-09-27 20:02:10 -04:00
|
|
|
using System.Collections.Concurrent;
|
2018-12-27 18:27:57 -05:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.Providers
|
|
|
|
{
|
|
|
|
public class DirectoryService : IDirectoryService
|
|
|
|
{
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
|
2021-03-26 19:26:56 -04:00
|
|
|
private readonly ConcurrentDictionary<string, FileSystemMetadata[]> _cache = new (StringComparer.Ordinal);
|
2018-12-27 18:27:57 -05:00
|
|
|
|
2021-03-26 19:26:56 -04:00
|
|
|
private readonly ConcurrentDictionary<string, FileSystemMetadata> _fileCache = new (StringComparer.Ordinal);
|
2018-12-27 18:27:57 -05:00
|
|
|
|
2021-03-26 19:26:56 -04:00
|
|
|
private readonly ConcurrentDictionary<string, List<string>> _filePathCache = new (StringComparer.Ordinal);
|
2018-12-27 18:27:57 -05:00
|
|
|
|
2019-09-10 16:37:53 -04:00
|
|
|
public DirectoryService(IFileSystem fileSystem)
|
2018-12-27 18:27:57 -05:00
|
|
|
{
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
}
|
|
|
|
|
|
|
|
public FileSystemMetadata[] GetFileSystemEntries(string path)
|
|
|
|
{
|
2021-05-23 18:30:41 -04:00
|
|
|
return _cache.GetOrAdd(path, (p, fileSystem) => fileSystem.GetFileSystemEntries(p).ToArray(), _fileSystem);
|
2018-12-27 18:27:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<FileSystemMetadata> GetFiles(string path)
|
|
|
|
{
|
|
|
|
var list = new List<FileSystemMetadata>();
|
|
|
|
var items = GetFileSystemEntries(path);
|
2021-05-23 18:30:41 -04:00
|
|
|
for (var i = 0; i < items.Length; i++)
|
2018-12-27 18:27:57 -05:00
|
|
|
{
|
2021-05-23 18:30:41 -04:00
|
|
|
var item = items[i];
|
2018-12-27 18:27:57 -05:00
|
|
|
if (!item.IsDirectory)
|
|
|
|
{
|
|
|
|
list.Add(item);
|
|
|
|
}
|
|
|
|
}
|
2019-09-10 16:37:53 -04:00
|
|
|
|
2018-12-27 18:27:57 -05:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2021-05-06 18:52:06 -04:00
|
|
|
public FileSystemMetadata? GetFile(string path)
|
2018-12-27 18:27:57 -05:00
|
|
|
{
|
2021-05-06 18:52:06 -04:00
|
|
|
if (!_fileCache.TryGetValue(path, out var result))
|
2018-12-27 18:27:57 -05:00
|
|
|
{
|
2021-05-06 18:52:06 -04:00
|
|
|
var file = _fileSystem.GetFileInfo(path);
|
2021-05-23 18:30:41 -04:00
|
|
|
if (file.Exists)
|
2021-05-06 18:52:06 -04:00
|
|
|
{
|
2021-05-23 18:30:41 -04:00
|
|
|
result = file;
|
2021-05-06 18:52:06 -04:00
|
|
|
_fileCache.TryAdd(path, result);
|
|
|
|
}
|
2018-12-27 18:27:57 -05:00
|
|
|
}
|
|
|
|
|
2020-09-27 20:02:10 -04:00
|
|
|
return result;
|
2018-12-27 18:27:57 -05:00
|
|
|
}
|
|
|
|
|
2020-02-23 04:53:51 -05:00
|
|
|
public IReadOnlyList<string> GetFilePaths(string path)
|
2021-05-23 18:30:41 -04:00
|
|
|
=> GetFilePaths(path, false, false);
|
2018-12-27 18:27:57 -05:00
|
|
|
|
2021-05-23 18:30:41 -04:00
|
|
|
public IReadOnlyList<string> GetSortedFilePaths(string path, bool clearCache)
|
|
|
|
=> GetFilePaths(path, clearCache, true);
|
|
|
|
|
|
|
|
public IReadOnlyList<string> GetFilePaths(string path, bool clearCache, bool sort = false)
|
2018-12-27 18:27:57 -05:00
|
|
|
{
|
2020-09-27 20:02:10 -04:00
|
|
|
if (clearCache)
|
2018-12-27 18:27:57 -05:00
|
|
|
{
|
2020-09-27 20:24:12 -04:00
|
|
|
_filePathCache.TryRemove(path, out _);
|
2018-12-27 18:27:57 -05:00
|
|
|
}
|
|
|
|
|
2021-05-23 18:30:41 -04:00
|
|
|
var filePaths = _filePathCache.GetOrAdd(path, (p, fileSystem) => fileSystem.GetFilePaths(p).ToList(), _fileSystem);
|
|
|
|
|
|
|
|
if (sort)
|
|
|
|
{
|
|
|
|
filePaths.Sort();
|
|
|
|
}
|
|
|
|
|
|
|
|
return filePaths;
|
2018-12-27 18:27:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|