FileData.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using MediaBrowser.Controller.Library;
  2. using MediaBrowser.Model.Logging;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. namespace MediaBrowser.Controller.IO
  7. {
  8. /// <summary>
  9. /// Provides low level File access that is much faster than the File/Directory api's
  10. /// </summary>
  11. public static class FileData
  12. {
  13. /// <summary>
  14. /// Gets the filtered file system entries.
  15. /// </summary>
  16. /// <param name="path">The path.</param>
  17. /// <param name="logger">The logger.</param>
  18. /// <param name="searchPattern">The search pattern.</param>
  19. /// <param name="flattenFolderDepth">The flatten folder depth.</param>
  20. /// <param name="resolveShortcuts">if set to <c>true</c> [resolve shortcuts].</param>
  21. /// <param name="args">The args.</param>
  22. /// <returns>Dictionary{System.StringFileSystemInfo}.</returns>
  23. /// <exception cref="System.ArgumentNullException">path</exception>
  24. public static Dictionary<string, FileSystemInfo> GetFilteredFileSystemEntries(string path, ILogger logger, string searchPattern = "*", int flattenFolderDepth = 0, bool resolveShortcuts = true, ItemResolveArgs args = null)
  25. {
  26. if (string.IsNullOrEmpty(path))
  27. {
  28. throw new ArgumentNullException("path");
  29. }
  30. var dict = new Dictionary<string, FileSystemInfo>(StringComparer.OrdinalIgnoreCase);
  31. var entries = new DirectoryInfo(path).EnumerateFileSystemInfos(searchPattern, SearchOption.TopDirectoryOnly);
  32. foreach (var entry in entries)
  33. {
  34. var isDirectory = entry.Attributes.HasFlag(FileAttributes.Directory);
  35. if (resolveShortcuts && FileSystem.IsShortcut(entry.FullName))
  36. {
  37. var newPath = FileSystem.ResolveShortcut(entry.FullName);
  38. if (string.IsNullOrWhiteSpace(newPath))
  39. {
  40. //invalid shortcut - could be old or target could just be unavailable
  41. logger.Warn("Encountered invalid shortcut: " + entry.FullName);
  42. continue;
  43. }
  44. var data = FileSystem.GetFileSystemInfo(newPath);
  45. if (data.Exists)
  46. {
  47. // Find out if the shortcut is pointing to a directory or file
  48. if (data.Attributes.HasFlag(FileAttributes.Directory))
  49. {
  50. // add to our physical locations
  51. if (args != null)
  52. {
  53. args.AddAdditionalLocation(newPath);
  54. }
  55. }
  56. dict[data.FullName] = data;
  57. }
  58. }
  59. else if (flattenFolderDepth > 0 && isDirectory)
  60. {
  61. foreach (var child in GetFilteredFileSystemEntries(entry.FullName, logger, flattenFolderDepth: flattenFolderDepth - 1, resolveShortcuts: resolveShortcuts))
  62. {
  63. dict[child.Key] = child.Value;
  64. }
  65. }
  66. else
  67. {
  68. dict[entry.FullName] = entry;
  69. }
  70. }
  71. return dict;
  72. }
  73. }
  74. }