ImageByNameService.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Controller;
  4. using MediaBrowser.Controller.Entities;
  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. public class ImageByNameInfo
  84. {
  85. public string Name { get; set; }
  86. public string Theme { get; set; }
  87. public long FileLength { get; set; }
  88. public string Format { get; set; }
  89. }
  90. /// <summary>
  91. /// Class ImageByNameService
  92. /// </summary>
  93. public class ImageByNameService : BaseApiService
  94. {
  95. /// <summary>
  96. /// The _app paths
  97. /// </summary>
  98. private readonly IServerApplicationPaths _appPaths;
  99. /// <summary>
  100. /// Initializes a new instance of the <see cref="ImageByNameService" /> class.
  101. /// </summary>
  102. /// <param name="appPaths">The app paths.</param>
  103. public ImageByNameService(IServerApplicationPaths appPaths)
  104. {
  105. _appPaths = appPaths;
  106. }
  107. public object Get(GetMediaInfoImages request)
  108. {
  109. return ToOptimizedResult(GetImageList(_appPaths.MediaInfoImagesPath));
  110. }
  111. public object Get(GetRatingImages request)
  112. {
  113. return ToOptimizedResult(GetImageList(_appPaths.RatingsPath));
  114. }
  115. public object Get(GetGeneralImages request)
  116. {
  117. return ToOptimizedResult(GetImageList(_appPaths.GeneralPath));
  118. }
  119. private List<ImageByNameInfo> GetImageList(string path)
  120. {
  121. try
  122. {
  123. return new DirectoryInfo(path)
  124. .GetFiles("*", SearchOption.AllDirectories)
  125. .Where(i => BaseItem.SupportedImageExtensions.Contains(i.Extension, StringComparer.Ordinal))
  126. .Select(i => new ImageByNameInfo
  127. {
  128. Name = Path.GetFileNameWithoutExtension(i.FullName),
  129. FileLength = i.Length,
  130. Theme = GetThemeName(i.FullName, path),
  131. Format = i.Extension.ToLower().TrimStart('.')
  132. })
  133. .OrderBy(i => i.Name)
  134. .ToList();
  135. }
  136. catch (DirectoryNotFoundException)
  137. {
  138. return new List<ImageByNameInfo>();
  139. }
  140. }
  141. private string GetThemeName(string path, string rootImagePath)
  142. {
  143. var parentName = Path.GetDirectoryName(path);
  144. if (string.Equals(parentName, rootImagePath, StringComparison.OrdinalIgnoreCase))
  145. {
  146. return null;
  147. }
  148. parentName = Path.GetFileName(parentName);
  149. return string.Equals(parentName, "all", StringComparison.OrdinalIgnoreCase) ?
  150. null :
  151. parentName;
  152. }
  153. /// <summary>
  154. /// Gets the specified request.
  155. /// </summary>
  156. /// <param name="request">The request.</param>
  157. /// <returns>System.Object.</returns>
  158. public object Get(GetGeneralImage request)
  159. {
  160. var filename = string.Equals(request.Type, "primary", StringComparison.OrdinalIgnoreCase)
  161. ? "folder"
  162. : request.Type;
  163. var paths = BaseItem.SupportedImageExtensions.Select(i => Path.Combine(_appPaths.GeneralPath, request.Name, filename + i)).ToList();
  164. var path = paths.FirstOrDefault(File.Exists) ?? paths.FirstOrDefault();
  165. return ToStaticFileResult(path);
  166. }
  167. /// <summary>
  168. /// Gets the specified request.
  169. /// </summary>
  170. /// <param name="request">The request.</param>
  171. /// <returns>System.Object.</returns>
  172. public object Get(GetRatingImage request)
  173. {
  174. var themeFolder = Path.Combine(_appPaths.RatingsPath, request.Theme);
  175. if (Directory.Exists(themeFolder))
  176. {
  177. var path = BaseItem.SupportedImageExtensions
  178. .Select(i => Path.Combine(themeFolder, request.Name + i))
  179. .FirstOrDefault(File.Exists);
  180. if (!string.IsNullOrEmpty(path))
  181. {
  182. return ToStaticFileResult(path);
  183. }
  184. }
  185. var allFolder = Path.Combine(_appPaths.RatingsPath, "all");
  186. if (Directory.Exists(allFolder))
  187. {
  188. // Avoid implicitly captured closure
  189. var currentRequest = request;
  190. var path = BaseItem.SupportedImageExtensions
  191. .Select(i => Path.Combine(allFolder, currentRequest.Name + i))
  192. .FirstOrDefault(File.Exists);
  193. if (!string.IsNullOrEmpty(path))
  194. {
  195. return ToStaticFileResult(path);
  196. }
  197. }
  198. throw new ResourceNotFoundException("MediaInfo image not found: " + request.Name);
  199. }
  200. /// <summary>
  201. /// Gets the specified request.
  202. /// </summary>
  203. /// <param name="request">The request.</param>
  204. /// <returns>System.Object.</returns>
  205. public object Get(GetMediaInfoImage request)
  206. {
  207. var themeFolder = Path.Combine(_appPaths.MediaInfoImagesPath, request.Theme);
  208. if (Directory.Exists(themeFolder))
  209. {
  210. var path = BaseItem.SupportedImageExtensions.Select(i => Path.Combine(themeFolder, request.Name + i))
  211. .FirstOrDefault(File.Exists);
  212. if (!string.IsNullOrEmpty(path))
  213. {
  214. return ToStaticFileResult(path);
  215. }
  216. }
  217. var allFolder = Path.Combine(_appPaths.MediaInfoImagesPath, "all");
  218. if (Directory.Exists(allFolder))
  219. {
  220. // Avoid implicitly captured closure
  221. var currentRequest = request;
  222. var path = BaseItem.SupportedImageExtensions.Select(i => Path.Combine(allFolder, currentRequest.Name + i))
  223. .FirstOrDefault(File.Exists);
  224. if (!string.IsNullOrEmpty(path))
  225. {
  226. return ToStaticFileResult(path);
  227. }
  228. }
  229. throw new ResourceNotFoundException("MediaInfo image not found: " + request.Name);
  230. }
  231. }
  232. }