ImageByNameController.cs 9.8 KB

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