FileData.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using MediaBrowser.Controller.Library;
  2. using MediaBrowser.Model.Logging;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  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="path">The path.</param>
  18. /// <param name="logger">The logger.</param>
  19. /// <param name="args">The args.</param>
  20. /// <param name="searchPattern">The search pattern.</param>
  21. /// <param name="flattenFolderDepth">The flatten folder depth.</param>
  22. /// <param name="resolveShortcuts">if set to <c>true</c> [resolve shortcuts].</param>
  23. /// <returns>Dictionary{System.StringFileSystemInfo}.</returns>
  24. /// <exception cref="System.ArgumentNullException">path</exception>
  25. public static Dictionary<string, FileSystemInfo> GetFilteredFileSystemEntries(string path, ILogger logger, ItemResolveArgs args, string searchPattern = "*", int flattenFolderDepth = 0, bool resolveShortcuts = true)
  26. {
  27. if (string.IsNullOrEmpty(path))
  28. {
  29. throw new ArgumentNullException("path");
  30. }
  31. if (args == null)
  32. {
  33. throw new ArgumentNullException("args");
  34. }
  35. var entries = new DirectoryInfo(path).EnumerateFileSystemInfos(searchPattern, SearchOption.TopDirectoryOnly);
  36. if (!resolveShortcuts && flattenFolderDepth == 0)
  37. {
  38. return entries.ToDictionary(i => i.FullName, StringComparer.OrdinalIgnoreCase);
  39. }
  40. var dict = new Dictionary<string, FileSystemInfo>(StringComparer.OrdinalIgnoreCase);
  41. foreach (var entry in entries)
  42. {
  43. var isDirectory = (entry.Attributes & FileAttributes.Directory) == FileAttributes.Directory;
  44. var fullName = entry.FullName;
  45. if (resolveShortcuts && FileSystem.IsShortcut(fullName))
  46. {
  47. var newPath = FileSystem.ResolveShortcut(fullName);
  48. if (string.IsNullOrWhiteSpace(newPath))
  49. {
  50. //invalid shortcut - could be old or target could just be unavailable
  51. logger.Warn("Encountered invalid shortcut: " + fullName);
  52. continue;
  53. }
  54. // Don't check if it exists here because that could return false for network shares.
  55. var data = new DirectoryInfo(newPath);
  56. // add to our physical locations
  57. args.AddAdditionalLocation(newPath);
  58. dict[newPath] = data;
  59. }
  60. else if (flattenFolderDepth > 0 && isDirectory)
  61. {
  62. foreach (var child in GetFilteredFileSystemEntries(fullName, logger, args, flattenFolderDepth: flattenFolderDepth - 1, resolveShortcuts: resolveShortcuts))
  63. {
  64. dict[child.Key] = child.Value;
  65. }
  66. }
  67. else
  68. {
  69. dict[fullName] = entry;
  70. }
  71. }
  72. return dict;
  73. }
  74. }
  75. }