FileData.cs 4.2 KB

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