| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.ComponentModel.DataAnnotations; |
| | | 4 | | using System.IO; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using Jellyfin.Api.Constants; |
| | | 9 | | using Jellyfin.Api.Extensions; |
| | | 10 | | using Jellyfin.Api.Helpers; |
| | | 11 | | using MediaBrowser.Common.Api; |
| | | 12 | | using MediaBrowser.Controller; |
| | | 13 | | using MediaBrowser.Controller.Entities; |
| | | 14 | | using MediaBrowser.Controller.Library; |
| | | 15 | | using MediaBrowser.Controller.Providers; |
| | | 16 | | using MediaBrowser.Model.Entities; |
| | | 17 | | using MediaBrowser.Model.Providers; |
| | | 18 | | using Microsoft.AspNetCore.Authorization; |
| | | 19 | | using Microsoft.AspNetCore.Http; |
| | | 20 | | using Microsoft.AspNetCore.Mvc; |
| | | 21 | | |
| | | 22 | | namespace Jellyfin.Api.Controllers; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Remote Images Controller. |
| | | 26 | | /// </summary> |
| | | 27 | | [Route("")] |
| | | 28 | | public class RemoteImageController : BaseJellyfinApiController |
| | | 29 | | { |
| | | 30 | | private readonly IProviderManager _providerManager; |
| | | 31 | | private readonly IServerApplicationPaths _applicationPaths; |
| | | 32 | | private readonly ILibraryManager _libraryManager; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Initializes a new instance of the <see cref="RemoteImageController"/> class. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="providerManager">Instance of the <see cref="IProviderManager"/> interface.</param> |
| | | 38 | | /// <param name="applicationPaths">Instance of the <see cref="IServerApplicationPaths"/> interface.</param> |
| | | 39 | | /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param> |
| | 0 | 40 | | public RemoteImageController( |
| | 0 | 41 | | IProviderManager providerManager, |
| | 0 | 42 | | IServerApplicationPaths applicationPaths, |
| | 0 | 43 | | ILibraryManager libraryManager) |
| | | 44 | | { |
| | 0 | 45 | | _providerManager = providerManager; |
| | 0 | 46 | | _applicationPaths = applicationPaths; |
| | 0 | 47 | | _libraryManager = libraryManager; |
| | 0 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets available remote images for an item. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name="itemId">Item Id.</param> |
| | | 54 | | /// <param name="type">The image type.</param> |
| | | 55 | | /// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped fr |
| | | 56 | | /// <param name="limit">Optional. The maximum number of records to return.</param> |
| | | 57 | | /// <param name="providerName">Optional. The image provider to use.</param> |
| | | 58 | | /// <param name="includeAllLanguages">Optional. Include all languages.</param> |
| | | 59 | | /// <response code="200">Remote Images returned.</response> |
| | | 60 | | /// <response code="404">Item not found.</response> |
| | | 61 | | /// <returns>Remote Image Result.</returns> |
| | | 62 | | [HttpGet("Items/{itemId}/RemoteImages")] |
| | | 63 | | [Authorize] |
| | | 64 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 65 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | | 66 | | public async Task<ActionResult<RemoteImageResult>> GetRemoteImages( |
| | | 67 | | [FromRoute, Required] Guid itemId, |
| | | 68 | | [FromQuery] ImageType? type, |
| | | 69 | | [FromQuery] int? startIndex, |
| | | 70 | | [FromQuery] int? limit, |
| | | 71 | | [FromQuery] string? providerName, |
| | | 72 | | [FromQuery] bool includeAllLanguages = false) |
| | | 73 | | { |
| | | 74 | | var item = _libraryManager.GetItemById<BaseItem>(itemId, User.GetUserId()); |
| | | 75 | | if (item is null) |
| | | 76 | | { |
| | | 77 | | return NotFound(); |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | var images = await _providerManager.GetAvailableRemoteImages( |
| | | 81 | | item, |
| | | 82 | | new RemoteImageQuery(providerName ?? string.Empty) |
| | | 83 | | { |
| | | 84 | | IncludeAllLanguages = includeAllLanguages, |
| | | 85 | | IncludeDisabledProviders = true, |
| | | 86 | | ImageType = type |
| | | 87 | | }, |
| | | 88 | | CancellationToken.None) |
| | | 89 | | .ConfigureAwait(false); |
| | | 90 | | |
| | | 91 | | var imageArray = images.ToArray(); |
| | | 92 | | var allProviders = _providerManager.GetRemoteImageProviderInfo(item); |
| | | 93 | | if (type.HasValue) |
| | | 94 | | { |
| | | 95 | | allProviders = allProviders.Where(o => o.SupportedImages.Contains(type.Value)); |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | var result = new RemoteImageResult |
| | | 99 | | { |
| | | 100 | | TotalRecordCount = imageArray.Length, |
| | | 101 | | Providers = allProviders.Select(o => o.Name) |
| | | 102 | | .Distinct(StringComparer.OrdinalIgnoreCase) |
| | | 103 | | .ToArray() |
| | | 104 | | }; |
| | | 105 | | |
| | | 106 | | if (startIndex.HasValue) |
| | | 107 | | { |
| | | 108 | | imageArray = imageArray.Skip(startIndex.Value).ToArray(); |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | if (limit.HasValue) |
| | | 112 | | { |
| | | 113 | | imageArray = imageArray.Take(limit.Value).ToArray(); |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | result.Images = imageArray; |
| | | 117 | | return result; |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Gets available remote image providers for an item. |
| | | 122 | | /// </summary> |
| | | 123 | | /// <param name="itemId">Item Id.</param> |
| | | 124 | | /// <response code="200">Returned remote image providers.</response> |
| | | 125 | | /// <response code="404">Item not found.</response> |
| | | 126 | | /// <returns>List of remote image providers.</returns> |
| | | 127 | | [HttpGet("Items/{itemId}/RemoteImages/Providers")] |
| | | 128 | | [Authorize] |
| | | 129 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 130 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | | 131 | | public ActionResult<IEnumerable<ImageProviderInfo>> GetRemoteImageProviders([FromRoute, Required] Guid itemId) |
| | | 132 | | { |
| | 0 | 133 | | var item = _libraryManager.GetItemById<BaseItem>(itemId, User.GetUserId()); |
| | 0 | 134 | | if (item is null) |
| | | 135 | | { |
| | 0 | 136 | | return NotFound(); |
| | | 137 | | } |
| | | 138 | | |
| | 0 | 139 | | return Ok(_providerManager.GetRemoteImageProviderInfo(item)); |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | /// <summary> |
| | | 143 | | /// Downloads a remote image for an item. |
| | | 144 | | /// </summary> |
| | | 145 | | /// <param name="itemId">Item Id.</param> |
| | | 146 | | /// <param name="type">The image type.</param> |
| | | 147 | | /// <param name="imageUrl">The image url.</param> |
| | | 148 | | /// <response code="204">Remote image downloaded.</response> |
| | | 149 | | /// <response code="404">Remote image not found.</response> |
| | | 150 | | /// <returns>Download status.</returns> |
| | | 151 | | [HttpPost("Items/{itemId}/RemoteImages/Download")] |
| | | 152 | | [Authorize(Policy = Policies.RequiresElevation)] |
| | | 153 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 154 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | | 155 | | public async Task<ActionResult> DownloadRemoteImage( |
| | | 156 | | [FromRoute, Required] Guid itemId, |
| | | 157 | | [FromQuery, Required] ImageType type, |
| | | 158 | | [FromQuery] string? imageUrl) |
| | | 159 | | { |
| | | 160 | | var item = _libraryManager.GetItemById<BaseItem>(itemId, User.GetUserId()); |
| | | 161 | | if (item is null) |
| | | 162 | | { |
| | | 163 | | return NotFound(); |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | await _providerManager.SaveImage(item, imageUrl, type, null, CancellationToken.None) |
| | | 167 | | .ConfigureAwait(false); |
| | | 168 | | |
| | | 169 | | await item.UpdateToRepositoryAsync(ItemUpdateType.ImageUpdate, CancellationToken.None).ConfigureAwait(false); |
| | | 170 | | return NoContent(); |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | /// <summary> |
| | | 174 | | /// Gets the full cache path. |
| | | 175 | | /// </summary> |
| | | 176 | | /// <param name="filename">The filename.</param> |
| | | 177 | | /// <returns>System.String.</returns> |
| | | 178 | | private string GetFullCachePath(string filename) |
| | | 179 | | { |
| | 0 | 180 | | return Path.Combine(_applicationPaths.CachePath, "remote-images", filename.Substring(0, 1), filename); |
| | | 181 | | } |
| | | 182 | | } |