ImageByNameController.cs 8.3 KB

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