FileData.cs 4.1 KB

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