ImageByNameService.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Entities;
  4. using ServiceStack.ServiceHost;
  5. using System;
  6. using System.IO;
  7. using System.Linq;
  8. namespace MediaBrowser.Api.Images
  9. {
  10. /// <summary>
  11. /// Class GetGeneralImage
  12. /// </summary>
  13. [Route("/Images/General/{Name}/{Type}", "GET")]
  14. [Api(Description = "Gets a general image by name")]
  15. public class GetGeneralImage
  16. {
  17. /// <summary>
  18. /// Gets or sets the name.
  19. /// </summary>
  20. /// <value>The name.</value>
  21. [ApiMember(Name = "Name", Description = "The name of the image", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  22. public string Name { get; set; }
  23. [ApiMember(Name = "Type", Description = "Image Type (primary, backdrop, logo, etc).", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  24. public string Type { get; set; }
  25. }
  26. /// <summary>
  27. /// Class GetRatingImage
  28. /// </summary>
  29. [Route("/Images/Ratings/{Theme}/{Name}", "GET")]
  30. [Api(Description = "Gets a rating image by name")]
  31. public class GetRatingImage
  32. {
  33. /// <summary>
  34. /// Gets or sets the name.
  35. /// </summary>
  36. /// <value>The name.</value>
  37. [ApiMember(Name = "Name", Description = "The name of the image", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  38. public string Name { get; set; }
  39. /// <summary>
  40. /// Gets or sets the theme.
  41. /// </summary>
  42. /// <value>The theme.</value>
  43. [ApiMember(Name = "Theme", Description = "The theme to get the image from", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  44. public string Theme { get; set; }
  45. }
  46. /// <summary>
  47. /// Class GetMediaInfoImage
  48. /// </summary>
  49. [Route("/Images/MediaInfo/{Theme}/{Name}", "GET")]
  50. [Api(Description = "Gets a media info image by name")]
  51. public class GetMediaInfoImage
  52. {
  53. /// <summary>
  54. /// Gets or sets the name.
  55. /// </summary>
  56. /// <value>The name.</value>
  57. [ApiMember(Name = "Name", Description = "The name of the image", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  58. public string Name { get; set; }
  59. /// <summary>
  60. /// Gets or sets the theme.
  61. /// </summary>
  62. /// <value>The theme.</value>
  63. [ApiMember(Name = "Theme", Description = "The theme to get the image from", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  64. public string Theme { get; set; }
  65. }
  66. /// <summary>
  67. /// Class ImageByNameService
  68. /// </summary>
  69. public class ImageByNameService : BaseApiService
  70. {
  71. /// <summary>
  72. /// The _app paths
  73. /// </summary>
  74. private readonly IServerApplicationPaths _appPaths;
  75. /// <summary>
  76. /// Initializes a new instance of the <see cref="ImageByNameService" /> class.
  77. /// </summary>
  78. /// <param name="appPaths">The app paths.</param>
  79. public ImageByNameService(IServerApplicationPaths appPaths)
  80. {
  81. _appPaths = appPaths;
  82. }
  83. /// <summary>
  84. /// Gets the specified request.
  85. /// </summary>
  86. /// <param name="request">The request.</param>
  87. /// <returns>System.Object.</returns>
  88. public object Get(GetGeneralImage request)
  89. {
  90. var filename = string.Equals(request.Type, "primary", StringComparison.OrdinalIgnoreCase)
  91. ? "folder"
  92. : request.Type;
  93. var paths = BaseItem.SupportedImageExtensions.Select(i => Path.Combine(_appPaths.GeneralPath, request.Name, filename + i)).ToList();
  94. var path = paths.FirstOrDefault(File.Exists) ?? paths.FirstOrDefault();
  95. return ToStaticFileResult(path);
  96. }
  97. /// <summary>
  98. /// Gets the specified request.
  99. /// </summary>
  100. /// <param name="request">The request.</param>
  101. /// <returns>System.Object.</returns>
  102. public object Get(GetRatingImage request)
  103. {
  104. var themeFolder = Path.Combine(_appPaths.RatingsPath, request.Theme);
  105. if (Directory.Exists(themeFolder))
  106. {
  107. var path = BaseItem.SupportedImageExtensions.Select(i => Path.Combine(themeFolder, request.Name + i))
  108. .FirstOrDefault(File.Exists);
  109. if (!string.IsNullOrEmpty(path))
  110. {
  111. return ToStaticFileResult(path);
  112. }
  113. }
  114. var allFolder = Path.Combine(_appPaths.RatingsPath, "all");
  115. if (Directory.Exists(allFolder))
  116. {
  117. // Avoid implicitly captured closure
  118. var currentRequest = request;
  119. var path = BaseItem.SupportedImageExtensions.Select(i => Path.Combine(allFolder, currentRequest.Name + i))
  120. .FirstOrDefault(File.Exists);
  121. if (!string.IsNullOrEmpty(path))
  122. {
  123. return ToStaticFileResult(path);
  124. }
  125. }
  126. throw new ResourceNotFoundException("MediaInfo image not found: " + request.Name);
  127. }
  128. /// <summary>
  129. /// Gets the specified request.
  130. /// </summary>
  131. /// <param name="request">The request.</param>
  132. /// <returns>System.Object.</returns>
  133. public object Get(GetMediaInfoImage request)
  134. {
  135. var themeFolder = Path.Combine(_appPaths.MediaInfoImagesPath, request.Theme);
  136. if (Directory.Exists(themeFolder))
  137. {
  138. var path = BaseItem.SupportedImageExtensions.Select(i => Path.Combine(themeFolder, request.Name + i))
  139. .FirstOrDefault(File.Exists);
  140. if (!string.IsNullOrEmpty(path))
  141. {
  142. return ToStaticFileResult(path);
  143. }
  144. }
  145. var allFolder = Path.Combine(_appPaths.MediaInfoImagesPath, "all");
  146. if (Directory.Exists(allFolder))
  147. {
  148. // Avoid implicitly captured closure
  149. var currentRequest = request;
  150. var path = BaseItem.SupportedImageExtensions.Select(i => Path.Combine(allFolder, currentRequest.Name + i))
  151. .FirstOrDefault(File.Exists);
  152. if (!string.IsNullOrEmpty(path))
  153. {
  154. return ToStaticFileResult(path);
  155. }
  156. }
  157. throw new ResourceNotFoundException("MediaInfo image not found: " + request.Name);
  158. }
  159. }
  160. }