ImageByNameService.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Common.Extensions;
  7. using MediaBrowser.Controller;
  8. using MediaBrowser.Controller.Entities;
  9. using MediaBrowser.Controller.Net;
  10. using MediaBrowser.Model.Dto;
  11. using MediaBrowser.Model.IO;
  12. using MediaBrowser.Model.Services;
  13. namespace MediaBrowser.Api.Images
  14. {
  15. /// <summary>
  16. /// Class GetGeneralImage
  17. /// </summary>
  18. [Route("/Images/General/{Name}/{Type}", "GET", Summary = "Gets a general image by name")]
  19. public class GetGeneralImage
  20. {
  21. /// <summary>
  22. /// Gets or sets the name.
  23. /// </summary>
  24. /// <value>The name.</value>
  25. [ApiMember(Name = "Name", Description = "The name of the image", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  26. public string Name { get; set; }
  27. [ApiMember(Name = "Type", Description = "Image Type (primary, backdrop, logo, etc).", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  28. public string Type { get; set; }
  29. }
  30. /// <summary>
  31. /// Class GetRatingImage
  32. /// </summary>
  33. [Route("/Images/Ratings/{Theme}/{Name}", "GET", Summary = "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", Summary = "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", Summary = "Gets all media info image by name")]
  69. [Authenticated]
  70. public class GetMediaInfoImages : IReturn<List<ImageByNameInfo>>
  71. {
  72. }
  73. [Route("/Images/Ratings", "GET", Summary = "Gets all rating images by name")]
  74. [Authenticated]
  75. public class GetRatingImages : IReturn<List<ImageByNameInfo>>
  76. {
  77. }
  78. [Route("/Images/General", "GET", Summary = "Gets all general images by name")]
  79. [Authenticated]
  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. private readonly IFileSystem _fileSystem;
  93. private readonly IHttpResultFactory _resultFactory;
  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, IHttpResultFactory resultFactory)
  99. {
  100. _appPaths = appPaths;
  101. _fileSystem = fileSystem;
  102. _resultFactory = resultFactory;
  103. }
  104. public object Get(GetMediaInfoImages request)
  105. {
  106. return ToOptimizedResult(GetImageList(_appPaths.MediaInfoImagesPath, true));
  107. }
  108. public object Get(GetRatingImages request)
  109. {
  110. return ToOptimizedResult(GetImageList(_appPaths.RatingsPath, true));
  111. }
  112. public object Get(GetGeneralImages request)
  113. {
  114. return ToOptimizedResult(GetImageList(_appPaths.GeneralPath, false));
  115. }
  116. private List<ImageByNameInfo> GetImageList(string path, bool supportsThemes)
  117. {
  118. try
  119. {
  120. return _fileSystem.GetFiles(path, BaseItem.SupportedImageExtensions, false, true)
  121. .Select(i => new ImageByNameInfo
  122. {
  123. Name = _fileSystem.GetFileNameWithoutExtension(i),
  124. FileLength = i.Length,
  125. // For themeable images, use the Theme property
  126. // For general images, the same object structure is fine,
  127. // but it's not owned by a theme, so call it Context
  128. Theme = supportsThemes ? GetThemeName(i.FullName, path) : null,
  129. Context = supportsThemes ? null : GetThemeName(i.FullName, path),
  130. Format = i.Extension.ToLowerInvariant().TrimStart('.')
  131. })
  132. .OrderBy(i => i.Name)
  133. .ToList();
  134. }
  135. catch (IOException)
  136. {
  137. return new List<ImageByNameInfo>();
  138. }
  139. }
  140. private string GetThemeName(string path, string rootImagePath)
  141. {
  142. var parentName = _fileSystem.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 Task<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(_fileSystem.FileExists) ?? paths.FirstOrDefault();
  164. return _resultFactory.GetStaticFileResult(Request, 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 (_fileSystem.DirectoryExists(themeFolder))
  175. {
  176. var path = BaseItem.SupportedImageExtensions
  177. .Select(i => Path.Combine(themeFolder, request.Name + i))
  178. .FirstOrDefault(_fileSystem.FileExists);
  179. if (!string.IsNullOrEmpty(path))
  180. {
  181. return _resultFactory.GetStaticFileResult(Request, path);
  182. }
  183. }
  184. var allFolder = Path.Combine(_appPaths.RatingsPath, "all");
  185. if (_fileSystem.DirectoryExists(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(_fileSystem.FileExists);
  192. if (!string.IsNullOrEmpty(path))
  193. {
  194. return _resultFactory.GetStaticFileResult(Request, 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 Task<object> Get(GetMediaInfoImage request)
  205. {
  206. var themeFolder = Path.Combine(_appPaths.MediaInfoImagesPath, request.Theme);
  207. if (_fileSystem.DirectoryExists(themeFolder))
  208. {
  209. var path = BaseItem.SupportedImageExtensions.Select(i => Path.Combine(themeFolder, request.Name + i))
  210. .FirstOrDefault(_fileSystem.FileExists);
  211. if (!string.IsNullOrEmpty(path))
  212. {
  213. return _resultFactory.GetStaticFileResult(Request, path);
  214. }
  215. }
  216. var allFolder = Path.Combine(_appPaths.MediaInfoImagesPath, "all");
  217. if (_fileSystem.DirectoryExists(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(_fileSystem.FileExists);
  223. if (!string.IsNullOrEmpty(path))
  224. {
  225. return _resultFactory.GetStaticFileResult(Request, path);
  226. }
  227. }
  228. throw new ResourceNotFoundException("MediaInfo image not found: " + request.Name);
  229. }
  230. }
  231. }