FileData.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. 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, FileSystemInfo> 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, FileSystemInfo>(StringComparer.OrdinalIgnoreCase);
  49. foreach (var entry in entries)
  50. {
  51. var isDirectory = (entry.Attributes & FileAttributes.Directory) == FileAttributes.Directory;
  52. var fullName = entry.FullName;
  53. if (resolveShortcuts && fileSystem.IsShortcut(fullName))
  54. {
  55. var newPath = fileSystem.ResolveShortcut(fullName);
  56. if (string.IsNullOrWhiteSpace(newPath))
  57. {
  58. //invalid shortcut - could be old or target could just be unavailable
  59. logger.Warn("Encountered invalid shortcut: " + fullName);
  60. continue;
  61. }
  62. // Don't check if it exists here because that could return false for network shares.
  63. var data = new DirectoryInfo(newPath);
  64. // add to our physical locations
  65. args.AddAdditionalLocation(newPath);
  66. dict[newPath] = data;
  67. }
  68. else if (flattenFolderDepth > 0 && isDirectory)
  69. {
  70. foreach (var child in GetFilteredFileSystemEntries(directoryService, fullName, fileSystem, logger, args, flattenFolderDepth: flattenFolderDepth - 1, resolveShortcuts: resolveShortcuts))
  71. {
  72. dict[child.Key] = child.Value;
  73. }
  74. }
  75. else
  76. {
  77. dict[fullName] = entry;
  78. }
  79. }
  80. return dict;
  81. }
  82. }
  83. }