VideoResolver.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System.Collections.Generic;
  2. using System.ComponentModel.Composition;
  3. using System.IO;
  4. using MediaBrowser.Controller.Events;
  5. using MediaBrowser.Model.Entities;
  6. namespace MediaBrowser.Controller.Resolvers
  7. {
  8. /// <summary>
  9. /// Resolves a Path into a Video
  10. /// </summary>
  11. [Export(typeof(IBaseItemResolver))]
  12. public class VideoResolver : BaseVideoResolver<Video>
  13. {
  14. }
  15. /// <summary>
  16. /// Resolves a Path into a Video or Video subclass
  17. /// </summary>
  18. public abstract class BaseVideoResolver<T> : BaseItemResolver<T>
  19. where T : Video, new()
  20. {
  21. protected override T Resolve(ItemResolveEventArgs args)
  22. {
  23. // If the path is a file check for a matching extensions
  24. if (!args.IsFolder)
  25. {
  26. if (IsVideoFile(args.Path))
  27. {
  28. return new T()
  29. {
  30. VideoType = VideoType.VideoFile,
  31. Path = args.Path
  32. };
  33. }
  34. }
  35. else
  36. {
  37. // If the path is a folder, check if it's bluray or dvd
  38. T item = ResolveFromFolderName(args.Path);
  39. if (item != null)
  40. {
  41. return item;
  42. }
  43. // Also check the subfolders for bluray or dvd
  44. foreach (KeyValuePair<string, FileAttributes> folder in args.FileSystemChildren)
  45. {
  46. if (!folder.Value.HasFlag(FileAttributes.Directory))
  47. {
  48. continue;
  49. }
  50. item = ResolveFromFolderName(folder.Key);
  51. if (item != null)
  52. {
  53. return item;
  54. }
  55. }
  56. }
  57. return null;
  58. }
  59. private T ResolveFromFolderName(string folder)
  60. {
  61. if (folder.IndexOf("video_ts", System.StringComparison.OrdinalIgnoreCase) != -1)
  62. {
  63. return new T()
  64. {
  65. VideoType = VideoType.DVD,
  66. Path = Path.GetDirectoryName(folder)
  67. };
  68. }
  69. if (folder.IndexOf("bdmv", System.StringComparison.OrdinalIgnoreCase) != -1)
  70. {
  71. return new T()
  72. {
  73. VideoType = VideoType.BluRay,
  74. Path = Path.GetDirectoryName(folder)
  75. };
  76. }
  77. return null;
  78. }
  79. private static bool IsVideoFile(string path)
  80. {
  81. string extension = Path.GetExtension(path).ToLower();
  82. switch (extension)
  83. {
  84. case ".mkv":
  85. case ".m2ts":
  86. case ".iso":
  87. case ".ts":
  88. case ".rmvb":
  89. case ".mov":
  90. case ".avi":
  91. case ".mpg":
  92. case ".mpeg":
  93. case ".wmv":
  94. case ".mp4":
  95. case ".divx":
  96. case ".dvr-ms":
  97. case ".wtv":
  98. case ".ogm":
  99. case ".ogv":
  100. case ".asf":
  101. case ".m4v":
  102. case ".flv":
  103. case ".f4v":
  104. case ".3gp":
  105. return true;
  106. default:
  107. return false;
  108. }
  109. }
  110. }
  111. }