jellyfin/Jellyfin.Api/Controllers/ImageByNameController.cs

232 lines
9.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2020-08-06 14:17:45 +00:00
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
2020-05-19 19:13:07 +00:00
using System.Net.Mime;
2020-06-22 13:44:11 +00:00
using Jellyfin.Api.Constants;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Net;
2020-05-19 15:24:04 +00:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
2020-06-17 16:49:34 +00:00
namespace Jellyfin.Api.Controllers
{
/// <summary>
/// Images By Name Controller.
/// </summary>
[Route("Images")]
public class ImageByNameController : BaseJellyfinApiController
{
private readonly IServerApplicationPaths _applicationPaths;
private readonly IFileSystem _fileSystem;
/// <summary>
/// Initializes a new instance of the <see cref="ImageByNameController" /> class.
/// </summary>
/// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager" /> interface.</param>
/// <param name="fileSystem">Instance of the <see cref="IFileSystem" /> interface.</param>
public ImageByNameController(
IServerConfigurationManager serverConfigurationManager,
IFileSystem fileSystem)
{
_applicationPaths = serverConfigurationManager.ApplicationPaths;
_fileSystem = fileSystem;
}
/// <summary>
/// Get all general images.
/// </summary>
2020-05-03 00:46:27 +00:00
/// <response code="200">Retrieved list of images.</response>
/// <returns>An <see cref="OkResult"/> containing the list of images.</returns>
[HttpGet("General")]
2020-06-22 13:44:11 +00:00
[Authorize(Policy = Policies.DefaultAuthorization)]
2020-04-21 20:07:11 +00:00
[ProducesResponseType(StatusCodes.Status200OK)]
2020-05-03 00:46:27 +00:00
public ActionResult<IEnumerable<ImageByNameInfo>> GetGeneralImages()
{
2020-05-19 15:14:37 +00:00
return GetImageList(_applicationPaths.GeneralPath, false);
}
/// <summary>
/// Get General Image.
/// </summary>
/// <param name="name">The name of the image.</param>
/// <param name="type">Image Type (primary, backdrop, logo, etc).</param>
2020-05-03 00:46:27 +00:00
/// <response code="200">Image stream retrieved.</response>
/// <response code="404">Image not found.</response>
/// <returns>A <see cref="FileStreamResult"/> containing the image contents on success, or a <see cref="NotFoundResult"/> if the image could not be found.</returns>
[HttpGet("General/{name}/{type}")]
[AllowAnonymous]
2020-05-19 19:13:07 +00:00
[Produces(MediaTypeNames.Application.Octet)]
2020-04-21 20:07:11 +00:00
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-08-06 14:17:45 +00:00
public ActionResult<FileStreamResult> GetGeneralImage([FromRoute, Required] string? name, [FromRoute, Required] string? type)
{
2020-04-21 13:57:45 +00:00
var filename = string.Equals(type, "primary", StringComparison.OrdinalIgnoreCase)
? "folder"
: type;
var path = BaseItem.SupportedImageExtensions
.Select(i => Path.Combine(_applicationPaths.GeneralPath, name, filename + i))
.FirstOrDefault(System.IO.File.Exists);
if (path == null)
{
2020-04-21 13:57:45 +00:00
return NotFound();
}
2020-04-21 13:57:45 +00:00
var contentType = MimeTypes.GetMimeType(path);
2020-06-17 16:49:34 +00:00
return File(System.IO.File.OpenRead(path), contentType);
}
/// <summary>
/// Get all general images.
/// </summary>
2020-05-03 00:46:27 +00:00
/// <response code="200">Retrieved list of images.</response>
/// <returns>An <see cref="OkResult"/> containing the list of images.</returns>
[HttpGet("Ratings")]
2020-06-22 13:44:11 +00:00
[Authorize(Policy = Policies.DefaultAuthorization)]
2020-04-21 20:07:11 +00:00
[ProducesResponseType(StatusCodes.Status200OK)]
2020-05-03 00:46:27 +00:00
public ActionResult<IEnumerable<ImageByNameInfo>> GetRatingImages()
{
2020-05-19 15:14:37 +00:00
return GetImageList(_applicationPaths.RatingsPath, false);
}
/// <summary>
/// Get rating image.
/// </summary>
/// <param name="theme">The theme to get the image from.</param>
/// <param name="name">The name of the image.</param>
2020-05-03 00:46:27 +00:00
/// <response code="200">Image stream retrieved.</response>
/// <response code="404">Image not found.</response>
/// <returns>A <see cref="FileStreamResult"/> containing the image contents on success, or a <see cref="NotFoundResult"/> if the image could not be found.</returns>
[HttpGet("Ratings/{theme}/{name}")]
[AllowAnonymous]
2020-05-19 19:13:07 +00:00
[Produces(MediaTypeNames.Application.Octet)]
2020-04-21 20:07:11 +00:00
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-04-21 20:07:11 +00:00
public ActionResult<FileStreamResult> GetRatingImage(
2020-08-06 14:17:45 +00:00
[FromRoute, Required] string? theme,
[FromRoute, Required] string? name)
{
2020-04-21 13:57:45 +00:00
return GetImageFile(_applicationPaths.RatingsPath, theme, name);
}
/// <summary>
/// Get all media info images.
/// </summary>
2020-05-03 00:46:27 +00:00
/// <response code="200">Image list retrieved.</response>
/// <returns>An <see cref="OkResult"/> containing the list of images.</returns>
[HttpGet("MediaInfo")]
2020-06-22 13:44:11 +00:00
[Authorize(Policy = Policies.DefaultAuthorization)]
2020-04-21 20:07:11 +00:00
[ProducesResponseType(StatusCodes.Status200OK)]
2020-05-03 00:47:05 +00:00
public ActionResult<IEnumerable<ImageByNameInfo>> GetMediaInfoImages()
{
2020-05-19 15:14:37 +00:00
return GetImageList(_applicationPaths.MediaInfoImagesPath, false);
}
/// <summary>
/// Get media info image.
/// </summary>
/// <param name="theme">The theme to get the image from.</param>
/// <param name="name">The name of the image.</param>
2020-05-03 00:46:27 +00:00
/// <response code="200">Image stream retrieved.</response>
/// <response code="404">Image not found.</response>
/// <returns>A <see cref="FileStreamResult"/> containing the image contents on success, or a <see cref="NotFoundResult"/> if the image could not be found.</returns>
[HttpGet("MediaInfo/{theme}/{name}")]
[AllowAnonymous]
2020-05-19 19:13:07 +00:00
[Produces(MediaTypeNames.Application.Octet)]
2020-04-21 20:07:11 +00:00
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-04-21 20:07:11 +00:00
public ActionResult<FileStreamResult> GetMediaInfoImage(
2020-08-06 14:17:45 +00:00
[FromRoute, Required] string? theme,
[FromRoute, Required] string? name)
{
2020-04-21 13:57:45 +00:00
return GetImageFile(_applicationPaths.MediaInfoImagesPath, theme, name);
}
/// <summary>
/// Internal FileHelper.
/// </summary>
/// <param name="basePath">Path to begin search.</param>
/// <param name="theme">Theme to search.</param>
/// <param name="name">File name to search for.</param>
2020-05-03 00:46:27 +00:00
/// <returns>A <see cref="FileStreamResult"/> containing the image contents on success, or a <see cref="NotFoundResult"/> if the image could not be found.</returns>
2020-06-27 16:50:44 +00:00
private ActionResult<FileStreamResult> GetImageFile(string basePath, string? theme, string? name)
{
var themeFolder = Path.Combine(basePath, theme);
if (Directory.Exists(themeFolder))
{
var path = BaseItem.SupportedImageExtensions.Select(i => Path.Combine(themeFolder, name + i))
.FirstOrDefault(System.IO.File.Exists);
if (!string.IsNullOrEmpty(path) && System.IO.File.Exists(path))
{
var contentType = MimeTypes.GetMimeType(path);
2020-06-17 16:49:34 +00:00
return File(System.IO.File.OpenRead(path), contentType);
}
}
var allFolder = Path.Combine(basePath, "all");
if (Directory.Exists(allFolder))
{
var path = BaseItem.SupportedImageExtensions.Select(i => Path.Combine(allFolder, name + i))
.FirstOrDefault(System.IO.File.Exists);
if (!string.IsNullOrEmpty(path) && System.IO.File.Exists(path))
{
var contentType = MimeTypes.GetMimeType(path);
2020-06-17 16:49:34 +00:00
return File(System.IO.File.OpenRead(path), contentType);
}
}
return NotFound();
}
private List<ImageByNameInfo> GetImageList(string path, bool supportsThemes)
{
try
{
return _fileSystem.GetFiles(path, BaseItem.SupportedImageExtensions, false, true)
.Select(i => new ImageByNameInfo
{
Name = _fileSystem.GetFileNameWithoutExtension(i),
FileLength = i.Length,
// For themeable images, use the Theme property
// For general images, the same object structure is fine,
// but it's not owned by a theme, so call it Context
Theme = supportsThemes ? GetThemeName(i.FullName, path) : null,
Context = supportsThemes ? null : GetThemeName(i.FullName, path),
Format = i.Extension.ToLowerInvariant().TrimStart('.')
})
.OrderBy(i => i.Name)
.ToList();
}
catch (IOException)
{
return new List<ImageByNameInfo>();
}
}
2020-04-20 19:12:35 +00:00
private string? GetThemeName(string path, string rootImagePath)
{
var parentName = Path.GetDirectoryName(path);
if (string.Equals(parentName, rootImagePath, StringComparison.OrdinalIgnoreCase))
{
return null;
}
parentName = Path.GetFileName(parentName);
return string.Equals(parentName, "all", StringComparison.OrdinalIgnoreCase) ? null : parentName;
}
}
}