FileData.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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, string path, IFileSystem fileSystem, ILogger logger, ItemResolveArgs args, int flattenFolderDepth = 0, bool resolveShortcuts = true)
  28. {
  29. if (string.IsNullOrEmpty(path))
  30. {
  31. throw new ArgumentNullException("path");
  32. }
  33. if (args == null)
  34. {
  35. throw new ArgumentNullException("args");
  36. }
  37. var entries = directoryService.GetFileSystemEntries(path);
  38. if (!resolveShortcuts && flattenFolderDepth == 0)
  39. {
  40. // Seeing dupes on some users file system for some reason
  41. var dictionary = new Dictionary<string, FileSystemInfo>(StringComparer.OrdinalIgnoreCase);
  42. foreach (var info in entries)
  43. {
  44. dictionary[info.FullName] = info;
  45. }
  46. return dictionary;
  47. }
  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. }