ImageByNameService.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 ServiceStack;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using CommonIO;
  12. namespace MediaBrowser.Api.Images
  13. {
  14. /// <summary>
  15. /// Class GetGeneralImage
  16. /// </summary>
  17. [Route("/Images/General/{Name}/{Type}", "GET", Summary = "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", Summary = "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", Summary = "Gets a media info image by name")]
  52. public class GetMediaInfoImage
  53. {
  54. /// <summary>
  55. /// Gets or sets the name.
  56. /// </summary>
  57. /// <value>The name.</value>
  58. [ApiMember(Name = "Name", Description = "The name of the image", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  59. public string Name { get; set; }
  60. /// <summary>
  61. /// Gets or sets the theme.
  62. /// </summary>
  63. /// <value>The theme.</value>
  64. [ApiMember(Name = "Theme", Description = "The theme to get the image from", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  65. public string Theme { get; set; }
  66. }
  67. [Route("/Images/MediaInfo", "GET", Summary = "Gets all media info image by name")]
  68. [Authenticated]
  69. public class GetMediaInfoImages : IReturn<List<ImageByNameInfo>>
  70. {
  71. }
  72. [Route("/Images/Ratings", "GET", Summary = "Gets all rating images by name")]
  73. [Authenticated]
  74. public class GetRatingImages : IReturn<List<ImageByNameInfo>>
  75. {
  76. }
  77. [Route("/Images/General", "GET", Summary = "Gets all general images by name")]
  78. [Authenticated]
  79. public class GetGeneralImages : IReturn<List<ImageByNameInfo>>
  80. {
  81. }
  82. /// <summary>
  83. /// Class ImageByNameService
  84. /// </summary>
  85. public class ImageByNameService : BaseApiService
  86. {
  87. /// <summary>
  88. /// The _app paths
  89. /// </summary>
  90. private readonly IServerApplicationPaths _appPaths;
  91. private readonly IFileSystem _fileSystem;
  92. /// <summary>
  93. /// Initializes a new instance of the <see cref="ImageByNameService" /> class.
  94. /// </summary>
  95. /// <param name="appPaths">The app paths.</param>
  96. public ImageByNameService(IServerApplicationPaths appPaths, IFileSystem fileSystem)
  97. {
  98. _appPaths = appPaths;
  99. _fileSystem = fileSystem;
  100. }
  101. public object Get(GetMediaInfoImages request)
  102. {
  103. return ToOptimizedResult(GetImageList(_appPaths.MediaInfoImagesPath, true));
  104. }
  105. public object Get(GetRatingImages request)
  106. {
  107. return ToOptimizedResult(GetImageList(_appPaths.RatingsPath, true));
  108. }
  109. public object Get(GetGeneralImages request)
  110. {
  111. return ToOptimizedResult(GetImageList(_appPaths.GeneralPath, false));
  112. }
  113. private List<ImageByNameInfo> GetImageList(string path, bool supportsThemes)
  114. {
  115. try
  116. {
  117. return _fileSystem.GetFiles(path, true)
  118. .Where(i => BaseItem.SupportedImageExtensions.Contains(i.Extension, StringComparer.Ordinal))
  119. .Select(i => new ImageByNameInfo
  120. {
  121. Name = _fileSystem.GetFileNameWithoutExtension(i),
  122. FileLength = i.Length,
  123. // For themeable images, use the Theme property
  124. // For general images, the same object structure is fine,
  125. // but it's not owned by a theme, so call it Context
  126. Theme = supportsThemes ? GetThemeName(i.FullName, path) : null,
  127. Context = supportsThemes ? null : GetThemeName(i.FullName, path),
  128. Format = i.Extension.ToLower().TrimStart('.')
  129. })
  130. .OrderBy(i => i.Name)
  131. .ToList();
  132. }
  133. catch (DirectoryNotFoundException)
  134. {
  135. return new List<ImageByNameInfo>();
  136. }
  137. }
  138. private string GetThemeName(string path, string rootImagePath)
  139. {
  140. var parentName = Path.GetDirectoryName(path);
  141. if (string.Equals(parentName, rootImagePath, StringComparison.OrdinalIgnoreCase))
  142. {
  143. return null;
  144. }
  145. parentName = Path.GetFileName(parentName);
  146. return string.Equals(parentName, "all", StringComparison.OrdinalIgnoreCase) ?
  147. null :
  148. parentName;
  149. }
  150. /// <summary>
  151. /// Gets the specified request.
  152. /// </summary>
  153. /// <param name="request">The request.</param>
  154. /// <returns>System.Object.</returns>
  155. public object Get(GetGeneralImage request)
  156. {
  157. var filename = string.Equals(request.Type, "primary", StringComparison.OrdinalIgnoreCase)
  158. ? "folder"
  159. : request.Type;
  160. var paths = BaseItem.SupportedImageExtensions.Select(i => Path.Combine(_appPaths.GeneralPath, request.Name, filename + i)).ToList();
  161. var path = paths.FirstOrDefault(_fileSystem.FileExists) ?? paths.FirstOrDefault();
  162. return ToStaticFileResult(path);
  163. }
  164. /// <summary>
  165. /// Gets the specified request.
  166. /// </summary>
  167. /// <param name="request">The request.</param>
  168. /// <returns>System.Object.</returns>
  169. public object Get(GetRatingImage request)
  170. {
  171. var themeFolder = Path.Combine(_appPaths.RatingsPath, request.Theme);
  172. if (_fileSystem.DirectoryExists(themeFolder))
  173. {
  174. var path = BaseItem.SupportedImageExtensions
  175. .Select(i => Path.Combine(themeFolder, request.Name + i))
  176. .FirstOrDefault(_fileSystem.FileExists);
  177. if (!string.IsNullOrEmpty(path))
  178. {
  179. return ToStaticFileResult(path);
  180. }
  181. }
  182. var allFolder = Path.Combine(_appPaths.RatingsPath, "all");
  183. if (_fileSystem.DirectoryExists(allFolder))
  184. {
  185. // Avoid implicitly captured closure
  186. var currentRequest = request;
  187. var path = BaseItem.SupportedImageExtensions
  188. .Select(i => Path.Combine(allFolder, currentRequest.Name + i))
  189. .FirstOrDefault(_fileSystem.FileExists);
  190. if (!string.IsNullOrEmpty(path))
  191. {
  192. return ToStaticFileResult(path);
  193. }
  194. }
  195. throw new ResourceNotFoundException("MediaInfo image not found: " + request.Name);
  196. }
  197. /// <summary>
  198. /// Gets the specified request.
  199. /// </summary>
  200. /// <param name="request">The request.</param>
  201. /// <returns>System.Object.</returns>
  202. public object Get(GetMediaInfoImage request)
  203. {
  204. var themeFolder = Path.Combine(_appPaths.MediaInfoImagesPath, request.Theme);
  205. if (_fileSystem.DirectoryExists(themeFolder))
  206. {
  207. var path = BaseItem.SupportedImageExtensions.Select(i => Path.Combine(themeFolder, request.Name + i))
  208. .FirstOrDefault(_fileSystem.FileExists);
  209. if (!string.IsNullOrEmpty(path))
  210. {
  211. return ToStaticFileResult(path);
  212. }
  213. }
  214. var allFolder = Path.Combine(_appPaths.MediaInfoImagesPath, "all");
  215. if (_fileSystem.DirectoryExists(allFolder))
  216. {
  217. // Avoid implicitly captured closure
  218. var currentRequest = request;
  219. var path = BaseItem.SupportedImageExtensions.Select(i => Path.Combine(allFolder, currentRequest.Name + i))
  220. .FirstOrDefault(_fileSystem.FileExists);
  221. if (!string.IsNullOrEmpty(path))
  222. {
  223. return ToStaticFileResult(path);
  224. }
  225. }
  226. throw new ResourceNotFoundException("MediaInfo image not found: " + request.Name);
  227. }
  228. }
  229. }