FileSystemHelper.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Controller.Resolvers;
  7. using MediaBrowser.Controller.Library;
  8. namespace MediaBrowser.Controller.IO
  9. {
  10. public static class FileSystemHelper
  11. {
  12. /// <summary>
  13. /// Transforms shortcuts into their actual paths and filters out items that should be ignored
  14. /// </summary>
  15. public static ItemResolveEventArgs FilterChildFileSystemEntries(ItemResolveEventArgs args, bool flattenShortcuts)
  16. {
  17. List<WIN32_FIND_DATA> returnChildren = new List<WIN32_FIND_DATA>();
  18. List<WIN32_FIND_DATA> resolvedShortcuts = new List<WIN32_FIND_DATA>();
  19. foreach (var file in args.FileSystemChildren)
  20. {
  21. // If it's a shortcut, resolve it
  22. if (Shortcut.IsShortcut(file.Path))
  23. {
  24. string newPath = Shortcut.ResolveShortcut(file.Path);
  25. WIN32_FIND_DATA newPathData = FileData.GetFileData(newPath);
  26. // Find out if the shortcut is pointing to a directory or file
  27. if (newPathData.IsDirectory)
  28. {
  29. // add to our physical locations
  30. args.AdditionalLocations.Add(newPath);
  31. // If we're flattening then get the shortcut's children
  32. if (flattenShortcuts)
  33. {
  34. returnChildren.Add(file);
  35. ItemResolveEventArgs newArgs = new ItemResolveEventArgs()
  36. {
  37. FileSystemChildren = FileData.GetFileSystemEntries(newPath, "*").ToArray()
  38. };
  39. resolvedShortcuts.AddRange(FilterChildFileSystemEntries(newArgs, false).FileSystemChildren);
  40. }
  41. else
  42. {
  43. returnChildren.Add(newPathData);
  44. }
  45. }
  46. else
  47. {
  48. returnChildren.Add(newPathData);
  49. }
  50. }
  51. else
  52. {
  53. //not a shortcut check to see if we should filter it out
  54. if (EntityResolutionHelper.ShouldResolvePath(file))
  55. {
  56. returnChildren.Add(file);
  57. }
  58. else
  59. {
  60. //filtered - see if it is one of our "indicator" folders and mark it now - no reason to search for it again
  61. args.IsBDFolder |= file.cFileName.Equals("bdmv", StringComparison.OrdinalIgnoreCase);
  62. args.IsDVDFolder |= file.cFileName.Equals("video_ts", StringComparison.OrdinalIgnoreCase);
  63. }
  64. }
  65. }
  66. if (resolvedShortcuts.Count > 0)
  67. {
  68. resolvedShortcuts.InsertRange(0, returnChildren);
  69. args.FileSystemChildren = resolvedShortcuts.ToArray();
  70. }
  71. else
  72. {
  73. args.FileSystemChildren = returnChildren.ToArray();
  74. }
  75. return args;
  76. }
  77. }
  78. }