ImageByNameController.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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.Controller.Net;
  10. using MediaBrowser.Model.Dto;
  11. using MediaBrowser.Model.IO;
  12. using MediaBrowser.Model.Net;
  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. [Authenticated]
  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. /// <returns>General images.</returns>
  42. [HttpGet("General")]
  43. [ProducesResponseType(typeof(ImageByNameInfo[]), StatusCodes.Status200OK)]
  44. [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
  45. public IActionResult GetGeneralImages()
  46. {
  47. return Ok(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. /// <returns>Image Stream.</returns>
  55. [HttpGet("General/{Name}/{Type}")]
  56. [ProducesResponseType(typeof(FileStreamResult), StatusCodes.Status200OK)]
  57. [ProducesResponseType(StatusCodes.Status404NotFound)]
  58. [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
  59. public IActionResult GetGeneralImage([FromRoute] string name, [FromRoute] string type)
  60. {
  61. var filename = string.Equals(type, "primary", StringComparison.OrdinalIgnoreCase)
  62. ? "folder"
  63. : type;
  64. var paths = BaseItem.SupportedImageExtensions
  65. .Select(i => Path.Combine(_applicationPaths.GeneralPath, name, filename + i)).ToList();
  66. var path = paths.FirstOrDefault(System.IO.File.Exists) ?? paths.FirstOrDefault();
  67. if (path == null || !System.IO.File.Exists(path))
  68. {
  69. return NotFound();
  70. }
  71. var contentType = MimeTypes.GetMimeType(path);
  72. return new FileStreamResult(System.IO.File.OpenRead(path), contentType);
  73. }
  74. /// <summary>
  75. /// Get all general images.
  76. /// </summary>
  77. /// <returns>General images.</returns>
  78. [HttpGet("Ratings")]
  79. [ProducesResponseType(typeof(ImageByNameInfo[]), StatusCodes.Status200OK)]
  80. [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
  81. public IActionResult GetRatingImages()
  82. {
  83. return Ok(GetImageList(_applicationPaths.RatingsPath, false));
  84. }
  85. /// <summary>
  86. /// Get rating image.
  87. /// </summary>
  88. /// <param name="theme">The theme to get the image from.</param>
  89. /// <param name="name">The name of the image.</param>
  90. /// <returns>Image Stream.</returns>
  91. [HttpGet("Ratings/{Theme}/{Name}")]
  92. [ProducesResponseType(typeof(FileStreamResult), StatusCodes.Status200OK)]
  93. [ProducesResponseType(StatusCodes.Status404NotFound)]
  94. [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
  95. public IActionResult GetRatingImage(
  96. [FromRoute] string theme,
  97. [FromRoute] string name)
  98. {
  99. return GetImageFile(_applicationPaths.RatingsPath, theme, name);
  100. }
  101. /// <summary>
  102. /// Get all media info images.
  103. /// </summary>
  104. /// <returns>Media Info images.</returns>
  105. [HttpGet("MediaInfo")]
  106. [ProducesResponseType(typeof(ImageByNameInfo[]), StatusCodes.Status200OK)]
  107. [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
  108. public IActionResult GetMediaInfoImages()
  109. {
  110. return Ok(GetImageList(_applicationPaths.MediaInfoImagesPath, false));
  111. }
  112. /// <summary>
  113. /// Get media info image.
  114. /// </summary>
  115. /// <param name="theme">The theme to get the image from.</param>
  116. /// <param name="name">The name of the image.</param>
  117. /// <returns>Image Stream.</returns>
  118. [HttpGet("MediaInfo/{Theme}/{Name}")]
  119. [ProducesResponseType(typeof(FileStreamResult), StatusCodes.Status200OK)]
  120. [ProducesResponseType(StatusCodes.Status404NotFound)]
  121. [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
  122. public IActionResult GetMediaInfoImage(
  123. [FromRoute] string theme,
  124. [FromRoute] string name)
  125. {
  126. return GetImageFile(_applicationPaths.MediaInfoImagesPath, theme, name);
  127. }
  128. /// <summary>
  129. /// Internal FileHelper.
  130. /// </summary>
  131. /// <param name="basePath">Path to begin search.</param>
  132. /// <param name="theme">Theme to search.</param>
  133. /// <param name="name">File name to search for.</param>
  134. /// <returns>Image Stream.</returns>
  135. private IActionResult GetImageFile(string basePath, string theme, string name)
  136. {
  137. var themeFolder = Path.Combine(basePath, theme);
  138. if (Directory.Exists(themeFolder))
  139. {
  140. var path = BaseItem.SupportedImageExtensions.Select(i => Path.Combine(themeFolder, name + i))
  141. .FirstOrDefault(System.IO.File.Exists);
  142. if (!string.IsNullOrEmpty(path) && System.IO.File.Exists(path))
  143. {
  144. var contentType = MimeTypes.GetMimeType(path);
  145. return new FileStreamResult(System.IO.File.OpenRead(path), contentType);
  146. }
  147. }
  148. var allFolder = Path.Combine(basePath, "all");
  149. if (Directory.Exists(allFolder))
  150. {
  151. var path = BaseItem.SupportedImageExtensions.Select(i => Path.Combine(allFolder, name + i))
  152. .FirstOrDefault(System.IO.File.Exists);
  153. if (!string.IsNullOrEmpty(path) && System.IO.File.Exists(path))
  154. {
  155. var contentType = MimeTypes.GetMimeType(path);
  156. return new FileStreamResult(System.IO.File.OpenRead(path), contentType);
  157. }
  158. }
  159. return NotFound();
  160. }
  161. private List<ImageByNameInfo> GetImageList(string path, bool supportsThemes)
  162. {
  163. try
  164. {
  165. return _fileSystem.GetFiles(path, BaseItem.SupportedImageExtensions, false, true)
  166. .Select(i => new ImageByNameInfo
  167. {
  168. Name = _fileSystem.GetFileNameWithoutExtension(i),
  169. FileLength = i.Length,
  170. // For themeable images, use the Theme property
  171. // For general images, the same object structure is fine,
  172. // but it's not owned by a theme, so call it Context
  173. Theme = supportsThemes ? GetThemeName(i.FullName, path) : null,
  174. Context = supportsThemes ? null : GetThemeName(i.FullName, path),
  175. Format = i.Extension.ToLowerInvariant().TrimStart('.')
  176. })
  177. .OrderBy(i => i.Name)
  178. .ToList();
  179. }
  180. catch (IOException)
  181. {
  182. return new List<ImageByNameInfo>();
  183. }
  184. }
  185. private string? GetThemeName(string path, string rootImagePath)
  186. {
  187. var parentName = Path.GetDirectoryName(path);
  188. if (string.Equals(parentName, rootImagePath, StringComparison.OrdinalIgnoreCase))
  189. {
  190. return null;
  191. }
  192. parentName = Path.GetFileName(parentName);
  193. return string.Equals(parentName, "all", StringComparison.OrdinalIgnoreCase) ? null : parentName;
  194. }
  195. }
  196. }