ImageByNameController.cs 9.6 KB

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