FileData.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.Attributes & FileAttributes.Directory) == FileAttributes.Directory;
  53. var fullName = entry.FullName;
  54. if (resolveShortcuts && fileSystem.IsShortcut(fullName))
  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. else if (flattenFolderDepth > 0 && isDirectory)
  70. {
  71. foreach (var child in GetFilteredFileSystemEntries(directoryService, fullName, fileSystem, logger, args, flattenFolderDepth: flattenFolderDepth - 1, resolveShortcuts: resolveShortcuts))
  72. {
  73. dict[child.Key] = child.Value;
  74. }
  75. }
  76. else
  77. {
  78. dict[fullName] = entry;
  79. }
  80. }
  81. return dict;
  82. }
  83. }
  84. }