FileData.cs 3.3 KB

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