FileData.cs 4.4 KB

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