FileData.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using MediaBrowser.Controller.Library;
  2. using MediaBrowser.Controller.Providers;
  3. using MediaBrowser.Model.Logging;
  4. using System;
  5. using System.Collections.Generic;
  6. using CommonIO;
  7. namespace MediaBrowser.Controller.IO
  8. {
  9. /// <summary>
  10. /// Provides low level File access that is much faster than the File/Directory api's
  11. /// </summary>
  12. public static class FileData
  13. {
  14. /// <summary>
  15. /// Gets the filtered file system entries.
  16. /// </summary>
  17. /// <param name="directoryService">The directory service.</param>
  18. /// <param name="path">The path.</param>
  19. /// <param name="fileSystem">The file system.</param>
  20. /// <param name="logger">The logger.</param>
  21. /// <param name="args">The args.</param>
  22. /// <param name="flattenFolderDepth">The flatten folder depth.</param>
  23. /// <param name="resolveShortcuts">if set to <c>true</c> [resolve shortcuts].</param>
  24. /// <returns>Dictionary{System.StringFileSystemInfo}.</returns>
  25. /// <exception cref="System.ArgumentNullException">path</exception>
  26. public static Dictionary<string, FileSystemMetadata> GetFilteredFileSystemEntries(IDirectoryService directoryService,
  27. string path,
  28. IFileSystem fileSystem,
  29. ILogger logger,
  30. ItemResolveArgs args,
  31. int flattenFolderDepth = 0,
  32. bool resolveShortcuts = true)
  33. {
  34. if (string.IsNullOrEmpty(path))
  35. {
  36. throw new ArgumentNullException("path");
  37. }
  38. if (args == null)
  39. {
  40. throw new ArgumentNullException("args");
  41. }
  42. if (!resolveShortcuts && flattenFolderDepth == 0)
  43. {
  44. return directoryService.GetFileSystemDictionary(path);
  45. }
  46. var entries = directoryService.GetFileSystemEntries(path);
  47. var dict = new Dictionary<string, FileSystemMetadata>(StringComparer.OrdinalIgnoreCase);
  48. foreach (var entry in entries)
  49. {
  50. var isDirectory = entry.IsDirectory;
  51. var fullName = entry.FullName;
  52. if (resolveShortcuts && fileSystem.IsShortcut(fullName))
  53. {
  54. try
  55. {
  56. var newPath = fileSystem.ResolveShortcut(fullName);
  57. if (string.IsNullOrWhiteSpace(newPath))
  58. {
  59. //invalid shortcut - could be old or target could just be unavailable
  60. logger.Warn("Encountered invalid shortcut: " + fullName);
  61. continue;
  62. }
  63. // Don't check if it exists here because that could return false for network shares.
  64. var data = fileSystem.GetDirectoryInfo(newPath);
  65. // add to our physical locations
  66. args.AddAdditionalLocation(newPath);
  67. dict[newPath] = data;
  68. }
  69. catch (Exception ex)
  70. {
  71. logger.ErrorException("Error resolving shortcut from {0}", ex, fullName);
  72. }
  73. }
  74. else if (flattenFolderDepth > 0 && isDirectory)
  75. {
  76. foreach (var child in GetFilteredFileSystemEntries(directoryService, fullName, fileSystem, logger, args, flattenFolderDepth: flattenFolderDepth - 1, resolveShortcuts: resolveShortcuts))
  77. {
  78. dict[child.Key] = child.Value;
  79. }
  80. }
  81. else
  82. {
  83. dict[fullName] = entry;
  84. }
  85. }
  86. return dict;
  87. }
  88. }
  89. }