FileSystemHelper.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. args.IsHDDVDFolder |= file.cFileName.Equals("hvdvd_ts", StringComparison.OrdinalIgnoreCase);
  64. }
  65. }
  66. }
  67. if (resolvedShortcuts.Count > 0)
  68. {
  69. resolvedShortcuts.InsertRange(0, returnChildren);
  70. args.FileSystemChildren = resolvedShortcuts.ToArray();
  71. }
  72. else
  73. {
  74. args.FileSystemChildren = returnChildren.ToArray();
  75. }
  76. return args;
  77. }
  78. public static bool IsVideoFile(string path)
  79. {
  80. string extension = System.IO.Path.GetExtension(path).ToLower();
  81. switch (extension)
  82. {
  83. case ".mkv":
  84. case ".m2ts":
  85. case ".iso":
  86. case ".ts":
  87. case ".rmvb":
  88. case ".mov":
  89. case ".avi":
  90. case ".mpg":
  91. case ".mpeg":
  92. case ".wmv":
  93. case ".mp4":
  94. case ".divx":
  95. case ".dvr-ms":
  96. case ".wtv":
  97. case ".ogm":
  98. case ".ogv":
  99. case ".asf":
  100. case ".m4v":
  101. case ".flv":
  102. case ".f4v":
  103. case ".3gp":
  104. return true;
  105. default:
  106. return false;
  107. }
  108. }
  109. }
  110. }