ImageByNameService.cs 9.9 KB

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