ImageByNameService.cs 9.3 KB

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