DirectoryService.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using Microsoft.Extensions.Logging;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using MediaBrowser.Controller.IO;
  8. using MediaBrowser.Model.IO;
  9. namespace MediaBrowser.Controller.Providers
  10. {
  11. public class DirectoryService : IDirectoryService
  12. {
  13. private readonly ILogger _logger;
  14. private readonly IFileSystem _fileSystem;
  15. private readonly Dictionary<string, FileSystemMetadata[]> _cache = new Dictionary<string, FileSystemMetadata[]>(StringComparer.OrdinalIgnoreCase);
  16. private readonly Dictionary<string, FileSystemMetadata> _fileCache = new Dictionary<string, FileSystemMetadata>(StringComparer.OrdinalIgnoreCase);
  17. private readonly Dictionary<string, List<string>> _filePathCache = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
  18. public DirectoryService(ILogger logger, IFileSystem fileSystem)
  19. {
  20. _logger = logger;
  21. _fileSystem = fileSystem;
  22. }
  23. public DirectoryService(IFileSystem fileSystem)
  24. : this(null, fileSystem) // TODO: @bond NullLogger
  25. {
  26. }
  27. public FileSystemMetadata[] GetFileSystemEntries(string path)
  28. {
  29. FileSystemMetadata[] entries;
  30. if (!_cache.TryGetValue(path, out entries))
  31. {
  32. //_logger.LogDebug("Getting files for " + path);
  33. entries = _fileSystem.GetFileSystemEntries(path).ToArray();
  34. //_cache.TryAdd(path, entries);
  35. _cache[path] = entries;
  36. }
  37. return entries;
  38. }
  39. public List<FileSystemMetadata> GetFiles(string path)
  40. {
  41. var list = new List<FileSystemMetadata>();
  42. var items = GetFileSystemEntries(path);
  43. foreach (var item in items)
  44. {
  45. if (!item.IsDirectory)
  46. {
  47. list.Add(item);
  48. }
  49. }
  50. return list;
  51. }
  52. public FileSystemMetadata GetFile(string path)
  53. {
  54. FileSystemMetadata file;
  55. if (!_fileCache.TryGetValue(path, out file))
  56. {
  57. file = _fileSystem.GetFileInfo(path);
  58. if (file != null && file.Exists)
  59. {
  60. //_fileCache.TryAdd(path, file);
  61. _fileCache[path] = file;
  62. }
  63. else
  64. {
  65. return null;
  66. }
  67. }
  68. return file;
  69. //return _fileSystem.GetFileInfo(path);
  70. }
  71. public List<string> GetFilePaths(string path)
  72. {
  73. return GetFilePaths(path, false);
  74. }
  75. public List<string> GetFilePaths(string path, bool clearCache)
  76. {
  77. List<string> result;
  78. if (clearCache || !_filePathCache.TryGetValue(path, out result))
  79. {
  80. result = _fileSystem.GetFilePaths(path).ToList();
  81. _filePathCache[path] = result;
  82. }
  83. return result;
  84. }
  85. }
  86. }