ImageByNameController.cs 9.9 KB

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