FileData.cs 3.7 KB

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