2
0

ImageByNameController.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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.GetFullPath(Path.Combine(_applicationPaths.GeneralPath, name, filename + i)))
  73. .FirstOrDefault(System.IO.File.Exists);
  74. if (path == null)
  75. {
  76. return NotFound();
  77. }
  78. if (!path.StartsWith(_applicationPaths.GeneralPath, StringComparison.InvariantCulture))
  79. {
  80. return BadRequest("Invalid image path.");
  81. }
  82. var contentType = MimeTypes.GetMimeType(path);
  83. return File(AsyncFile.OpenRead(path), contentType);
  84. }
  85. /// <summary>
  86. /// Get all general images.
  87. /// </summary>
  88. /// <response code="200">Retrieved list of images.</response>
  89. /// <returns>An <see cref="OkResult"/> containing the list of images.</returns>
  90. [HttpGet("Ratings")]
  91. [Authorize(Policy = Policies.DefaultAuthorization)]
  92. [ProducesResponseType(StatusCodes.Status200OK)]
  93. public ActionResult<IEnumerable<ImageByNameInfo>> GetRatingImages()
  94. {
  95. return GetImageList(_applicationPaths.RatingsPath, false);
  96. }
  97. /// <summary>
  98. /// Get rating image.
  99. /// </summary>
  100. /// <param name="theme">The theme to get the image from.</param>
  101. /// <param name="name">The name of the image.</param>
  102. /// <response code="200">Image stream retrieved.</response>
  103. /// <response code="404">Image not found.</response>
  104. /// <returns>A <see cref="FileStreamResult"/> containing the image contents on success, or a <see cref="NotFoundResult"/> if the image could not be found.</returns>
  105. [HttpGet("Ratings/{theme}/{name}")]
  106. [AllowAnonymous]
  107. [Produces(MediaTypeNames.Application.Octet)]
  108. [ProducesResponseType(StatusCodes.Status200OK)]
  109. [ProducesResponseType(StatusCodes.Status404NotFound)]
  110. [ProducesImageFile]
  111. public ActionResult GetRatingImage(
  112. [FromRoute, Required] string theme,
  113. [FromRoute, Required] string name)
  114. {
  115. return GetImageFile(_applicationPaths.RatingsPath, theme, name);
  116. }
  117. /// <summary>
  118. /// Get all media info images.
  119. /// </summary>
  120. /// <response code="200">Image list retrieved.</response>
  121. /// <returns>An <see cref="OkResult"/> containing the list of images.</returns>
  122. [HttpGet("MediaInfo")]
  123. [Authorize(Policy = Policies.DefaultAuthorization)]
  124. [ProducesResponseType(StatusCodes.Status200OK)]
  125. public ActionResult<IEnumerable<ImageByNameInfo>> GetMediaInfoImages()
  126. {
  127. return GetImageList(_applicationPaths.MediaInfoImagesPath, false);
  128. }
  129. /// <summary>
  130. /// Get media info image.
  131. /// </summary>
  132. /// <param name="theme">The theme to get the image from.</param>
  133. /// <param name="name">The name of the image.</param>
  134. /// <response code="200">Image stream retrieved.</response>
  135. /// <response code="404">Image not found.</response>
  136. /// <returns>A <see cref="FileStreamResult"/> containing the image contents on success, or a <see cref="NotFoundResult"/> if the image could not be found.</returns>
  137. [HttpGet("MediaInfo/{theme}/{name}")]
  138. [AllowAnonymous]
  139. [Produces(MediaTypeNames.Application.Octet)]
  140. [ProducesResponseType(StatusCodes.Status200OK)]
  141. [ProducesResponseType(StatusCodes.Status404NotFound)]
  142. [ProducesImageFile]
  143. public ActionResult GetMediaInfoImage(
  144. [FromRoute, Required] string theme,
  145. [FromRoute, Required] string name)
  146. {
  147. return GetImageFile(_applicationPaths.MediaInfoImagesPath, theme, name);
  148. }
  149. /// <summary>
  150. /// Internal FileHelper.
  151. /// </summary>
  152. /// <param name="basePath">Path to begin search.</param>
  153. /// <param name="theme">Theme to search.</param>
  154. /// <param name="name">File name to search for.</param>
  155. /// <returns>A <see cref="FileStreamResult"/> containing the image contents on success, or a <see cref="NotFoundResult"/> if the image could not be found.</returns>
  156. private ActionResult GetImageFile(string basePath, string theme, string? name)
  157. {
  158. var themeFolder = Path.GetFullPath(Path.Combine(basePath, theme));
  159. if (Directory.Exists(themeFolder))
  160. {
  161. var path = BaseItem.SupportedImageExtensions.Select(i => Path.Combine(themeFolder, name + i))
  162. .FirstOrDefault(System.IO.File.Exists);
  163. if (!string.IsNullOrEmpty(path) && System.IO.File.Exists(path))
  164. {
  165. if (!path.StartsWith(basePath, StringComparison.InvariantCulture))
  166. {
  167. return BadRequest("Invalid image path.");
  168. }
  169. var contentType = MimeTypes.GetMimeType(path);
  170. return PhysicalFile(path, contentType);
  171. }
  172. }
  173. var allFolder = Path.GetFullPath(Path.Combine(basePath, "all"));
  174. if (Directory.Exists(allFolder))
  175. {
  176. var path = BaseItem.SupportedImageExtensions.Select(i => Path.Combine(allFolder, name + i))
  177. .FirstOrDefault(System.IO.File.Exists);
  178. if (!string.IsNullOrEmpty(path) && System.IO.File.Exists(path))
  179. {
  180. if (!path.StartsWith(basePath, StringComparison.InvariantCulture))
  181. {
  182. return BadRequest("Invalid image path.");
  183. }
  184. var contentType = MimeTypes.GetMimeType(path);
  185. return PhysicalFile(path, contentType);
  186. }
  187. }
  188. return NotFound();
  189. }
  190. private List<ImageByNameInfo> GetImageList(string path, bool supportsThemes)
  191. {
  192. try
  193. {
  194. return _fileSystem.GetFiles(path, BaseItem.SupportedImageExtensions, false, true)
  195. .Select(i => new ImageByNameInfo
  196. {
  197. Name = _fileSystem.GetFileNameWithoutExtension(i),
  198. FileLength = i.Length,
  199. // For themeable images, use the Theme property
  200. // For general images, the same object structure is fine,
  201. // but it's not owned by a theme, so call it Context
  202. Theme = supportsThemes ? GetThemeName(i.FullName, path) : null,
  203. Context = supportsThemes ? null : GetThemeName(i.FullName, path),
  204. Format = i.Extension.ToLowerInvariant().TrimStart('.')
  205. })
  206. .OrderBy(i => i.Name)
  207. .ToList();
  208. }
  209. catch (IOException)
  210. {
  211. return new List<ImageByNameInfo>();
  212. }
  213. }
  214. private string? GetThemeName(string path, string rootImagePath)
  215. {
  216. var parentName = Path.GetDirectoryName(path);
  217. if (string.Equals(parentName, rootImagePath, StringComparison.OrdinalIgnoreCase))
  218. {
  219. return null;
  220. }
  221. parentName = Path.GetFileName(parentName);
  222. return string.Equals(parentName, "all", StringComparison.OrdinalIgnoreCase) ? null : parentName;
  223. }
  224. }
  225. }