jellyfin/Jellyfin.Api/Controllers/RemoteImageController.cs

261 lines
10 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;
2020-05-20 13:18:51 +00:00
using System.Net.Mime;
using System.Threading;
using System.Threading.Tasks;
2020-06-22 13:44:11 +00:00
using Jellyfin.Api.Constants;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Net;
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;
2020-08-13 21:50:19 +00:00
private readonly IHttpClientFactory _httpClientFactory;
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>
2020-08-13 21:50:19 +00:00
/// <param name="httpClientFactory">Instance of the <see cref="IHttpClientFactory"/> interface.</param>
/// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
public RemoteImageController(
IProviderManager providerManager,
IServerApplicationPaths applicationPaths,
2020-08-13 21:50:19 +00:00
IHttpClientFactory httpClientFactory,
ILibraryManager libraryManager)
{
_providerManager = providerManager;
_applicationPaths = applicationPaths;
2020-08-13 21:50:19 +00:00
_httpClientFactory = httpClientFactory;
_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(
[FromRoute] 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)]
public ActionResult<IEnumerable<ImageProviderInfo>> GetRemoteImageProviders([FromRoute] 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>
/// Gets a remote image.
/// </summary>
/// <param name="imageUrl">The image url.</param>
2020-05-19 15:23:28 +00:00
/// <response code="200">Remote image returned.</response>
/// <response code="404">Remote image not found.</response>
/// <returns>Image Stream.</returns>
2020-08-05 19:57:01 +00:00
[HttpGet("Images/Remote")]
2020-05-20 13:18:51 +00:00
[Produces(MediaTypeNames.Application.Octet)]
2020-04-21 20:09:06 +00:00
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
2020-08-06 14:17:45 +00:00
public async Task<ActionResult> GetRemoteImage([FromQuery, Required] string imageUrl)
{
2020-04-21 13:58:54 +00:00
var urlHash = imageUrl.GetMD5();
var pointerCachePath = GetFullCachePath(urlHash.ToString());
2020-04-21 13:58:54 +00:00
string? contentPath = null;
2020-05-19 15:23:28 +00:00
var hasFile = false;
2020-04-21 13:58:54 +00:00
try
{
contentPath = await System.IO.File.ReadAllTextAsync(pointerCachePath).ConfigureAwait(false);
if (System.IO.File.Exists(contentPath))
{
2020-04-21 13:58:54 +00:00
hasFile = true;
}
2020-04-21 13:58:54 +00:00
}
catch (FileNotFoundException)
{
2020-05-19 15:23:28 +00:00
// The file isn't cached yet
2020-04-21 13:58:54 +00:00
}
catch (IOException)
{
2020-05-19 15:23:28 +00:00
// The file isn't cached yet
2020-04-21 13:58:54 +00:00
}
2020-04-21 13:58:54 +00:00
if (!hasFile)
{
await DownloadImage(imageUrl, urlHash, pointerCachePath).ConfigureAwait(false);
contentPath = await System.IO.File.ReadAllTextAsync(pointerCachePath).ConfigureAwait(false);
}
2020-04-21 13:58:54 +00:00
if (string.IsNullOrEmpty(contentPath))
{
2020-04-21 13:58:54 +00:00
return NotFound();
}
2020-04-21 13:58:54 +00:00
var contentType = MimeTypes.GetMimeType(contentPath);
2020-06-17 16:51:50 +00:00
return File(System.IO.File.OpenRead(contentPath), contentType);
}
/// <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(
[FromRoute] 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);
}
/// <summary>
/// Downloads the image.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="urlHash">The URL hash.</param>
/// <param name="pointerCachePath">The pointer cache path.</param>
/// <returns>Task.</returns>
private async Task DownloadImage(string url, Guid urlHash, string pointerCachePath)
{
2020-08-16 15:47:08 +00:00
var httpClient = _httpClientFactory.CreateClient();
2020-08-16 16:57:01 +00:00
using var response = await httpClient.GetAsync(url).ConfigureAwait(false);
2020-08-13 21:50:19 +00:00
var ext = response.Content.Headers.ContentType.MediaType.Split('/').Last();
var fullCachePath = GetFullCachePath(urlHash + "." + ext);
Directory.CreateDirectory(Path.GetDirectoryName(fullCachePath));
2020-08-13 21:50:19 +00:00
await using var fileStream = new FileStream(fullCachePath, FileMode.Create, FileAccess.Write, FileShare.Read, IODefaults.FileStreamBufferSize, true);
await response.Content.CopyToAsync(fileStream).ConfigureAwait(false);
Directory.CreateDirectory(Path.GetDirectoryName(pointerCachePath));
await System.IO.File.WriteAllTextAsync(pointerCachePath, fullCachePath, CancellationToken.None)
.ConfigureAwait(false);
}
}
}