FileData.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Model.Logging;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. namespace MediaBrowser.Controller.IO
  8. {
  9. /// <summary>
  10. /// Provides low level File access that is much faster than the File/Directory api's
  11. /// </summary>
  12. public static class FileData
  13. {
  14. /// <summary>
  15. /// Gets the filtered file system entries.
  16. /// </summary>
  17. /// <param name="path">The path.</param>
  18. /// <param name="fileSystem">The file system.</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, IFileSystem fileSystem, 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. var dictionary = new Dictionary<string, FileSystemInfo>(StringComparer.OrdinalIgnoreCase);
  41. foreach (var info in entries)
  42. {
  43. dictionary[info.FullName] = info;
  44. }
  45. return dictionary;
  46. }
  47. var dict = new Dictionary<string, FileSystemInfo>(StringComparer.OrdinalIgnoreCase);
  48. foreach (var entry in entries)
  49. {
  50. var isDirectory = (entry.Attributes & FileAttributes.Directory) == FileAttributes.Directory;
  51. var fullName = entry.FullName;
  52. if (resolveShortcuts && fileSystem.IsShortcut(fullName))
  53. {
  54. var newPath = fileSystem.ResolveShortcut(fullName);
  55. if (string.IsNullOrWhiteSpace(newPath))
  56. {
  57. //invalid shortcut - could be old or target could just be unavailable
  58. logger.Warn("Encountered invalid shortcut: " + fullName);
  59. continue;
  60. }
  61. // Don't check if it exists here because that could return false for network shares.
  62. var data = new DirectoryInfo(newPath);
  63. // add to our physical locations
  64. args.AddAdditionalLocation(newPath);
  65. dict[newPath] = data;
  66. }
  67. else if (flattenFolderDepth > 0 && isDirectory)
  68. {
  69. foreach (var child in GetFilteredFileSystemEntries(fullName, fileSystem, logger, args, flattenFolderDepth: flattenFolderDepth - 1, resolveShortcuts: resolveShortcuts))
  70. {
  71. dict[child.Key] = child.Value;
  72. }
  73. }
  74. else
  75. {
  76. dict[fullName] = entry;
  77. }
  78. }
  79. return dict;
  80. }
  81. }
  82. }