FileData.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using MediaBrowser.Controller.Library;
  2. using MediaBrowser.Controller.Providers;
  3. using MediaBrowser.Model.Logging;
  4. using System;
  5. using System.Collections.Generic;
  6. using MediaBrowser.Model.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. private static Dictionary<string, FileSystemMetadata> GetFileSystemDictionary(FileSystemMetadata[] list)
  15. {
  16. var dict = new Dictionary<string, FileSystemMetadata>(StringComparer.OrdinalIgnoreCase);
  17. foreach (var file in list)
  18. {
  19. dict[file.FullName] = file;
  20. }
  21. return dict;
  22. }
  23. /// <summary>
  24. /// Gets the filtered file system entries.
  25. /// </summary>
  26. /// <param name="directoryService">The directory service.</param>
  27. /// <param name="path">The path.</param>
  28. /// <param name="fileSystem">The file system.</param>
  29. /// <param name="logger">The logger.</param>
  30. /// <param name="args">The args.</param>
  31. /// <param name="flattenFolderDepth">The flatten folder depth.</param>
  32. /// <param name="resolveShortcuts">if set to <c>true</c> [resolve shortcuts].</param>
  33. /// <returns>Dictionary{System.StringFileSystemInfo}.</returns>
  34. /// <exception cref="System.ArgumentNullException">path</exception>
  35. public static FileSystemMetadata[] GetFilteredFileSystemEntries(IDirectoryService directoryService,
  36. string path,
  37. IFileSystem fileSystem,
  38. ILogger logger,
  39. ItemResolveArgs args,
  40. int flattenFolderDepth = 0,
  41. bool resolveShortcuts = true)
  42. {
  43. if (string.IsNullOrEmpty(path))
  44. {
  45. throw new ArgumentNullException("path");
  46. }
  47. if (args == null)
  48. {
  49. throw new ArgumentNullException("args");
  50. }
  51. var entries = directoryService.GetFileSystemEntries(path);
  52. if (!resolveShortcuts && flattenFolderDepth == 0)
  53. {
  54. return entries;
  55. }
  56. var dict = new Dictionary<string, FileSystemMetadata>(StringComparer.OrdinalIgnoreCase);
  57. foreach (var entry in entries)
  58. {
  59. var isDirectory = entry.IsDirectory;
  60. var fullName = entry.FullName;
  61. if (resolveShortcuts && fileSystem.IsShortcut(fullName))
  62. {
  63. try
  64. {
  65. var newPath = fileSystem.ResolveShortcut(fullName);
  66. if (string.IsNullOrWhiteSpace(newPath))
  67. {
  68. //invalid shortcut - could be old or target could just be unavailable
  69. logger.Warn("Encountered invalid shortcut: " + fullName);
  70. continue;
  71. }
  72. // Don't check if it exists here because that could return false for network shares.
  73. var data = fileSystem.GetDirectoryInfo(newPath);
  74. // add to our physical locations
  75. args.AddAdditionalLocation(newPath);
  76. dict[newPath] = data;
  77. }
  78. catch (Exception ex)
  79. {
  80. logger.ErrorException("Error resolving shortcut from {0}", ex, fullName);
  81. }
  82. }
  83. else if (flattenFolderDepth > 0 && isDirectory)
  84. {
  85. foreach (var child in GetFilteredFileSystemEntries(directoryService, fullName, fileSystem, logger, args, flattenFolderDepth: flattenFolderDepth - 1, resolveShortcuts: resolveShortcuts))
  86. {
  87. dict[child.FullName] = child;
  88. }
  89. }
  90. else
  91. {
  92. dict[fullName] = entry;
  93. }
  94. }
  95. var returnResult = new FileSystemMetadata[dict.Count];
  96. var index = 0;
  97. var values = dict.Values;
  98. foreach (var value in values)
  99. {
  100. returnResult[index] = value;
  101. index++;
  102. }
  103. return returnResult;
  104. }
  105. }
  106. }