FileData.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections.Generic;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Controller.Providers;
  5. using MediaBrowser.Model.IO;
  6. using Microsoft.Extensions.Logging;
  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="ArgumentNullException">path</exception>
  35. public static FileSystemMetadata[] GetFilteredFileSystemEntries(
  36. IDirectoryService directoryService,
  37. string path,
  38. IFileSystem fileSystem,
  39. IServerApplicationHost appHost,
  40. ILogger logger,
  41. ItemResolveArgs args,
  42. int flattenFolderDepth = 0,
  43. bool resolveShortcuts = true)
  44. {
  45. if (string.IsNullOrEmpty(path))
  46. {
  47. throw new ArgumentNullException(nameof(path));
  48. }
  49. if (args == null)
  50. {
  51. throw new ArgumentNullException(nameof(args));
  52. }
  53. var entries = directoryService.GetFileSystemEntries(path);
  54. if (!resolveShortcuts && flattenFolderDepth == 0)
  55. {
  56. return entries;
  57. }
  58. var dict = new Dictionary<string, FileSystemMetadata>(StringComparer.OrdinalIgnoreCase);
  59. foreach (var entry in entries)
  60. {
  61. var isDirectory = entry.IsDirectory;
  62. var fullName = entry.FullName;
  63. if (resolveShortcuts && fileSystem.IsShortcut(fullName))
  64. {
  65. try
  66. {
  67. var newPath = appHost.ExpandVirtualPath(fileSystem.ResolveShortcut(fullName));
  68. if (string.IsNullOrEmpty(newPath))
  69. {
  70. //invalid shortcut - could be old or target could just be unavailable
  71. logger.LogWarning("Encountered invalid shortcut: " + fullName);
  72. continue;
  73. }
  74. // Don't check if it exists here because that could return false for network shares.
  75. var data = fileSystem.GetDirectoryInfo(newPath);
  76. // add to our physical locations
  77. args.AddAdditionalLocation(newPath);
  78. dict[newPath] = data;
  79. }
  80. catch (Exception ex)
  81. {
  82. logger.LogError(ex, "Error resolving shortcut from {path}", fullName);
  83. }
  84. }
  85. else if (flattenFolderDepth > 0 && isDirectory)
  86. {
  87. foreach (var child in GetFilteredFileSystemEntries(directoryService, fullName, fileSystem, appHost, logger, args, flattenFolderDepth: flattenFolderDepth - 1, resolveShortcuts: resolveShortcuts))
  88. {
  89. dict[child.FullName] = child;
  90. }
  91. }
  92. else
  93. {
  94. dict[fullName] = entry;
  95. }
  96. }
  97. var returnResult = new FileSystemMetadata[dict.Count];
  98. var index = 0;
  99. var values = dict.Values;
  100. foreach (var value in values)
  101. {
  102. returnResult[index] = value;
  103. index++;
  104. }
  105. return returnResult;
  106. }
  107. }
  108. }