ImageByNameService.cs 9.8 KB

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