FileData.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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="searchPattern">The search pattern.</param>
  20. /// <param name="flattenFolderDepth">The flatten folder depth.</param>
  21. /// <param name="resolveShortcuts">if set to <c>true</c> [resolve shortcuts].</param>
  22. /// <param name="args">The args.</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, string searchPattern = "*", int flattenFolderDepth = 0, bool resolveShortcuts = true, ItemResolveArgs args = null)
  26. {
  27. if (string.IsNullOrEmpty(path))
  28. {
  29. throw new ArgumentNullException("path");
  30. }
  31. var dict = new Dictionary<string, FileSystemInfo>(StringComparer.OrdinalIgnoreCase);
  32. var entries = new DirectoryInfo(path).EnumerateFileSystemInfos(searchPattern, SearchOption.TopDirectoryOnly);
  33. foreach (var entry in entries)
  34. {
  35. var isDirectory = entry.Attributes.HasFlag(FileAttributes.Directory);
  36. if (resolveShortcuts && FileSystem.IsShortcut(entry.FullName))
  37. {
  38. var newPath = FileSystem.ResolveShortcut(entry.FullName);
  39. if (string.IsNullOrWhiteSpace(newPath))
  40. {
  41. //invalid shortcut - could be old or target could just be unavailable
  42. logger.Warn("Encountered invalid shortuct: " + entry.FullName);
  43. continue;
  44. }
  45. var data = FileSystem.GetFileSystemInfo(newPath);
  46. if (data.Exists)
  47. {
  48. // Find out if the shortcut is pointing to a directory or file
  49. if (data.Attributes.HasFlag(FileAttributes.Directory))
  50. {
  51. // add to our physical locations
  52. if (args != null)
  53. {
  54. args.AddAdditionalLocation(newPath);
  55. }
  56. }
  57. dict[data.FullName] = data;
  58. }
  59. }
  60. else if (flattenFolderDepth > 0 && isDirectory)
  61. {
  62. foreach (var child in GetFilteredFileSystemEntries(entry.FullName, logger, flattenFolderDepth: flattenFolderDepth - 1, resolveShortcuts: resolveShortcuts))
  63. {
  64. dict[child.Key] = child.Value;
  65. }
  66. }
  67. else
  68. {
  69. dict[entry.FullName] = entry;
  70. }
  71. }
  72. return dict;
  73. }
  74. }
  75. }