FileSystemHelper.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Threading.Tasks;
  7. using MediaBrowser.Controller.Resolvers;
  8. using MediaBrowser.Controller.Library;
  9. namespace MediaBrowser.Controller.IO
  10. {
  11. public static class FileSystemHelper
  12. {
  13. /// <summary>
  14. /// Transforms shortcuts into their actual paths and filters out items that should be ignored
  15. /// </summary>
  16. public static ItemResolveEventArgs FilterChildFileSystemEntries(ItemResolveEventArgs args, bool flattenShortcuts)
  17. {
  18. List<WIN32_FIND_DATA> returnChildren = new List<WIN32_FIND_DATA>();
  19. List<WIN32_FIND_DATA> resolvedShortcuts = new List<WIN32_FIND_DATA>();
  20. foreach (var file in args.FileSystemChildren)
  21. {
  22. // If it's a shortcut, resolve it
  23. if (Shortcut.IsShortcut(file.Path))
  24. {
  25. string newPath = Shortcut.ResolveShortcut(file.Path);
  26. WIN32_FIND_DATA newPathData = FileData.GetFileData(newPath);
  27. // Find out if the shortcut is pointing to a directory or file
  28. if (newPathData.IsDirectory)
  29. {
  30. // add to our physical locations
  31. args.AdditionalLocations.Add(newPath);
  32. // If we're flattening then get the shortcut's children
  33. if (flattenShortcuts)
  34. {
  35. returnChildren.Add(file);
  36. ItemResolveEventArgs newArgs = new ItemResolveEventArgs()
  37. {
  38. FileSystemChildren = FileData.GetFileSystemEntries(newPath, "*").ToArray()
  39. };
  40. resolvedShortcuts.AddRange(FilterChildFileSystemEntries(newArgs, false).FileSystemChildren);
  41. }
  42. else
  43. {
  44. returnChildren.Add(newPathData);
  45. }
  46. }
  47. else
  48. {
  49. returnChildren.Add(newPathData);
  50. }
  51. }
  52. else
  53. {
  54. //not a shortcut check to see if we should filter it out
  55. if (EntityResolutionHelper.ShouldResolvePath(file))
  56. {
  57. returnChildren.Add(file);
  58. }
  59. else
  60. {
  61. //filtered - see if it is one of our "indicator" folders and mark it now - no reason to search for it again
  62. args.IsBDFolder |= file.cFileName.Equals("bdmv", StringComparison.OrdinalIgnoreCase);
  63. args.IsDVDFolder |= file.cFileName.Equals("video_ts", StringComparison.OrdinalIgnoreCase);
  64. args.IsHDDVDFolder |= file.cFileName.Equals("hvdvd_ts", StringComparison.OrdinalIgnoreCase);
  65. //and check to see if it is a metadata folder and collect contents now if so
  66. if (IsMetadataFolder(file.cFileName))
  67. {
  68. args.MetadataFiles = Directory.GetFiles(Path.Combine(args.Path, "metadata"), "*", SearchOption.TopDirectoryOnly);
  69. }
  70. }
  71. }
  72. }
  73. if (resolvedShortcuts.Count > 0)
  74. {
  75. resolvedShortcuts.InsertRange(0, returnChildren);
  76. args.FileSystemChildren = resolvedShortcuts.ToArray();
  77. }
  78. else
  79. {
  80. args.FileSystemChildren = returnChildren.ToArray();
  81. }
  82. return args;
  83. }
  84. public static bool IsMetadataFolder(string path)
  85. {
  86. return path.TrimEnd('\\').EndsWith("metadata", StringComparison.OrdinalIgnoreCase);
  87. }
  88. public static bool IsVideoFile(string path)
  89. {
  90. string extension = System.IO.Path.GetExtension(path).ToLower();
  91. switch (extension)
  92. {
  93. case ".mkv":
  94. case ".m2ts":
  95. case ".iso":
  96. case ".ts":
  97. case ".rmvb":
  98. case ".mov":
  99. case ".avi":
  100. case ".mpg":
  101. case ".mpeg":
  102. case ".wmv":
  103. case ".mp4":
  104. case ".divx":
  105. case ".dvr-ms":
  106. case ".wtv":
  107. case ".ogm":
  108. case ".ogv":
  109. case ".asf":
  110. case ".m4v":
  111. case ".flv":
  112. case ".f4v":
  113. case ".3gp":
  114. return true;
  115. default:
  116. return false;
  117. }
  118. }
  119. }
  120. }