ImageByNameService.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Model.Dto;
  5. using ServiceStack;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. namespace MediaBrowser.Api.Images
  11. {
  12. /// <summary>
  13. /// Class GetGeneralImage
  14. /// </summary>
  15. [Route("/Images/General/{Name}/{Type}", "GET")]
  16. [Api(Description = "Gets a general image by name")]
  17. public class GetGeneralImage
  18. {
  19. /// <summary>
  20. /// Gets or sets the name.
  21. /// </summary>
  22. /// <value>The name.</value>
  23. [ApiMember(Name = "Name", Description = "The name of the image", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  24. public string Name { get; set; }
  25. [ApiMember(Name = "Type", Description = "Image Type (primary, backdrop, logo, etc).", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  26. public string Type { get; set; }
  27. }
  28. /// <summary>
  29. /// Class GetRatingImage
  30. /// </summary>
  31. [Route("/Images/Ratings/{Theme}/{Name}", "GET")]
  32. [Api(Description = "Gets a rating image by name")]
  33. public class GetRatingImage
  34. {
  35. /// <summary>
  36. /// Gets or sets the name.
  37. /// </summary>
  38. /// <value>The name.</value>
  39. [ApiMember(Name = "Name", Description = "The name of the image", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  40. public string Name { get; set; }
  41. /// <summary>
  42. /// Gets or sets the theme.
  43. /// </summary>
  44. /// <value>The theme.</value>
  45. [ApiMember(Name = "Theme", Description = "The theme to get the image from", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  46. public string Theme { get; set; }
  47. }
  48. /// <summary>
  49. /// Class GetMediaInfoImage
  50. /// </summary>
  51. [Route("/Images/MediaInfo/{Theme}/{Name}", "GET")]
  52. [Api(Description = "Gets a media info image by name")]
  53. public class GetMediaInfoImage
  54. {
  55. /// <summary>
  56. /// Gets or sets the name.
  57. /// </summary>
  58. /// <value>The name.</value>
  59. [ApiMember(Name = "Name", Description = "The name of the image", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  60. public string Name { get; set; }
  61. /// <summary>
  62. /// Gets or sets the theme.
  63. /// </summary>
  64. /// <value>The theme.</value>
  65. [ApiMember(Name = "Theme", Description = "The theme to get the image from", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  66. public string Theme { get; set; }
  67. }
  68. [Route("/Images/MediaInfo", "GET")]
  69. [Api(Description = "Gets all media info image by name")]
  70. public class GetMediaInfoImages : IReturn<List<ImageByNameInfo>>
  71. {
  72. }
  73. [Route("/Images/Ratings", "GET")]
  74. [Api(Description = "Gets all rating images by name")]
  75. public class GetRatingImages : IReturn<List<ImageByNameInfo>>
  76. {
  77. }
  78. [Route("/Images/General", "GET")]
  79. [Api(Description = "Gets all general images by name")]
  80. public class GetGeneralImages : IReturn<List<ImageByNameInfo>>
  81. {
  82. }
  83. /// <summary>
  84. /// Class ImageByNameService
  85. /// </summary>
  86. public class ImageByNameService : BaseApiService
  87. {
  88. /// <summary>
  89. /// The _app paths
  90. /// </summary>
  91. private readonly IServerApplicationPaths _appPaths;
  92. /// <summary>
  93. /// Initializes a new instance of the <see cref="ImageByNameService" /> class.
  94. /// </summary>
  95. /// <param name="appPaths">The app paths.</param>
  96. public ImageByNameService(IServerApplicationPaths appPaths)
  97. {
  98. _appPaths = appPaths;
  99. }
  100. public object Get(GetMediaInfoImages request)
  101. {
  102. return ToOptimizedResult(GetImageList(_appPaths.MediaInfoImagesPath, true));
  103. }
  104. public object Get(GetRatingImages request)
  105. {
  106. return ToOptimizedResult(GetImageList(_appPaths.RatingsPath, true));
  107. }
  108. public object Get(GetGeneralImages request)
  109. {
  110. return ToOptimizedResult(GetImageList(_appPaths.GeneralPath, false));
  111. }
  112. private List<ImageByNameInfo> GetImageList(string path, bool supportsThemes)
  113. {
  114. try
  115. {
  116. return new DirectoryInfo(path)
  117. .GetFiles("*", SearchOption.AllDirectories)
  118. .Where(i => BaseItem.SupportedImageExtensions.Contains(i.Extension, StringComparer.Ordinal))
  119. .Select(i => new ImageByNameInfo
  120. {
  121. Name = Path.GetFileNameWithoutExtension(i.FullName),
  122. FileLength = i.Length,
  123. // For themeable images, use the Theme property
  124. // For general images, the same object structure is fine,
  125. // but it's not owned by a theme, so call it Context
  126. Theme = supportsThemes ? GetThemeName(i.FullName, path) : null,
  127. Context = supportsThemes ? null : GetThemeName(i.FullName, path),
  128. Format = i.Extension.ToLower().TrimStart('.')
  129. })
  130. .OrderBy(i => i.Name)
  131. .ToList();
  132. }
  133. catch (DirectoryNotFoundException)
  134. {
  135. return new List<ImageByNameInfo>();
  136. }
  137. }
  138. private string GetThemeName(string path, string rootImagePath)
  139. {
  140. var parentName = Path.GetDirectoryName(path);
  141. if (string.Equals(parentName, rootImagePath, StringComparison.OrdinalIgnoreCase))
  142. {
  143. return null;
  144. }
  145. parentName = Path.GetFileName(parentName);
  146. return string.Equals(parentName, "all", StringComparison.OrdinalIgnoreCase) ?
  147. null :
  148. parentName;
  149. }
  150. /// <summary>
  151. /// Gets the specified request.
  152. /// </summary>
  153. /// <param name="request">The request.</param>
  154. /// <returns>System.Object.</returns>
  155. public object Get(GetGeneralImage request)
  156. {
  157. var filename = string.Equals(request.Type, "primary", StringComparison.OrdinalIgnoreCase)
  158. ? "folder"
  159. : request.Type;
  160. var paths = BaseItem.SupportedImageExtensions.Select(i => Path.Combine(_appPaths.GeneralPath, request.Name, filename + i)).ToList();
  161. var path = paths.FirstOrDefault(File.Exists) ?? paths.FirstOrDefault();
  162. return ToStaticFileResult(path);
  163. }
  164. /// <summary>
  165. /// Gets the specified request.
  166. /// </summary>
  167. /// <param name="request">The request.</param>
  168. /// <returns>System.Object.</returns>
  169. public object Get(GetRatingImage request)
  170. {
  171. var themeFolder = Path.Combine(_appPaths.RatingsPath, request.Theme);
  172. if (Directory.Exists(themeFolder))
  173. {
  174. var path = BaseItem.SupportedImageExtensions
  175. .Select(i => Path.Combine(themeFolder, request.Name + i))
  176. .FirstOrDefault(File.Exists);
  177. if (!string.IsNullOrEmpty(path))
  178. {
  179. return ToStaticFileResult(path);
  180. }
  181. }
  182. var allFolder = Path.Combine(_appPaths.RatingsPath, "all");
  183. if (Directory.Exists(allFolder))
  184. {
  185. // Avoid implicitly captured closure
  186. var currentRequest = request;
  187. var path = BaseItem.SupportedImageExtensions
  188. .Select(i => Path.Combine(allFolder, currentRequest.Name + i))
  189. .FirstOrDefault(File.Exists);
  190. if (!string.IsNullOrEmpty(path))
  191. {
  192. return ToStaticFileResult(path);
  193. }
  194. }
  195. throw new ResourceNotFoundException("MediaInfo image not found: " + request.Name);
  196. }
  197. /// <summary>
  198. /// Gets the specified request.
  199. /// </summary>
  200. /// <param name="request">The request.</param>
  201. /// <returns>System.Object.</returns>
  202. public object Get(GetMediaInfoImage request)
  203. {
  204. var themeFolder = Path.Combine(_appPaths.MediaInfoImagesPath, request.Theme);
  205. if (Directory.Exists(themeFolder))
  206. {
  207. var path = BaseItem.SupportedImageExtensions.Select(i => Path.Combine(themeFolder, request.Name + i))
  208. .FirstOrDefault(File.Exists);
  209. if (!string.IsNullOrEmpty(path))
  210. {
  211. return ToStaticFileResult(path);
  212. }
  213. }
  214. var allFolder = Path.Combine(_appPaths.MediaInfoImagesPath, "all");
  215. if (Directory.Exists(allFolder))
  216. {
  217. // Avoid implicitly captured closure
  218. var currentRequest = request;
  219. var path = BaseItem.SupportedImageExtensions.Select(i => Path.Combine(allFolder, currentRequest.Name + i))
  220. .FirstOrDefault(File.Exists);
  221. if (!string.IsNullOrEmpty(path))
  222. {
  223. return ToStaticFileResult(path);
  224. }
  225. }
  226. throw new ResourceNotFoundException("MediaInfo image not found: " + request.Name);
  227. }
  228. }
  229. }