FileData.cs 4.1 KB

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