ImageByNameController.cs 9.5 KB

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