VideoResolver.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Model.Entities;
  4. using MediaBrowser.Controller.IO;
  5. using System.ComponentModel.Composition;
  6. using System.IO;
  7. namespace MediaBrowser.Controller.Resolvers
  8. {
  9. /// <summary>
  10. /// Resolves a Path into a Video
  11. /// </summary>
  12. [Export(typeof(IBaseItemResolver))]
  13. public class VideoResolver : BaseVideoResolver<Video>
  14. {
  15. public override ResolverPriority Priority
  16. {
  17. get { return ResolverPriority.Last; }
  18. }
  19. }
  20. /// <summary>
  21. /// Resolves a Path into a Video or Video subclass
  22. /// </summary>
  23. public abstract class BaseVideoResolver<T> : BaseItemResolver<T>
  24. where T : Video, new()
  25. {
  26. protected override T Resolve(ItemResolveEventArgs args)
  27. {
  28. // If the path is a file check for a matching extensions
  29. if (!args.IsDirectory)
  30. {
  31. if (FileSystemHelper.IsVideoFile(args.Path))
  32. {
  33. VideoType type = Path.GetExtension(args.Path).EndsWith("iso", System.StringComparison.OrdinalIgnoreCase) ? VideoType.Iso : VideoType.VideoFile;
  34. return new T
  35. {
  36. VideoType = type,
  37. Path = args.Path
  38. };
  39. }
  40. }
  41. else
  42. {
  43. // If the path is a folder, check if it's bluray or dvd
  44. T item = ResolveFromFolderName(args.Path);
  45. if (item != null)
  46. {
  47. return item;
  48. }
  49. // Also check the subfolders for bluray or dvd
  50. for (int i = 0; i < args.FileSystemChildren.Length; i++)
  51. {
  52. var folder = args.FileSystemChildren[i];
  53. if (!folder.IsDirectory)
  54. {
  55. continue;
  56. }
  57. item = ResolveFromFolderName(folder.Path);
  58. if (item != null)
  59. {
  60. return item;
  61. }
  62. }
  63. }
  64. return null;
  65. }
  66. private T ResolveFromFolderName(string folder)
  67. {
  68. if (folder.IndexOf("video_ts", System.StringComparison.OrdinalIgnoreCase) != -1)
  69. {
  70. return new T
  71. {
  72. VideoType = VideoType.Dvd,
  73. Path = Path.GetDirectoryName(folder)
  74. };
  75. }
  76. if (folder.IndexOf("bdmv", System.StringComparison.OrdinalIgnoreCase) != -1)
  77. {
  78. return new T
  79. {
  80. VideoType = VideoType.BluRay,
  81. Path = Path.GetDirectoryName(folder)
  82. };
  83. }
  84. return null;
  85. }
  86. }
  87. }