2020-04-20 13:21:06 -07:00
|
|
|
using System;
|
2020-05-19 08:23:28 -07:00
|
|
|
using System.Collections.Generic;
|
2020-04-20 13:21:06 -07:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2020-05-20 06:18:51 -07:00
|
|
|
using System.Net.Mime;
|
2020-04-20 13:21:06 -07:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2020-06-22 06:44:11 -07:00
|
|
|
using Jellyfin.Api.Constants;
|
2020-04-20 13:21:06 -07:00
|
|
|
using MediaBrowser.Common.Extensions;
|
|
|
|
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.Net;
|
|
|
|
using MediaBrowser.Model.Providers;
|
2020-05-19 08:23:28 -07:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2020-04-20 13:21:06 -07:00
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
|
|
|
|
2020-06-18 06:11:46 -07:00
|
|
|
namespace Jellyfin.Api.Controllers
|
2020-04-20 13:21:06 -07:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Remote Images Controller.
|
|
|
|
/// </summary>
|
|
|
|
[Route("Images")]
|
2020-06-22 06:44:11 -07:00
|
|
|
[Authorize(Policy = Policies.DefaultAuthorization)]
|
2020-04-20 13:21:06 -07:00
|
|
|
public class RemoteImageController : BaseJellyfinApiController
|
|
|
|
{
|
|
|
|
private readonly IProviderManager _providerManager;
|
|
|
|
private readonly IServerApplicationPaths _applicationPaths;
|
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
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="httpClient">Instance of the <see cref="IHttpClient"/> interface.</param>
|
|
|
|
/// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
|
|
|
|
public RemoteImageController(
|
|
|
|
IProviderManager providerManager,
|
|
|
|
IServerApplicationPaths applicationPaths,
|
|
|
|
IHttpClient httpClient,
|
|
|
|
ILibraryManager libraryManager)
|
|
|
|
{
|
|
|
|
_providerManager = providerManager;
|
|
|
|
_applicationPaths = applicationPaths;
|
|
|
|
_httpClient = httpClient;
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets available remote images for an item.
|
|
|
|
/// </summary>
|
2020-06-20 17:02:07 -07:00
|
|
|
/// <param name="itemId">Item Id.</param>
|
2020-04-20 13:21:06 -07:00
|
|
|
/// <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 08:23:28 -07:00
|
|
|
/// <param name="includeAllLanguages">Optional. Include all languages.</param>
|
|
|
|
/// <response code="200">Remote Images returned.</response>
|
|
|
|
/// <response code="404">Item not found.</response>
|
2020-04-20 13:21:06 -07:00
|
|
|
/// <returns>Remote Image Result.</returns>
|
2020-06-20 17:02:07 -07:00
|
|
|
[HttpGet("{itemId}/RemoteImages")]
|
2020-04-21 13:09:06 -07:00
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
2020-04-20 13:21:06 -07:00
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
2020-04-21 13:09:06 -07:00
|
|
|
public async Task<ActionResult<RemoteImageResult>> GetRemoteImages(
|
2020-06-20 17:02:07 -07:00
|
|
|
[FromRoute] Guid itemId,
|
2020-04-20 13:21:06 -07:00
|
|
|
[FromQuery] ImageType? type,
|
|
|
|
[FromQuery] int? startIndex,
|
|
|
|
[FromQuery] int? limit,
|
|
|
|
[FromQuery] string providerName,
|
|
|
|
[FromQuery] bool includeAllLanguages)
|
|
|
|
{
|
2020-06-20 17:02:07 -07:00
|
|
|
var item = _libraryManager.GetItemById(itemId);
|
2020-04-21 06:58:54 -07:00
|
|
|
if (item == null)
|
2020-04-20 13:21:06 -07:00
|
|
|
{
|
2020-04-21 06:58:54 -07:00
|
|
|
return NotFound();
|
|
|
|
}
|
2020-04-20 13:21:06 -07:00
|
|
|
|
2020-04-21 06:58:54 -07:00
|
|
|
var images = await _providerManager.GetAvailableRemoteImages(
|
|
|
|
item,
|
2020-06-13 14:13:57 -07:00
|
|
|
new RemoteImageQuery(providerName)
|
2020-04-21 06:58:54 -07: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-20 13:21:06 -07:00
|
|
|
|
2020-04-21 06:58:54 -07:00
|
|
|
var result = new RemoteImageResult
|
|
|
|
{
|
|
|
|
TotalRecordCount = imageArray.Length,
|
|
|
|
Providers = allProviders.Select(o => o.Name)
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
|
|
.ToArray()
|
|
|
|
};
|
2020-04-20 13:21:06 -07:00
|
|
|
|
2020-04-21 06:58:54 -07:00
|
|
|
if (startIndex.HasValue)
|
|
|
|
{
|
|
|
|
imageArray = imageArray.Skip(startIndex.Value).ToArray();
|
2020-04-20 13:21:06 -07:00
|
|
|
}
|
2020-04-21 06:58:54 -07:00
|
|
|
|
|
|
|
if (limit.HasValue)
|
2020-04-20 13:21:06 -07:00
|
|
|
{
|
2020-04-21 06:58:54 -07:00
|
|
|
imageArray = imageArray.Take(limit.Value).ToArray();
|
2020-04-20 13:21:06 -07:00
|
|
|
}
|
2020-04-21 06:58:54 -07:00
|
|
|
|
|
|
|
result.Images = imageArray;
|
2020-05-19 08:23:28 -07:00
|
|
|
return result;
|
2020-04-20 13:21:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets available remote image providers for an item.
|
|
|
|
/// </summary>
|
2020-06-20 17:02:07 -07:00
|
|
|
/// <param name="itemId">Item Id.</param>
|
2020-05-19 08:23:28 -07:00
|
|
|
/// <response code="200">Returned remote image providers.</response>
|
|
|
|
/// <response code="404">Item not found.</response>
|
|
|
|
/// <returns>List of remote image providers.</returns>
|
2020-06-20 17:02:07 -07:00
|
|
|
[HttpGet("{itemId}/RemoteImages/Providers")]
|
2020-04-21 13:09:06 -07:00
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
2020-04-20 13:21:06 -07:00
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
2020-06-20 17:02:07 -07:00
|
|
|
public ActionResult<IEnumerable<ImageProviderInfo>> GetRemoteImageProviders([FromRoute] Guid itemId)
|
2020-04-20 13:21:06 -07:00
|
|
|
{
|
2020-06-20 17:02:07 -07:00
|
|
|
var item = _libraryManager.GetItemById(itemId);
|
2020-04-21 06:58:54 -07:00
|
|
|
if (item == null)
|
2020-04-20 13:21:06 -07:00
|
|
|
{
|
2020-04-21 06:58:54 -07:00
|
|
|
return NotFound();
|
2020-04-20 13:21:06 -07:00
|
|
|
}
|
2020-04-21 06:58:54 -07:00
|
|
|
|
2020-05-19 08:23:28 -07:00
|
|
|
return Ok(_providerManager.GetRemoteImageProviderInfo(item));
|
2020-04-20 13:21:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a remote image.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="imageUrl">The image url.</param>
|
2020-05-19 08:23:28 -07:00
|
|
|
/// <response code="200">Remote image returned.</response>
|
|
|
|
/// <response code="404">Remote image not found.</response>
|
2020-04-20 13:21:06 -07:00
|
|
|
/// <returns>Image Stream.</returns>
|
|
|
|
[HttpGet("Remote")]
|
2020-05-20 06:18:51 -07:00
|
|
|
[Produces(MediaTypeNames.Application.Octet)]
|
2020-04-21 13:09:06 -07:00
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
2020-04-20 13:21:06 -07:00
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
2020-04-21 13:09:06 -07:00
|
|
|
public async Task<ActionResult<FileStreamResult>> GetRemoteImage([FromQuery, BindRequired] string imageUrl)
|
2020-04-20 13:21:06 -07:00
|
|
|
{
|
2020-04-21 06:58:54 -07:00
|
|
|
var urlHash = imageUrl.GetMD5();
|
|
|
|
var pointerCachePath = GetFullCachePath(urlHash.ToString());
|
2020-04-20 13:21:06 -07:00
|
|
|
|
2020-04-21 06:58:54 -07:00
|
|
|
string? contentPath = null;
|
2020-05-19 08:23:28 -07:00
|
|
|
var hasFile = false;
|
2020-04-20 13:21:06 -07:00
|
|
|
|
2020-04-21 06:58:54 -07:00
|
|
|
try
|
|
|
|
{
|
|
|
|
contentPath = await System.IO.File.ReadAllTextAsync(pointerCachePath).ConfigureAwait(false);
|
|
|
|
if (System.IO.File.Exists(contentPath))
|
2020-04-20 13:21:06 -07:00
|
|
|
{
|
2020-04-21 06:58:54 -07:00
|
|
|
hasFile = true;
|
2020-04-20 13:21:06 -07:00
|
|
|
}
|
2020-04-21 06:58:54 -07:00
|
|
|
}
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
{
|
2020-05-19 08:23:28 -07:00
|
|
|
// The file isn't cached yet
|
2020-04-21 06:58:54 -07:00
|
|
|
}
|
|
|
|
catch (IOException)
|
|
|
|
{
|
2020-05-19 08:23:28 -07:00
|
|
|
// The file isn't cached yet
|
2020-04-21 06:58:54 -07:00
|
|
|
}
|
2020-04-20 13:21:06 -07:00
|
|
|
|
2020-04-21 06:58:54 -07:00
|
|
|
if (!hasFile)
|
|
|
|
{
|
|
|
|
await DownloadImage(imageUrl, urlHash, pointerCachePath).ConfigureAwait(false);
|
|
|
|
contentPath = await System.IO.File.ReadAllTextAsync(pointerCachePath).ConfigureAwait(false);
|
2020-04-20 13:21:06 -07:00
|
|
|
}
|
2020-04-21 06:58:54 -07:00
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(contentPath))
|
2020-04-20 13:21:06 -07:00
|
|
|
{
|
2020-04-21 06:58:54 -07:00
|
|
|
return NotFound();
|
2020-04-20 13:21:06 -07:00
|
|
|
}
|
2020-04-21 06:58:54 -07:00
|
|
|
|
|
|
|
var contentType = MimeTypes.GetMimeType(contentPath);
|
2020-06-17 09:51:50 -07:00
|
|
|
return File(System.IO.File.OpenRead(contentPath), contentType);
|
2020-04-20 13:21:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Downloads a remote image for an item.
|
|
|
|
/// </summary>
|
2020-06-20 17:02:07 -07:00
|
|
|
/// <param name="itemId">Item Id.</param>
|
2020-04-20 13:21:06 -07:00
|
|
|
/// <param name="type">The image type.</param>
|
|
|
|
/// <param name="imageUrl">The image url.</param>
|
2020-06-20 17:02:07 -07:00
|
|
|
/// <response code="204">Remote image downloaded.</response>
|
2020-05-19 08:23:28 -07:00
|
|
|
/// <response code="404">Remote image not found.</response>
|
|
|
|
/// <returns>Download status.</returns>
|
2020-06-20 17:02:07 -07:00
|
|
|
[HttpPost("{itemId}/RemoteImages/Download")]
|
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
2020-04-20 13:21:06 -07:00
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
2020-04-21 13:09:06 -07:00
|
|
|
public async Task<ActionResult> DownloadRemoteImage(
|
2020-06-20 17:02:07 -07:00
|
|
|
[FromRoute] Guid itemId,
|
2020-04-20 13:21:06 -07:00
|
|
|
[FromQuery, BindRequired] ImageType type,
|
2020-06-27 09:50:44 -07:00
|
|
|
[FromQuery] string? imageUrl)
|
2020-04-20 13:21:06 -07:00
|
|
|
{
|
2020-06-20 17:02:07 -07:00
|
|
|
var item = _libraryManager.GetItemById(itemId);
|
2020-04-21 06:58:54 -07:00
|
|
|
if (item == null)
|
2020-04-20 13:21:06 -07:00
|
|
|
{
|
2020-04-21 06:58:54 -07:00
|
|
|
return NotFound();
|
|
|
|
}
|
2020-04-20 13:21:06 -07:00
|
|
|
|
2020-04-21 06:58:54 -07:00
|
|
|
await _providerManager.SaveImage(item, imageUrl, type, null, CancellationToken.None)
|
|
|
|
.ConfigureAwait(false);
|
2020-04-20 13:21:06 -07:00
|
|
|
|
2020-04-21 06:58:54 -07:00
|
|
|
item.UpdateToRepository(ItemUpdateType.ImageUpdate, CancellationToken.None);
|
2020-06-20 17:02:07 -07:00
|
|
|
return NoContent();
|
2020-04-20 13:21:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
{
|
|
|
|
using var result = await _httpClient.GetResponse(new HttpRequestOptions
|
|
|
|
{
|
|
|
|
Url = url,
|
|
|
|
BufferContent = false
|
|
|
|
}).ConfigureAwait(false);
|
|
|
|
var ext = result.ContentType.Split('/').Last();
|
|
|
|
|
|
|
|
var fullCachePath = GetFullCachePath(urlHash + "." + ext);
|
|
|
|
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(fullCachePath));
|
2020-05-19 08:23:28 -07:00
|
|
|
await using (var stream = result.Content)
|
2020-04-20 13:21:06 -07:00
|
|
|
{
|
2020-05-19 08:23:28 -07:00
|
|
|
await using var fileStream = new FileStream(fullCachePath, FileMode.Create, FileAccess.Write, FileShare.Read, IODefaults.FileStreamBufferSize, true);
|
|
|
|
await stream.CopyToAsync(fileStream).ConfigureAwait(false);
|
2020-04-20 13:21:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(pointerCachePath));
|
|
|
|
await System.IO.File.WriteAllTextAsync(pointerCachePath, fullCachePath, CancellationToken.None)
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|