FileData.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using MediaBrowser.Controller.Library;
  2. using MediaBrowser.Model.Logging;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. namespace MediaBrowser.Controller.IO
  7. {
  8. /// <summary>
  9. /// Provides low level File access that is much faster than the File/Directory api's
  10. /// </summary>
  11. public static class FileData
  12. {
  13. /// <summary>
  14. /// Gets the filtered file system entries.
  15. /// </summary>
  16. /// <param name="path">The path.</param>
  17. /// <param name="logger">The logger.</param>
  18. /// <param name="args">The args.</param>
  19. /// <param name="searchPattern">The search pattern.</param>
  20. /// <param name="flattenFolderDepth">The flatten folder depth.</param>
  21. /// <param name="resolveShortcuts">if set to <c>true</c> [resolve shortcuts].</param>
  22. /// <returns>Dictionary{System.StringFileSystemInfo}.</returns>
  23. /// <exception cref="System.ArgumentNullException">path</exception>
  24. public static Dictionary<string, FileSystemInfo> GetFilteredFileSystemEntries(string path, ILogger logger, ItemResolveArgs args, string searchPattern = "*", int flattenFolderDepth = 0, bool resolveShortcuts = true)
  25. {
  26. if (string.IsNullOrEmpty(path))
  27. {
  28. throw new ArgumentNullException("path");
  29. }
  30. if (args == null)
  31. {
  32. throw new ArgumentNullException("args");
  33. }
  34. var entries = new DirectoryInfo(path).EnumerateFileSystemInfos(searchPattern, SearchOption.TopDirectoryOnly);
  35. if (!resolveShortcuts && flattenFolderDepth == 0)
  36. {
  37. // Seeing dupes on some users file system for some reason
  38. var dictionary = new Dictionary<string, FileSystemInfo>(StringComparer.OrdinalIgnoreCase);
  39. foreach (var info in entries)
  40. {
  41. dictionary[info.FullName] = info;
  42. }
  43. return dictionary;
  44. }
  45. var dict = new Dictionary<string, FileSystemInfo>(StringComparer.OrdinalIgnoreCase);
  46. foreach (var entry in entries)
  47. {
  48. var isDirectory = (entry.Attributes & FileAttributes.Directory) == FileAttributes.Directory;
  49. var fullName = entry.FullName;
  50. if (resolveShortcuts && FileSystem.IsShortcut(fullName))
  51. {
  52. var newPath = FileSystem.ResolveShortcut(fullName);
  53. if (string.IsNullOrWhiteSpace(newPath))
  54. {
  55. //invalid shortcut - could be old or target could just be unavailable
  56. logger.Warn("Encountered invalid shortcut: " + fullName);
  57. continue;
  58. }
  59. // Don't check if it exists here because that could return false for network shares.
  60. var data = new DirectoryInfo(newPath);
  61. // add to our physical locations
  62. args.AddAdditionalLocation(newPath);
  63. dict[newPath] = data;
  64. }
  65. else if (flattenFolderDepth > 0 && isDirectory)
  66. {
  67. foreach (var child in GetFilteredFileSystemEntries(fullName, logger, args, flattenFolderDepth: flattenFolderDepth - 1, resolveShortcuts: resolveShortcuts))
  68. {
  69. dict[child.Key] = child.Value;
  70. }
  71. }
  72. else
  73. {
  74. dict[fullName] = entry;
  75. }
  76. }
  77. return dict;
  78. }
  79. }
  80. }