ImageByNameController.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net.Mime;
  7. using Jellyfin.Api.Attributes;
  8. using Jellyfin.Api.Constants;
  9. using MediaBrowser.Controller;
  10. using MediaBrowser.Controller.Configuration;
  11. using MediaBrowser.Controller.Entities;
  12. using MediaBrowser.Model.Dto;
  13. using MediaBrowser.Model.IO;
  14. using MediaBrowser.Model.Net;
  15. using Microsoft.AspNetCore.Authorization;
  16. using Microsoft.AspNetCore.Http;
  17. using Microsoft.AspNetCore.Mvc;
  18. namespace Jellyfin.Api.Controllers
  19. {
  20. /// <summary>
  21. /// Images By Name Controller.
  22. /// </summary>
  23. [Route("Images")]
  24. public class ImageByNameController : BaseJellyfinApiController
  25. {
  26. private readonly IServerApplicationPaths _applicationPaths;
  27. private readonly IFileSystem _fileSystem;
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="ImageByNameController" /> class.
  30. /// </summary>
  31. /// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager" /> interface.</param>
  32. /// <param name="fileSystem">Instance of the <see cref="IFileSystem" /> interface.</param>
  33. public ImageByNameController(
  34. IServerConfigurationManager serverConfigurationManager,
  35. IFileSystem fileSystem)
  36. {
  37. _applicationPaths = serverConfigurationManager.ApplicationPaths;
  38. _fileSystem = fileSystem;
  39. }
  40. /// <summary>
  41. /// Get all general images.
  42. /// </summary>
  43. /// <response code="200">Retrieved list of images.</response>
  44. /// <returns>An <see cref="OkResult"/> containing the list of images.</returns>
  45. [HttpGet("General")]
  46. [Authorize(Policy = Policies.DefaultAuthorization)]
  47. [ProducesResponseType(StatusCodes.Status200OK)]
  48. public ActionResult<IEnumerable<ImageByNameInfo>> GetGeneralImages()
  49. {
  50. return GetImageList(_applicationPaths.GeneralPath, false);
  51. }
  52. /// <summary>
  53. /// Get General Image.
  54. /// </summary>
  55. /// <param name="name">The name of the image.</param>
  56. /// <param name="type">Image Type (primary, backdrop, logo, etc).</param>
  57. /// <response code="200">Image stream retrieved.</response>
  58. /// <response code="404">Image not found.</response>
  59. /// <returns>A <see cref="FileStreamResult"/> containing the image contents on success, or a <see cref="NotFoundResult"/> if the image could not be found.</returns>
  60. [HttpGet("General/{name}/{type}")]
  61. [AllowAnonymous]
  62. [Produces(MediaTypeNames.Application.Octet)]
  63. [ProducesResponseType(StatusCodes.Status200OK)]
  64. [ProducesResponseType(StatusCodes.Status404NotFound)]
  65. [ProducesImageFile]
  66. public ActionResult GetGeneralImage([FromRoute, Required] string name, [FromRoute, Required] string type)
  67. {
  68. var filename = string.Equals(type, "primary", StringComparison.OrdinalIgnoreCase)
  69. ? "folder"
  70. : type;
  71. var path = BaseItem.SupportedImageExtensions
  72. .Select(i => Path.Combine(_applicationPaths.GeneralPath, name, filename + i))
  73. .FirstOrDefault(System.IO.File.Exists);
  74. if (path == null)
  75. {
  76. return NotFound();
  77. }
  78. var contentType = MimeTypes.GetMimeType(path);
  79. return File(System.IO.File.OpenRead(path), contentType);
  80. }
  81. /// <summary>
  82. /// Get all general images.
  83. /// </summary>
  84. /// <response code="200">Retrieved list of images.</response>
  85. /// <returns>An <see cref="OkResult"/> containing the list of images.</returns>
  86. [HttpGet("Ratings")]
  87. [Authorize(Policy = Policies.DefaultAuthorization)]
  88. [ProducesResponseType(StatusCodes.Status200OK)]
  89. public ActionResult<IEnumerable<ImageByNameInfo>> GetRatingImages()
  90. {
  91. return GetImageList(_applicationPaths.RatingsPath, false);
  92. }
  93. /// <summary>
  94. /// Get rating image.
  95. /// </summary>
  96. /// <param name="theme">The theme to get the image from.</param>
  97. /// <param name="name">The name of the image.</param>
  98. /// <response code="200">Image stream retrieved.</response>
  99. /// <response code="404">Image not found.</response>
  100. /// <returns>A <see cref="FileStreamResult"/> containing the image contents on success, or a <see cref="NotFoundResult"/> if the image could not be found.</returns>
  101. [HttpGet("Ratings/{theme}/{name}")]
  102. [AllowAnonymous]
  103. [Produces(MediaTypeNames.Application.Octet)]
  104. [ProducesResponseType(StatusCodes.Status200OK)]
  105. [ProducesResponseType(StatusCodes.Status404NotFound)]
  106. [ProducesImageFile]
  107. public ActionResult GetRatingImage(
  108. [FromRoute, Required] string theme,
  109. [FromRoute, Required] string name)
  110. {
  111. return GetImageFile(_applicationPaths.RatingsPath, theme, name);
  112. }
  113. /// <summary>
  114. /// Get all media info images.
  115. /// </summary>
  116. /// <response code="200">Image list retrieved.</response>
  117. /// <returns>An <see cref="OkResult"/> containing the list of images.</returns>
  118. [HttpGet("MediaInfo")]
  119. [Authorize(Policy = Policies.DefaultAuthorization)]
  120. [ProducesResponseType(StatusCodes.Status200OK)]
  121. public ActionResult<IEnumerable<ImageByNameInfo>> GetMediaInfoImages()
  122. {
  123. return GetImageList(_applicationPaths.MediaInfoImagesPath, false);
  124. }
  125. /// <summary>
  126. /// Get media info image.
  127. /// </summary>
  128. /// <param name="theme">The theme to get the image from.</param>
  129. /// <param name="name">The name of the image.</param>
  130. /// <response code="200">Image stream retrieved.</response>
  131. /// <response code="404">Image not found.</response>
  132. /// <returns>A <see cref="FileStreamResult"/> containing the image contents on success, or a <see cref="NotFoundResult"/> if the image could not be found.</returns>
  133. [HttpGet("MediaInfo/{theme}/{name}")]
  134. [AllowAnonymous]
  135. [Produces(MediaTypeNames.Application.Octet)]
  136. [ProducesResponseType(StatusCodes.Status200OK)]
  137. [ProducesResponseType(StatusCodes.Status404NotFound)]
  138. [ProducesImageFile]
  139. public ActionResult GetMediaInfoImage(
  140. [FromRoute, Required] string theme,
  141. [FromRoute, Required] string name)
  142. {
  143. return GetImageFile(_applicationPaths.MediaInfoImagesPath, theme, name);
  144. }
  145. /// <summary>
  146. /// Internal FileHelper.
  147. /// </summary>
  148. /// <param name="basePath">Path to begin search.</param>
  149. /// <param name="theme">Theme to search.</param>
  150. /// <param name="name">File name to search for.</param>
  151. /// <returns>A <see cref="FileStreamResult"/> containing the image contents on success, or a <see cref="NotFoundResult"/> if the image could not be found.</returns>
  152. private ActionResult GetImageFile(string basePath, string? theme, string? name)
  153. {
  154. var themeFolder = Path.Combine(basePath, theme);
  155. if (Directory.Exists(themeFolder))
  156. {
  157. var path = BaseItem.SupportedImageExtensions.Select(i => Path.Combine(themeFolder, name + i))
  158. .FirstOrDefault(System.IO.File.Exists);
  159. if (!string.IsNullOrEmpty(path) && System.IO.File.Exists(path))
  160. {
  161. var contentType = MimeTypes.GetMimeType(path);
  162. return PhysicalFile(path, contentType);
  163. }
  164. }
  165. var allFolder = Path.Combine(basePath, "all");
  166. if (Directory.Exists(allFolder))
  167. {
  168. var path = BaseItem.SupportedImageExtensions.Select(i => Path.Combine(allFolder, name + i))
  169. .FirstOrDefault(System.IO.File.Exists);
  170. if (!string.IsNullOrEmpty(path) && System.IO.File.Exists(path))
  171. {
  172. var contentType = MimeTypes.GetMimeType(path);
  173. return PhysicalFile(path, contentType);
  174. }
  175. }
  176. return NotFound();
  177. }
  178. private List<ImageByNameInfo> GetImageList(string path, bool supportsThemes)
  179. {
  180. try
  181. {
  182. return _fileSystem.GetFiles(path, BaseItem.SupportedImageExtensions, false, true)
  183. .Select(i => new ImageByNameInfo
  184. {
  185. Name = _fileSystem.GetFileNameWithoutExtension(i),
  186. FileLength = i.Length,
  187. // For themeable images, use the Theme property
  188. // For general images, the same object structure is fine,
  189. // but it's not owned by a theme, so call it Context
  190. Theme = supportsThemes ? GetThemeName(i.FullName, path) : null,
  191. Context = supportsThemes ? null : GetThemeName(i.FullName, path),
  192. Format = i.Extension.ToLowerInvariant().TrimStart('.')
  193. })
  194. .OrderBy(i => i.Name)
  195. .ToList();
  196. }
  197. catch (IOException)
  198. {
  199. return new List<ImageByNameInfo>();
  200. }
  201. }
  202. private string? GetThemeName(string path, string rootImagePath)
  203. {
  204. var parentName = Path.GetDirectoryName(path);
  205. if (string.Equals(parentName, rootImagePath, StringComparison.OrdinalIgnoreCase))
  206. {
  207. return null;
  208. }
  209. parentName = Path.GetFileName(parentName);
  210. return string.Equals(parentName, "all", StringComparison.OrdinalIgnoreCase) ? null : parentName;
  211. }
  212. }
  213. }