FileData.cs 3.6 KB

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