jellyfin-server/Jellyfin.Api/Controllers/RemoteImageController.cs

184 lines
7.3 KiB
C#
Raw Normal View History

using System;
2020-05-19 15:23:28 +00:00
using System.Collections.Generic;
2020-08-06 14:17:45 +00:00
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
2020-08-13 21:50:19 +00:00
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
2020-06-22 13:44:11 +00:00
using Jellyfin.Api.Constants;
2021-09-19 18:53:31 +00:00
using Jellyfin.Extensions;
using MediaBrowser.Common.Extensions;
2020-08-31 17:05:21 +00:00
using MediaBrowser.Common.Net;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Providers;
2020-05-19 15:23:28 +00:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
2020-06-18 13:11:46 +00:00
namespace Jellyfin.Api.Controllers
{
/// <summary>
/// Remote Images Controller.
/// </summary>
2020-08-05 19:57:01 +00:00
[Route("")]
public class RemoteImageController : BaseJellyfinApiController
{
private readonly IProviderManager _providerManager;
private readonly IServerApplicationPaths _applicationPaths;
private readonly ILibraryManager _libraryManager;
/// <summary>
/// Initializes a new instance of the <see cref="RemoteImageController"/> class.
/// </summary>
/// <param name="providerManager">Instance of the <see cref="IProviderManager"/> interface.</param>
/// <param name="applicationPaths">Instance of the <see cref="IServerApplicationPaths"/> interface.</param>
/// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
public RemoteImageController(
IProviderManager providerManager,
IServerApplicationPaths applicationPaths,
ILibraryManager libraryManager)
{
_providerManager = providerManager;
_applicationPaths = applicationPaths;
_libraryManager = libraryManager;
}
/// <summary>
/// Gets available remote images for an item.
/// </summary>
/// <param name="itemId">Item Id.</param>
/// <param name="type">The image type.</param>
/// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped from the results.</param>
/// <param name="limit">Optional. The maximum number of records to return.</param>
/// <param name="providerName">Optional. The image provider to use.</param>
2020-05-19 15:23:28 +00:00
/// <param name="includeAllLanguages">Optional. Include all languages.</param>
/// <response code="200">Remote Images returned.</response>
/// <response code="404">Item not found.</response>
/// <returns>Remote Image Result.</returns>
2020-08-05 19:57:01 +00:00
[HttpGet("Items/{itemId}/RemoteImages")]
[Authorize(Policy = Policies.DefaultAuthorization)]
2020-04-21 20:09:06 +00:00
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-04-21 20:09:06 +00:00
public async Task<ActionResult<RemoteImageResult>> GetRemoteImages(
2020-09-06 15:07:27 +00:00
[FromRoute, Required] Guid itemId,
[FromQuery] ImageType? type,
[FromQuery] int? startIndex,
[FromQuery] int? limit,
2020-08-05 19:57:01 +00:00
[FromQuery] string? providerName,
[FromQuery] bool includeAllLanguages = false)
{
var item = _libraryManager.GetItemById(itemId);
2020-04-21 13:58:54 +00:00
if (item == null)
{
2020-04-21 13:58:54 +00:00
return NotFound();
}
2020-04-21 13:58:54 +00:00
var images = await _providerManager.GetAvailableRemoteImages(
item,
2020-08-05 19:57:01 +00:00
new RemoteImageQuery(providerName ?? string.Empty)
2020-04-21 13:58:54 +00:00
{
IncludeAllLanguages = includeAllLanguages,
IncludeDisabledProviders = true,
ImageType = type
}, CancellationToken.None)
.ConfigureAwait(false);
var imageArray = images.ToArray();
var allProviders = _providerManager.GetRemoteImageProviderInfo(item);
if (type.HasValue)
{
allProviders = allProviders.Where(o => o.SupportedImages.Contains(type.Value));
}
2020-04-21 13:58:54 +00:00
var result = new RemoteImageResult
{
TotalRecordCount = imageArray.Length,
Providers = allProviders.Select(o => o.Name)
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToArray()
};
2020-04-21 13:58:54 +00:00
if (startIndex.HasValue)
{
imageArray = imageArray.Skip(startIndex.Value).ToArray();
}
2020-04-21 13:58:54 +00:00
if (limit.HasValue)
{
2020-04-21 13:58:54 +00:00
imageArray = imageArray.Take(limit.Value).ToArray();
}
2020-04-21 13:58:54 +00:00
result.Images = imageArray;
2020-05-19 15:23:28 +00:00
return result;
}
/// <summary>
/// Gets available remote image providers for an item.
/// </summary>
/// <param name="itemId">Item Id.</param>
2020-05-19 15:23:28 +00:00
/// <response code="200">Returned remote image providers.</response>
/// <response code="404">Item not found.</response>
/// <returns>List of remote image providers.</returns>
2020-08-05 19:57:01 +00:00
[HttpGet("Items/{itemId}/RemoteImages/Providers")]
[Authorize(Policy = Policies.DefaultAuthorization)]
2020-04-21 20:09:06 +00:00
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-09-06 15:07:27 +00:00
public ActionResult<IEnumerable<ImageProviderInfo>> GetRemoteImageProviders([FromRoute, Required] Guid itemId)
{
var item = _libraryManager.GetItemById(itemId);
2020-04-21 13:58:54 +00:00
if (item == null)
{
2020-04-21 13:58:54 +00:00
return NotFound();
}
2020-04-21 13:58:54 +00:00
2020-05-19 15:23:28 +00:00
return Ok(_providerManager.GetRemoteImageProviderInfo(item));
}
/// <summary>
/// Downloads a remote image for an item.
/// </summary>
/// <param name="itemId">Item Id.</param>
/// <param name="type">The image type.</param>
/// <param name="imageUrl">The image url.</param>
/// <response code="204">Remote image downloaded.</response>
2020-05-19 15:23:28 +00:00
/// <response code="404">Remote image not found.</response>
/// <returns>Download status.</returns>
2020-08-05 19:57:01 +00:00
[HttpPost("Items/{itemId}/RemoteImages/Download")]
[Authorize(Policy = Policies.RequiresElevation)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-04-21 20:09:06 +00:00
public async Task<ActionResult> DownloadRemoteImage(
2020-09-06 15:07:27 +00:00
[FromRoute, Required] Guid itemId,
2020-08-06 14:17:45 +00:00
[FromQuery, Required] ImageType type,
2020-06-27 16:50:44 +00:00
[FromQuery] string? imageUrl)
{
var item = _libraryManager.GetItemById(itemId);
2020-04-21 13:58:54 +00:00
if (item == null)
{
2020-04-21 13:58:54 +00:00
return NotFound();
}
2020-04-21 13:58:54 +00:00
await _providerManager.SaveImage(item, imageUrl, type, null, CancellationToken.None)
.ConfigureAwait(false);
2020-08-21 20:01:19 +00:00
await item.UpdateToRepositoryAsync(ItemUpdateType.ImageUpdate, CancellationToken.None).ConfigureAwait(false);
return NoContent();
}
/// <summary>
/// Gets the full cache path.
/// </summary>
/// <param name="filename">The filename.</param>
/// <returns>System.String.</returns>
private string GetFullCachePath(string filename)
{
return Path.Combine(_applicationPaths.CachePath, "remote-images", filename.Substring(0, 1), filename);
}
}
}