FileData.cs 4.1 KB

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