2
0

ImageByNameService.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Controller;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Net;
  6. using MediaBrowser.Model.Dto;
  7. using ServiceStack;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  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 new DirectoryInfo(path)
  118. .GetFiles("*", SearchOption.AllDirectories)
  119. .Where(i => BaseItem.SupportedImageExtensions.Contains(i.Extension, StringComparer.Ordinal))
  120. .Select(i => new ImageByNameInfo
  121. {
  122. Name = _fileSystem.GetFileNameWithoutExtension(i),
  123. FileLength = i.Length,
  124. // For themeable images, use the Theme property
  125. // For general images, the same object structure is fine,
  126. // but it's not owned by a theme, so call it Context
  127. Theme = supportsThemes ? GetThemeName(i.FullName, path) : null,
  128. Context = supportsThemes ? null : GetThemeName(i.FullName, path),
  129. Format = i.Extension.ToLower().TrimStart('.')
  130. })
  131. .OrderBy(i => i.Name)
  132. .ToList();
  133. }
  134. catch (DirectoryNotFoundException)
  135. {
  136. return new List<ImageByNameInfo>();
  137. }
  138. }
  139. private string GetThemeName(string path, string rootImagePath)
  140. {
  141. var parentName = Path.GetDirectoryName(path);
  142. if (string.Equals(parentName, rootImagePath, StringComparison.OrdinalIgnoreCase))
  143. {
  144. return null;
  145. }
  146. parentName = Path.GetFileName(parentName);
  147. return string.Equals(parentName, "all", StringComparison.OrdinalIgnoreCase) ?
  148. null :
  149. parentName;
  150. }
  151. /// <summary>
  152. /// Gets the specified request.
  153. /// </summary>
  154. /// <param name="request">The request.</param>
  155. /// <returns>System.Object.</returns>
  156. public object Get(GetGeneralImage request)
  157. {
  158. var filename = string.Equals(request.Type, "primary", StringComparison.OrdinalIgnoreCase)
  159. ? "folder"
  160. : request.Type;
  161. var paths = BaseItem.SupportedImageExtensions.Select(i => Path.Combine(_appPaths.GeneralPath, request.Name, filename + i)).ToList();
  162. var path = paths.FirstOrDefault(File.Exists) ?? paths.FirstOrDefault();
  163. return ToStaticFileResult(path);
  164. }
  165. /// <summary>
  166. /// Gets the specified request.
  167. /// </summary>
  168. /// <param name="request">The request.</param>
  169. /// <returns>System.Object.</returns>
  170. public object Get(GetRatingImage request)
  171. {
  172. var themeFolder = Path.Combine(_appPaths.RatingsPath, request.Theme);
  173. if (Directory.Exists(themeFolder))
  174. {
  175. var path = BaseItem.SupportedImageExtensions
  176. .Select(i => Path.Combine(themeFolder, request.Name + i))
  177. .FirstOrDefault(File.Exists);
  178. if (!string.IsNullOrEmpty(path))
  179. {
  180. return ToStaticFileResult(path);
  181. }
  182. }
  183. var allFolder = Path.Combine(_appPaths.RatingsPath, "all");
  184. if (Directory.Exists(allFolder))
  185. {
  186. // Avoid implicitly captured closure
  187. var currentRequest = request;
  188. var path = BaseItem.SupportedImageExtensions
  189. .Select(i => Path.Combine(allFolder, currentRequest.Name + i))
  190. .FirstOrDefault(File.Exists);
  191. if (!string.IsNullOrEmpty(path))
  192. {
  193. return ToStaticFileResult(path);
  194. }
  195. }
  196. throw new ResourceNotFoundException("MediaInfo image not found: " + request.Name);
  197. }
  198. /// <summary>
  199. /// Gets the specified request.
  200. /// </summary>
  201. /// <param name="request">The request.</param>
  202. /// <returns>System.Object.</returns>
  203. public object Get(GetMediaInfoImage request)
  204. {
  205. var themeFolder = Path.Combine(_appPaths.MediaInfoImagesPath, request.Theme);
  206. if (Directory.Exists(themeFolder))
  207. {
  208. var path = BaseItem.SupportedImageExtensions.Select(i => Path.Combine(themeFolder, request.Name + i))
  209. .FirstOrDefault(File.Exists);
  210. if (!string.IsNullOrEmpty(path))
  211. {
  212. return ToStaticFileResult(path);
  213. }
  214. }
  215. var allFolder = Path.Combine(_appPaths.MediaInfoImagesPath, "all");
  216. if (Directory.Exists(allFolder))
  217. {
  218. // Avoid implicitly captured closure
  219. var currentRequest = request;
  220. var path = BaseItem.SupportedImageExtensions.Select(i => Path.Combine(allFolder, currentRequest.Name + i))
  221. .FirstOrDefault(File.Exists);
  222. if (!string.IsNullOrEmpty(path))
  223. {
  224. return ToStaticFileResult(path);
  225. }
  226. }
  227. throw new ResourceNotFoundException("MediaInfo image not found: " + request.Name);
  228. }
  229. }
  230. }