DirectoryService.cs 2.6 KB

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