FileData.cs 3.8 KB

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