ImageByNameService.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Controller;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Model.Dto;
  6. using ServiceStack;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. namespace MediaBrowser.Api.Images
  12. {
  13. /// <summary>
  14. /// Class GetGeneralImage
  15. /// </summary>
  16. [Route("/Images/General/{Name}/{Type}", "GET")]
  17. [Api(Description = "Gets a general image by name")]
  18. public class GetGeneralImage
  19. {
  20. /// <summary>
  21. /// Gets or sets the name.
  22. /// </summary>
  23. /// <value>The name.</value>
  24. [ApiMember(Name = "Name", Description = "The name of the image", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  25. public string Name { get; set; }
  26. [ApiMember(Name = "Type", Description = "Image Type (primary, backdrop, logo, etc).", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  27. public string Type { get; set; }
  28. }
  29. /// <summary>
  30. /// Class GetRatingImage
  31. /// </summary>
  32. [Route("/Images/Ratings/{Theme}/{Name}", "GET")]
  33. [Api(Description = "Gets a rating image by name")]
  34. public class GetRatingImage
  35. {
  36. /// <summary>
  37. /// Gets or sets the name.
  38. /// </summary>
  39. /// <value>The name.</value>
  40. [ApiMember(Name = "Name", Description = "The name of the image", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  41. public string Name { get; set; }
  42. /// <summary>
  43. /// Gets or sets the theme.
  44. /// </summary>
  45. /// <value>The theme.</value>
  46. [ApiMember(Name = "Theme", Description = "The theme to get the image from", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  47. public string Theme { get; set; }
  48. }
  49. /// <summary>
  50. /// Class GetMediaInfoImage
  51. /// </summary>
  52. [Route("/Images/MediaInfo/{Theme}/{Name}", "GET")]
  53. [Api(Description = "Gets a media info image by name")]
  54. public class GetMediaInfoImage
  55. {
  56. /// <summary>
  57. /// Gets or sets the name.
  58. /// </summary>
  59. /// <value>The name.</value>
  60. [ApiMember(Name = "Name", Description = "The name of the image", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  61. public string Name { get; set; }
  62. /// <summary>
  63. /// Gets or sets the theme.
  64. /// </summary>
  65. /// <value>The theme.</value>
  66. [ApiMember(Name = "Theme", Description = "The theme to get the image from", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  67. public string Theme { get; set; }
  68. }
  69. [Route("/Images/MediaInfo", "GET")]
  70. [Api(Description = "Gets all media info image by name")]
  71. public class GetMediaInfoImages : IReturn<List<ImageByNameInfo>>
  72. {
  73. }
  74. [Route("/Images/Ratings", "GET")]
  75. [Api(Description = "Gets all rating images by name")]
  76. public class GetRatingImages : IReturn<List<ImageByNameInfo>>
  77. {
  78. }
  79. [Route("/Images/General", "GET")]
  80. [Api(Description = "Gets all general images by name")]
  81. public class GetGeneralImages : IReturn<List<ImageByNameInfo>>
  82. {
  83. }
  84. /// <summary>
  85. /// Class ImageByNameService
  86. /// </summary>
  87. public class ImageByNameService : BaseApiService
  88. {
  89. /// <summary>
  90. /// The _app paths
  91. /// </summary>
  92. private readonly IServerApplicationPaths _appPaths;
  93. private readonly IFileSystem _fileSystem;
  94. /// <summary>
  95. /// Initializes a new instance of the <see cref="ImageByNameService" /> class.
  96. /// </summary>
  97. /// <param name="appPaths">The app paths.</param>
  98. public ImageByNameService(IServerApplicationPaths appPaths, IFileSystem fileSystem)
  99. {
  100. _appPaths = appPaths;
  101. _fileSystem = fileSystem;
  102. }
  103. public object Get(GetMediaInfoImages request)
  104. {
  105. return ToOptimizedResult(GetImageList(_appPaths.MediaInfoImagesPath, true));
  106. }
  107. public object Get(GetRatingImages request)
  108. {
  109. return ToOptimizedResult(GetImageList(_appPaths.RatingsPath, true));
  110. }
  111. public object Get(GetGeneralImages request)
  112. {
  113. return ToOptimizedResult(GetImageList(_appPaths.GeneralPath, false));
  114. }
  115. private List<ImageByNameInfo> GetImageList(string path, bool supportsThemes)
  116. {
  117. try
  118. {
  119. return new DirectoryInfo(path)
  120. .GetFiles("*", SearchOption.AllDirectories)
  121. .Where(i => BaseItem.SupportedImageExtensions.Contains(i.Extension, StringComparer.Ordinal))
  122. .Select(i => new ImageByNameInfo
  123. {
  124. Name = _fileSystem.GetFileNameWithoutExtension(i),
  125. FileLength = i.Length,
  126. // For themeable images, use the Theme property
  127. // For general images, the same object structure is fine,
  128. // but it's not owned by a theme, so call it Context
  129. Theme = supportsThemes ? GetThemeName(i.FullName, path) : null,
  130. Context = supportsThemes ? null : 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. }