FileData.cs 3.8 KB

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