BaseVideoResolver.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Controller.Providers;
  4. using MediaBrowser.Model.Entities;
  5. using MediaBrowser.Naming.Common;
  6. using MediaBrowser.Naming.Video;
  7. using System;
  8. using System.IO;
  9. using System.Linq;
  10. using MediaBrowser.Server.Implementations.Logging;
  11. namespace MediaBrowser.Server.Implementations.Library.Resolvers
  12. {
  13. /// <summary>
  14. /// Resolves a Path into a Video or Video subclass
  15. /// </summary>
  16. /// <typeparam name="T"></typeparam>
  17. public abstract class BaseVideoResolver<T> : Controller.Resolvers.ItemResolver<T>
  18. where T : Video, new()
  19. {
  20. protected readonly ILibraryManager LibraryManager;
  21. protected BaseVideoResolver(ILibraryManager libraryManager)
  22. {
  23. LibraryManager = libraryManager;
  24. }
  25. /// <summary>
  26. /// Resolves the specified args.
  27. /// </summary>
  28. /// <param name="args">The args.</param>
  29. /// <returns>`0.</returns>
  30. protected override T Resolve(ItemResolveArgs args)
  31. {
  32. return ResolveVideo<T>(args, false);
  33. }
  34. /// <summary>
  35. /// Resolves the video.
  36. /// </summary>
  37. /// <typeparam name="TVideoType">The type of the T video type.</typeparam>
  38. /// <param name="args">The args.</param>
  39. /// <param name="parseName">if set to <c>true</c> [parse name].</param>
  40. /// <returns>``0.</returns>
  41. protected TVideoType ResolveVideo<TVideoType>(ItemResolveArgs args, bool parseName)
  42. where TVideoType : Video, new()
  43. {
  44. var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();
  45. // If the path is a file check for a matching extensions
  46. var parser = new Naming.Video.VideoResolver(namingOptions, new PatternsLogger());
  47. if (args.IsDirectory)
  48. {
  49. TVideoType video = null;
  50. VideoFileInfo videoInfo = null;
  51. // Loop through each child file/folder and see if we find a video
  52. foreach (var child in args.FileSystemChildren)
  53. {
  54. var filename = child.Name;
  55. if ((child.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
  56. {
  57. if (IsDvdDirectory(filename))
  58. {
  59. videoInfo = parser.ResolveDirectory(args.Path);
  60. if (videoInfo == null)
  61. {
  62. return null;
  63. }
  64. video = new TVideoType
  65. {
  66. Path = args.Path,
  67. VideoType = VideoType.Dvd,
  68. ProductionYear = videoInfo.Year
  69. };
  70. break;
  71. }
  72. if (IsBluRayDirectory(filename))
  73. {
  74. videoInfo = parser.ResolveDirectory(args.Path);
  75. if (videoInfo == null)
  76. {
  77. return null;
  78. }
  79. video = new TVideoType
  80. {
  81. Path = args.Path,
  82. VideoType = VideoType.BluRay,
  83. ProductionYear = videoInfo.Year
  84. };
  85. break;
  86. }
  87. }
  88. else if (IsDvdFile(filename))
  89. {
  90. videoInfo = parser.ResolveDirectory(args.Path);
  91. if (videoInfo == null)
  92. {
  93. return null;
  94. }
  95. video = new TVideoType
  96. {
  97. Path = args.Path,
  98. VideoType = VideoType.Dvd,
  99. ProductionYear = videoInfo.Year
  100. };
  101. break;
  102. }
  103. }
  104. if (video != null)
  105. {
  106. video.Name = parseName ?
  107. videoInfo.Name :
  108. Path.GetFileName(args.Path);
  109. Set3DFormat(video, videoInfo);
  110. }
  111. return video;
  112. }
  113. else
  114. {
  115. var videoInfo = parser.ResolveFile(args.Path);
  116. if (videoInfo == null)
  117. {
  118. return null;
  119. }
  120. if (LibraryManager.IsVideoFile(args.Path) || videoInfo.IsStub)
  121. {
  122. var path = args.Path;
  123. var video = new TVideoType
  124. {
  125. Path = path,
  126. IsInMixedFolder = true,
  127. ProductionYear = videoInfo.Year
  128. };
  129. SetVideoType(video, videoInfo);
  130. video.Name = parseName ?
  131. videoInfo.Name :
  132. Path.GetFileNameWithoutExtension(args.Path);
  133. Set3DFormat(video, videoInfo);
  134. return video;
  135. }
  136. }
  137. return null;
  138. }
  139. protected void SetVideoType(Video video, VideoFileInfo videoInfo)
  140. {
  141. var extension = Path.GetExtension(video.Path);
  142. video.VideoType = string.Equals(extension, ".iso", StringComparison.OrdinalIgnoreCase) ||
  143. string.Equals(extension, ".img", StringComparison.OrdinalIgnoreCase) ?
  144. VideoType.Iso :
  145. VideoType.VideoFile;
  146. video.IsShortcut = string.Equals(extension, ".strm", StringComparison.OrdinalIgnoreCase);
  147. video.IsPlaceHolder = videoInfo.IsStub;
  148. if (videoInfo.IsStub)
  149. {
  150. if (string.Equals(videoInfo.StubType, "dvd", StringComparison.OrdinalIgnoreCase))
  151. {
  152. video.VideoType = VideoType.Dvd;
  153. }
  154. else if (string.Equals(videoInfo.StubType, "hddvd", StringComparison.OrdinalIgnoreCase))
  155. {
  156. video.VideoType = VideoType.HdDvd;
  157. }
  158. else if (string.Equals(videoInfo.StubType, "bluray", StringComparison.OrdinalIgnoreCase))
  159. {
  160. video.VideoType = VideoType.BluRay;
  161. }
  162. }
  163. }
  164. protected void Set3DFormat(Video video, bool is3D, string format3D)
  165. {
  166. if (is3D)
  167. {
  168. if (string.Equals(format3D, "fsbs", StringComparison.OrdinalIgnoreCase))
  169. {
  170. video.Video3DFormat = Video3DFormat.FullSideBySide;
  171. }
  172. else if (string.Equals(format3D, "ftab", StringComparison.OrdinalIgnoreCase))
  173. {
  174. video.Video3DFormat = Video3DFormat.FullTopAndBottom;
  175. }
  176. else if (string.Equals(format3D, "hsbs", StringComparison.OrdinalIgnoreCase))
  177. {
  178. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  179. }
  180. else if (string.Equals(format3D, "htab", StringComparison.OrdinalIgnoreCase))
  181. {
  182. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  183. }
  184. else if (string.Equals(format3D, "sbs", StringComparison.OrdinalIgnoreCase))
  185. {
  186. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  187. }
  188. else if (string.Equals(format3D, "sbs3d", StringComparison.OrdinalIgnoreCase))
  189. {
  190. video.Video3DFormat = Video3DFormat.HalfSideBySide;
  191. }
  192. else if (string.Equals(format3D, "tab", StringComparison.OrdinalIgnoreCase))
  193. {
  194. video.Video3DFormat = Video3DFormat.HalfTopAndBottom;
  195. }
  196. }
  197. }
  198. protected void Set3DFormat(Video video, VideoFileInfo videoInfo)
  199. {
  200. Set3DFormat(video, videoInfo.Is3D, videoInfo.Format3D);
  201. }
  202. protected void Set3DFormat(Video video)
  203. {
  204. var namingOptions = ((LibraryManager)LibraryManager).GetNamingOptions();
  205. var resolver = new Format3DParser(namingOptions, new PatternsLogger());
  206. var result = resolver.Parse(video.Path);
  207. Set3DFormat(video, result.Is3D, result.Format3D);
  208. }
  209. /// <summary>
  210. /// Determines whether [is DVD directory] [the specified directory name].
  211. /// </summary>
  212. /// <param name="directoryName">Name of the directory.</param>
  213. /// <returns><c>true</c> if [is DVD directory] [the specified directory name]; otherwise, <c>false</c>.</returns>
  214. protected bool IsDvdDirectory(string directoryName)
  215. {
  216. return string.Equals(directoryName, "video_ts", StringComparison.OrdinalIgnoreCase);
  217. }
  218. /// <summary>
  219. /// Determines whether [is DVD file] [the specified name].
  220. /// </summary>
  221. /// <param name="name">The name.</param>
  222. /// <returns><c>true</c> if [is DVD file] [the specified name]; otherwise, <c>false</c>.</returns>
  223. protected bool IsDvdFile(string name)
  224. {
  225. return string.Equals(name, "video_ts.ifo", StringComparison.OrdinalIgnoreCase);
  226. }
  227. /// <summary>
  228. /// Determines whether [is blu ray directory] [the specified directory name].
  229. /// </summary>
  230. /// <param name="directoryName">Name of the directory.</param>
  231. /// <returns><c>true</c> if [is blu ray directory] [the specified directory name]; otherwise, <c>false</c>.</returns>
  232. protected bool IsBluRayDirectory(string directoryName)
  233. {
  234. return string.Equals(directoryName, "bdmv", StringComparison.OrdinalIgnoreCase);
  235. }
  236. }
  237. }