2019-01-13 20:01:16 +00:00
|
|
|
using System;
|
2019-01-13 19:24:58 +00:00
|
|
|
using System.Collections.Generic;
|
2019-02-28 22:22:57 +00:00
|
|
|
using System.Globalization;
|
2019-01-13 19:24:58 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2020-05-19 11:50:14 +00:00
|
|
|
using System.Runtime.CompilerServices;
|
2019-01-13 19:24:58 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using MediaBrowser.Common.Extensions;
|
2019-11-17 22:05:39 +00:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-09-18 18:49:06 +00:00
|
|
|
using MediaBrowser.Controller.Drawing;
|
2019-01-13 19:24:58 +00:00
|
|
|
using MediaBrowser.Controller.Dto;
|
2013-02-21 01:33:05 +00:00
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-07-04 02:22:57 +00:00
|
|
|
using MediaBrowser.Controller.Net;
|
2013-05-05 04:49:49 +00:00
|
|
|
using MediaBrowser.Controller.Providers;
|
2013-09-19 19:46:19 +00:00
|
|
|
using MediaBrowser.Model.Drawing;
|
2013-05-05 04:49:49 +00:00
|
|
|
using MediaBrowser.Model.Dto;
|
2013-02-21 01:33:05 +00:00
|
|
|
using MediaBrowser.Model.Entities;
|
2016-10-25 19:02:04 +00:00
|
|
|
using MediaBrowser.Model.IO;
|
2020-05-20 02:12:03 +00:00
|
|
|
using MediaBrowser.Model.Net;
|
2016-10-25 19:02:04 +00:00
|
|
|
using MediaBrowser.Model.Services;
|
2018-12-13 13:18:25 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2019-03-05 18:20:28 +00:00
|
|
|
using Microsoft.Net.Http.Headers;
|
2020-05-13 02:10:35 +00:00
|
|
|
using User = Jellyfin.Data.Entities.User;
|
2018-12-13 13:18:25 +00:00
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
namespace MediaBrowser.Api.Images
|
|
|
|
{
|
|
|
|
/// <summary>
|
2020-01-21 19:26:30 +00:00
|
|
|
/// Class GetItemImage.
|
2013-02-21 01:33:05 +00:00
|
|
|
/// </summary>
|
2014-11-15 02:31:03 +00:00
|
|
|
[Route("/Items/{Id}/Images", "GET", Summary = "Gets information about an item's images")]
|
2014-07-04 02:22:57 +00:00
|
|
|
[Authenticated]
|
2013-05-05 04:49:49 +00:00
|
|
|
public class GetItemImageInfos : IReturn<List<ImageInfo>>
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the id.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The id.</value>
|
|
|
|
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
|
|
|
|
public string Id { get; set; }
|
|
|
|
}
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
[Route("/Items/{Id}/Images/{Type}", "GET")]
|
|
|
|
[Route("/Items/{Id}/Images/{Type}/{Index}", "GET")]
|
2014-09-26 03:47:46 +00:00
|
|
|
[Route("/Items/{Id}/Images/{Type}", "HEAD")]
|
|
|
|
[Route("/Items/{Id}/Images/{Type}/{Index}", "HEAD")]
|
2015-04-16 14:59:39 +00:00
|
|
|
[Route("/Items/{Id}/Images/{Type}/{Index}/{Tag}/{Format}/{MaxWidth}/{MaxHeight}/{PercentPlayed}/{UnplayedCount}", "GET")]
|
|
|
|
[Route("/Items/{Id}/Images/{Type}/{Index}/{Tag}/{Format}/{MaxWidth}/{MaxHeight}/{PercentPlayed}/{UnplayedCount}", "HEAD")]
|
2013-02-21 01:33:05 +00:00
|
|
|
public class GetItemImage : ImageRequest
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the id.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The id.</value>
|
2018-09-12 17:26:21 +00:00
|
|
|
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path")]
|
|
|
|
public Guid Id { get; set; }
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
|
2013-05-05 04:49:49 +00:00
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// Class UpdateItemImageIndex.
|
2013-05-05 04:49:49 +00:00
|
|
|
/// </summary>
|
2014-11-15 02:31:03 +00:00
|
|
|
[Route("/Items/{Id}/Images/{Type}/{Index}/Index", "POST", Summary = "Updates the index for an item image")]
|
2015-03-16 01:48:25 +00:00
|
|
|
[Authenticated(Roles = "admin")]
|
2013-05-05 04:49:49 +00:00
|
|
|
public class UpdateItemImageIndex : IReturnVoid
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the id.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The id.</value>
|
2018-09-12 17:26:21 +00:00
|
|
|
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
|
2015-03-16 01:48:25 +00:00
|
|
|
public string Id { get; set; }
|
2013-05-05 04:49:49 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the type of the image.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The type of the image.</value>
|
|
|
|
[ApiMember(Name = "Type", Description = "Image Type", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
|
|
|
|
public ImageType Type { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the index.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The index.</value>
|
|
|
|
[ApiMember(Name = "Index", Description = "Image Index", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "POST")]
|
|
|
|
public int Index { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the new index.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The new index.</value>
|
2018-09-12 17:26:21 +00:00
|
|
|
[ApiMember(Name = "NewIndex", Description = "The new image index", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
|
2013-05-05 04:49:49 +00:00
|
|
|
public int NewIndex { get; set; }
|
|
|
|
}
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// Class GetPersonImage.
|
2013-02-21 01:33:05 +00:00
|
|
|
/// </summary>
|
2013-08-03 14:38:56 +00:00
|
|
|
[Route("/Artists/{Name}/Images/{Type}", "GET")]
|
|
|
|
[Route("/Artists/{Name}/Images/{Type}/{Index}", "GET")]
|
2013-02-21 01:33:05 +00:00
|
|
|
[Route("/Genres/{Name}/Images/{Type}", "GET")]
|
|
|
|
[Route("/Genres/{Name}/Images/{Type}/{Index}", "GET")]
|
2013-06-11 03:31:00 +00:00
|
|
|
[Route("/MusicGenres/{Name}/Images/{Type}", "GET")]
|
|
|
|
[Route("/MusicGenres/{Name}/Images/{Type}/{Index}", "GET")]
|
2013-08-03 14:38:56 +00:00
|
|
|
[Route("/Persons/{Name}/Images/{Type}", "GET")]
|
|
|
|
[Route("/Persons/{Name}/Images/{Type}/{Index}", "GET")]
|
|
|
|
[Route("/Studios/{Name}/Images/{Type}", "GET")]
|
|
|
|
[Route("/Studios/{Name}/Images/{Type}/{Index}", "GET")]
|
2018-09-12 17:26:21 +00:00
|
|
|
////[Route("/Years/{Year}/Images/{Type}", "GET")]
|
|
|
|
////[Route("/Years/{Year}/Images/{Type}/{Index}", "GET")]
|
2014-09-27 02:28:13 +00:00
|
|
|
[Route("/Artists/{Name}/Images/{Type}", "HEAD")]
|
|
|
|
[Route("/Artists/{Name}/Images/{Type}/{Index}", "HEAD")]
|
|
|
|
[Route("/Genres/{Name}/Images/{Type}", "HEAD")]
|
|
|
|
[Route("/Genres/{Name}/Images/{Type}/{Index}", "HEAD")]
|
|
|
|
[Route("/MusicGenres/{Name}/Images/{Type}", "HEAD")]
|
|
|
|
[Route("/MusicGenres/{Name}/Images/{Type}/{Index}", "HEAD")]
|
|
|
|
[Route("/Persons/{Name}/Images/{Type}", "HEAD")]
|
|
|
|
[Route("/Persons/{Name}/Images/{Type}/{Index}", "HEAD")]
|
|
|
|
[Route("/Studios/{Name}/Images/{Type}", "HEAD")]
|
|
|
|
[Route("/Studios/{Name}/Images/{Type}/{Index}", "HEAD")]
|
2018-09-12 17:26:21 +00:00
|
|
|
////[Route("/Years/{Year}/Images/{Type}", "HEAD")]
|
|
|
|
////[Route("/Years/{Year}/Images/{Type}/{Index}", "HEAD")]
|
2013-08-03 14:38:56 +00:00
|
|
|
public class GetItemByNameImage : ImageRequest
|
2013-06-11 03:31:00 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the name.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The name.</value>
|
2013-08-03 14:38:56 +00:00
|
|
|
[ApiMember(Name = "Name", Description = "Item name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
|
2013-06-11 03:31:00 +00:00
|
|
|
public string Name { get; set; }
|
|
|
|
}
|
2013-07-01 17:17:33 +00:00
|
|
|
|
2013-08-03 14:38:56 +00:00
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// Class GetUserImage.
|
2013-08-03 14:38:56 +00:00
|
|
|
/// </summary>
|
|
|
|
[Route("/Users/{Id}/Images/{Type}", "GET")]
|
|
|
|
[Route("/Users/{Id}/Images/{Type}/{Index}", "GET")]
|
2014-09-30 04:47:30 +00:00
|
|
|
[Route("/Users/{Id}/Images/{Type}", "HEAD")]
|
|
|
|
[Route("/Users/{Id}/Images/{Type}/{Index}", "HEAD")]
|
2013-08-03 14:38:56 +00:00
|
|
|
public class GetUserImage : ImageRequest
|
2013-07-01 17:17:33 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
2013-08-03 14:38:56 +00:00
|
|
|
/// Gets or sets the id.
|
2013-07-01 17:17:33 +00:00
|
|
|
/// </summary>
|
2013-08-03 14:38:56 +00:00
|
|
|
/// <value>The id.</value>
|
|
|
|
[ApiMember(Name = "Id", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
|
2018-09-12 17:26:21 +00:00
|
|
|
public Guid Id { get; set; }
|
2013-07-01 17:17:33 +00:00
|
|
|
}
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// Class DeleteItemImage.
|
2013-02-21 01:33:05 +00:00
|
|
|
/// </summary>
|
2013-08-03 14:38:56 +00:00
|
|
|
[Route("/Items/{Id}/Images/{Type}", "DELETE")]
|
|
|
|
[Route("/Items/{Id}/Images/{Type}/{Index}", "DELETE")]
|
2015-03-16 01:48:25 +00:00
|
|
|
[Authenticated(Roles = "admin")]
|
2013-08-03 14:38:56 +00:00
|
|
|
public class DeleteItemImage : DeleteImageRequest, IReturnVoid
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
2013-08-03 14:38:56 +00:00
|
|
|
/// Gets or sets the id.
|
2013-02-21 01:33:05 +00:00
|
|
|
/// </summary>
|
2013-08-03 14:38:56 +00:00
|
|
|
/// <value>The id.</value>
|
|
|
|
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
|
2015-03-16 01:48:25 +00:00
|
|
|
public string Id { get; set; }
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// Class DeleteUserImage.
|
2013-02-21 01:33:05 +00:00
|
|
|
/// </summary>
|
|
|
|
[Route("/Users/{Id}/Images/{Type}", "DELETE")]
|
|
|
|
[Route("/Users/{Id}/Images/{Type}/{Index}", "DELETE")]
|
2014-07-04 02:22:57 +00:00
|
|
|
[Authenticated]
|
2013-02-25 04:39:53 +00:00
|
|
|
public class DeleteUserImage : DeleteImageRequest, IReturnVoid
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the id.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The id.</value>
|
2013-03-08 21:06:02 +00:00
|
|
|
[ApiMember(Name = "Id", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
|
2018-09-12 17:26:21 +00:00
|
|
|
public Guid Id { get; set; }
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
2013-02-25 04:39:53 +00:00
|
|
|
|
2013-05-05 04:49:49 +00:00
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// Class PostUserImage.
|
2013-05-05 04:49:49 +00:00
|
|
|
/// </summary>
|
2013-02-25 04:39:53 +00:00
|
|
|
[Route("/Users/{Id}/Images/{Type}", "POST")]
|
|
|
|
[Route("/Users/{Id}/Images/{Type}/{Index}", "POST")]
|
2014-07-04 02:22:57 +00:00
|
|
|
[Authenticated]
|
2013-02-25 04:39:53 +00:00
|
|
|
public class PostUserImage : DeleteImageRequest, IRequiresRequestStream, IReturnVoid
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the id.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The id.</value>
|
2013-03-08 21:06:02 +00:00
|
|
|
[ApiMember(Name = "Id", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
|
2015-03-16 01:48:25 +00:00
|
|
|
public string Id { get; set; }
|
2013-02-25 04:39:53 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// The raw Http Request Input Stream.
|
2013-02-25 04:39:53 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <value>The request stream.</value>
|
|
|
|
public Stream RequestStream { get; set; }
|
|
|
|
}
|
2013-05-04 21:20:27 +00:00
|
|
|
|
2013-05-05 04:49:49 +00:00
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// Class PostItemImage.
|
2013-05-05 04:49:49 +00:00
|
|
|
/// </summary>
|
2013-05-04 21:20:27 +00:00
|
|
|
[Route("/Items/{Id}/Images/{Type}", "POST")]
|
|
|
|
[Route("/Items/{Id}/Images/{Type}/{Index}", "POST")]
|
2015-03-16 01:48:25 +00:00
|
|
|
[Authenticated(Roles = "admin")]
|
2013-05-04 21:20:27 +00:00
|
|
|
public class PostItemImage : DeleteImageRequest, IRequiresRequestStream, IReturnVoid
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the id.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The id.</value>
|
|
|
|
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
|
|
|
|
public string Id { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// The raw Http Request Input Stream.
|
2013-05-04 21:20:27 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <value>The request stream.</value>
|
|
|
|
public Stream RequestStream { get; set; }
|
|
|
|
}
|
2013-05-05 04:49:49 +00:00
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// Class ImageService.
|
2013-02-21 01:33:05 +00:00
|
|
|
/// </summary>
|
2013-03-16 05:52:33 +00:00
|
|
|
public class ImageService : BaseApiService
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
2013-02-27 20:25:45 +00:00
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
|
2013-02-28 19:32:41 +00:00
|
|
|
private readonly ILibraryManager _libraryManager;
|
2013-03-31 17:39:28 +00:00
|
|
|
|
2013-05-05 04:49:49 +00:00
|
|
|
private readonly IProviderManager _providerManager;
|
|
|
|
|
2013-09-18 18:49:06 +00:00
|
|
|
private readonly IImageProcessor _imageProcessor;
|
2014-02-07 20:30:41 +00:00
|
|
|
private readonly IFileSystem _fileSystem;
|
2016-11-10 14:41:24 +00:00
|
|
|
private readonly IAuthorizationContext _authContext;
|
2013-12-05 16:50:21 +00:00
|
|
|
|
2013-02-27 20:25:45 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="ImageService" /> class.
|
|
|
|
/// </summary>
|
2019-11-17 22:05:39 +00:00
|
|
|
public ImageService(
|
2019-12-10 16:07:23 +00:00
|
|
|
ILogger<ImageService> logger,
|
2019-11-17 22:05:39 +00:00
|
|
|
IServerConfigurationManager serverConfigurationManager,
|
|
|
|
IHttpResultFactory httpResultFactory,
|
|
|
|
IUserManager userManager,
|
|
|
|
ILibraryManager libraryManager,
|
|
|
|
IProviderManager providerManager,
|
|
|
|
IImageProcessor imageProcessor,
|
|
|
|
IFileSystem fileSystem,
|
|
|
|
IAuthorizationContext authContext)
|
|
|
|
: base(logger, serverConfigurationManager, httpResultFactory)
|
2013-02-27 20:25:45 +00:00
|
|
|
{
|
|
|
|
_userManager = userManager;
|
2013-02-28 19:32:41 +00:00
|
|
|
_libraryManager = libraryManager;
|
2013-05-05 04:49:49 +00:00
|
|
|
_providerManager = providerManager;
|
2013-09-18 18:49:06 +00:00
|
|
|
_imageProcessor = imageProcessor;
|
2014-02-07 20:30:41 +00:00
|
|
|
_fileSystem = fileSystem;
|
2016-11-10 14:41:24 +00:00
|
|
|
_authContext = authContext;
|
2013-02-27 20:25:45 +00:00
|
|
|
}
|
|
|
|
|
2013-05-05 04:49:49 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the specified request.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
/// <returns>System.Object.</returns>
|
|
|
|
public object Get(GetItemImageInfos request)
|
|
|
|
{
|
2014-04-25 20:15:50 +00:00
|
|
|
var item = _libraryManager.GetItemById(request.Id);
|
2013-05-05 04:49:49 +00:00
|
|
|
|
2013-09-17 02:44:06 +00:00
|
|
|
var result = GetItemImageInfos(item);
|
2013-05-05 04:49:49 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
return ToOptimizedResult(result);
|
2013-05-05 04:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the item image infos.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
/// <returns>Task{List{ImageInfo}}.</returns>
|
2013-09-17 02:44:06 +00:00
|
|
|
public List<ImageInfo> GetItemImageInfos(BaseItem item)
|
2013-05-05 04:49:49 +00:00
|
|
|
{
|
|
|
|
var list = new List<ImageInfo>();
|
2016-07-04 20:11:30 +00:00
|
|
|
var itemImages = item.ImageInfos;
|
|
|
|
|
2020-05-19 11:50:14 +00:00
|
|
|
if (itemImages.Length == 0)
|
|
|
|
{
|
|
|
|
// short-circuit
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
_libraryManager.UpdateImages(item); // this makes sure dimensions and hashes are correct
|
|
|
|
|
2017-08-09 19:56:38 +00:00
|
|
|
foreach (var image in itemImages)
|
2013-05-05 04:49:49 +00:00
|
|
|
{
|
2017-08-09 19:56:38 +00:00
|
|
|
if (!item.AllowsMultipleImages(image.Type))
|
2013-05-05 04:49:49 +00:00
|
|
|
{
|
2017-08-09 19:56:38 +00:00
|
|
|
var info = GetImageInfo(item, image, null);
|
|
|
|
|
|
|
|
if (info != null)
|
|
|
|
{
|
|
|
|
list.Add(info);
|
|
|
|
}
|
2013-11-15 15:09:52 +00:00
|
|
|
}
|
2013-05-05 04:49:49 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 20:11:30 +00:00
|
|
|
foreach (var imageType in itemImages.Select(i => i.Type).Distinct().Where(item.AllowsMultipleImages))
|
2013-05-05 04:49:49 +00:00
|
|
|
{
|
2014-02-07 20:30:41 +00:00
|
|
|
var index = 0;
|
2013-05-05 04:49:49 +00:00
|
|
|
|
2014-02-07 20:30:41 +00:00
|
|
|
// Prevent implicitly captured closure
|
|
|
|
var currentImageType = imageType;
|
2013-05-18 17:07:20 +00:00
|
|
|
|
2016-07-04 20:11:30 +00:00
|
|
|
foreach (var image in itemImages.Where(i => i.Type == currentImageType))
|
2013-05-05 04:49:49 +00:00
|
|
|
{
|
2014-02-07 20:30:41 +00:00
|
|
|
var info = GetImageInfo(item, image, index);
|
2013-05-05 04:49:49 +00:00
|
|
|
|
2013-12-05 16:50:21 +00:00
|
|
|
if (info != null)
|
|
|
|
{
|
|
|
|
list.Add(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
index++;
|
|
|
|
}
|
2013-05-05 04:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
2013-05-18 17:07:20 +00:00
|
|
|
|
2017-10-22 06:22:43 +00:00
|
|
|
private ImageInfo GetImageInfo(BaseItem item, ItemImageInfo info, int? imageIndex)
|
2013-11-15 15:09:52 +00:00
|
|
|
{
|
2019-03-07 14:54:30 +00:00
|
|
|
int? width = null;
|
|
|
|
int? height = null;
|
2020-05-19 11:50:14 +00:00
|
|
|
string blurhash = null;
|
2019-03-07 14:54:30 +00:00
|
|
|
long length = 0;
|
|
|
|
|
2013-11-15 15:09:52 +00:00
|
|
|
try
|
|
|
|
{
|
2019-03-07 14:54:30 +00:00
|
|
|
if (info.IsLocalFile)
|
2014-10-15 00:05:09 +00:00
|
|
|
{
|
2019-03-07 14:54:30 +00:00
|
|
|
var fileInfo = _fileSystem.GetFileInfo(info.Path);
|
|
|
|
length = fileInfo.Length;
|
2015-10-18 13:10:30 +00:00
|
|
|
|
2020-05-20 22:43:19 +00:00
|
|
|
blurhash = info.BlurHash;
|
2020-05-19 11:50:14 +00:00
|
|
|
width = info.Width;
|
|
|
|
height = info.Height;
|
2017-10-22 06:22:43 +00:00
|
|
|
|
2019-03-07 14:54:30 +00:00
|
|
|
if (width <= 0 || height <= 0)
|
|
|
|
{
|
|
|
|
width = null;
|
|
|
|
height = null;
|
2015-10-16 17:06:31 +00:00
|
|
|
}
|
2014-10-15 00:05:09 +00:00
|
|
|
}
|
2019-03-07 14:54:30 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Logger.LogError(ex, "Error getting image information for {Item}", item.Name);
|
|
|
|
}
|
2014-10-15 00:05:09 +00:00
|
|
|
|
2019-03-07 14:54:30 +00:00
|
|
|
try
|
|
|
|
{
|
2013-11-15 15:09:52 +00:00
|
|
|
return new ImageInfo
|
|
|
|
{
|
2014-02-07 20:30:41 +00:00
|
|
|
Path = info.Path,
|
2013-11-15 15:09:52 +00:00
|
|
|
ImageIndex = imageIndex,
|
2014-02-07 20:30:41 +00:00
|
|
|
ImageType = info.Type,
|
|
|
|
ImageTag = _imageProcessor.GetImageCacheTag(item, info),
|
2015-10-18 13:10:30 +00:00
|
|
|
Size = length,
|
2020-05-25 21:52:13 +00:00
|
|
|
BlurHash = blurhash,
|
2014-10-15 00:05:09 +00:00
|
|
|
Width = width,
|
|
|
|
Height = height
|
2013-11-15 15:09:52 +00:00
|
|
|
};
|
|
|
|
}
|
2013-11-23 20:01:09 +00:00
|
|
|
catch (Exception ex)
|
2013-11-15 15:09:52 +00:00
|
|
|
{
|
2019-03-07 14:54:30 +00:00
|
|
|
Logger.LogError(ex, "Error getting image information for {Path}", info.Path);
|
2013-11-15 15:09:52 +00:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the specified request.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
/// <returns>System.Object.</returns>
|
|
|
|
public object Get(GetItemImage request)
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
return GetImage(request, request.Id, null, false);
|
2014-08-08 04:36:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the specified request.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
/// <returns>System.Object.</returns>
|
|
|
|
public object Head(GetItemImage request)
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
return GetImage(request, request.Id, null, true);
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the specified request.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
/// <returns>System.Object.</returns>
|
|
|
|
public object Get(GetUserImage request)
|
|
|
|
{
|
2014-09-28 15:27:26 +00:00
|
|
|
var item = _userManager.GetUserById(request.Id);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2020-05-13 02:10:35 +00:00
|
|
|
return GetImage(request, item, false);
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 04:47:30 +00:00
|
|
|
public object Head(GetUserImage request)
|
|
|
|
{
|
|
|
|
var item = _userManager.GetUserById(request.Id);
|
|
|
|
|
2020-05-13 02:10:35 +00:00
|
|
|
return GetImage(request, item, true);
|
2014-09-30 04:47:30 +00:00
|
|
|
}
|
|
|
|
|
2013-08-03 14:38:56 +00:00
|
|
|
public object Get(GetItemByNameImage request)
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
2019-11-17 22:05:39 +00:00
|
|
|
var type = GetPathValue(0).ToString();
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2017-05-22 04:54:02 +00:00
|
|
|
var item = GetItemByName(request.Name, type, _libraryManager, new DtoOptions(false));
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
return GetImage(request, item.Id, item, false);
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
|
2014-09-27 02:28:13 +00:00
|
|
|
public object Head(GetItemByNameImage request)
|
|
|
|
{
|
2019-11-17 22:05:39 +00:00
|
|
|
var type = GetPathValue(0).ToString();
|
2014-09-27 02:28:13 +00:00
|
|
|
|
2017-05-22 04:54:02 +00:00
|
|
|
var item = GetItemByName(request.Name, type, _libraryManager, new DtoOptions(false));
|
2014-09-27 02:28:13 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
return GetImage(request, item.Id, item, true);
|
2014-09-27 02:28:13 +00:00
|
|
|
}
|
|
|
|
|
2013-04-22 04:38:03 +00:00
|
|
|
/// <summary>
|
2013-08-03 14:38:56 +00:00
|
|
|
/// Posts the specified request.
|
2013-04-22 04:38:03 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="request">The request.</param>
|
2018-09-12 17:26:21 +00:00
|
|
|
public Task Post(PostUserImage request)
|
2013-04-22 04:38:03 +00:00
|
|
|
{
|
2019-11-17 22:05:39 +00:00
|
|
|
var id = Guid.Parse(GetPathValue(1));
|
2013-05-05 04:49:49 +00:00
|
|
|
|
2019-11-17 22:05:39 +00:00
|
|
|
AssertCanUpdateUser(_authContext, _userManager, id, true);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2019-11-17 22:05:39 +00:00
|
|
|
request.Type = Enum.Parse<ImageType>(GetPathValue(3).ToString(), true);
|
|
|
|
|
2020-05-13 02:10:35 +00:00
|
|
|
var user = _userManager.GetUserById(id);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2020-05-13 02:10:35 +00:00
|
|
|
return PostImage(user, request.RequestStream, Request.ContentType);
|
2013-06-11 03:31:00 +00:00
|
|
|
}
|
|
|
|
|
2013-05-04 21:20:27 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Posts the specified request.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request">The request.</param>
|
2018-09-12 17:26:21 +00:00
|
|
|
public Task Post(PostItemImage request)
|
2013-05-04 21:20:27 +00:00
|
|
|
{
|
2019-11-17 22:05:39 +00:00
|
|
|
var id = Guid.Parse(GetPathValue(1));
|
2013-05-04 21:20:27 +00:00
|
|
|
|
2019-11-17 22:05:39 +00:00
|
|
|
request.Type = Enum.Parse<ImageType>(GetPathValue(3).ToString(), true);
|
2013-05-04 21:20:27 +00:00
|
|
|
|
|
|
|
var item = _libraryManager.GetItemById(id);
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
return PostImage(item, request.RequestStream, request.Type, Request.ContentType);
|
2013-05-04 21:20:27 +00:00
|
|
|
}
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Deletes the specified request.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
public void Delete(DeleteUserImage request)
|
|
|
|
{
|
2015-03-16 01:48:25 +00:00
|
|
|
var userId = request.Id;
|
2017-04-21 20:03:07 +00:00
|
|
|
AssertCanUpdateUser(_authContext, _userManager, userId, true);
|
2015-03-16 01:48:25 +00:00
|
|
|
|
2020-05-13 02:10:35 +00:00
|
|
|
var user = _userManager.GetUserById(userId);
|
2020-05-20 16:09:52 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
File.Delete(user.ProfileImage.Path);
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
2020-05-27 03:08:27 +00:00
|
|
|
Logger.LogError(e, "Error deleting user profile image:");
|
2020-05-20 16:09:52 +00:00
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2020-06-11 21:51:02 +00:00
|
|
|
_userManager.ClearProfileImage(user);
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
2013-05-05 04:49:49 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Deletes the specified request.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
public void Delete(DeleteItemImage request)
|
|
|
|
{
|
|
|
|
var item = _libraryManager.GetItemById(request.Id);
|
|
|
|
|
2017-10-03 18:39:37 +00:00
|
|
|
item.DeleteImage(request.Type, request.Index ?? 0);
|
2013-05-05 04:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Posts the specified request.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
public void Post(UpdateItemImageIndex request)
|
|
|
|
{
|
|
|
|
var item = _libraryManager.GetItemById(request.Id);
|
|
|
|
|
2017-10-03 18:39:37 +00:00
|
|
|
UpdateItemIndex(item, request.Type, request.Index, request.NewIndex);
|
2013-05-05 04:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Updates the index of the item.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
/// <param name="type">The type.</param>
|
|
|
|
/// <param name="currentIndex">Index of the current.</param>
|
|
|
|
/// <param name="newIndex">The new index.</param>
|
|
|
|
/// <returns>Task.</returns>
|
2018-09-12 17:26:21 +00:00
|
|
|
private void UpdateItemIndex(BaseItem item, ImageType type, int currentIndex, int newIndex)
|
2013-05-05 04:49:49 +00:00
|
|
|
{
|
2017-10-03 18:39:37 +00:00
|
|
|
item.SwapImages(type, currentIndex, newIndex);
|
2013-05-05 04:49:49 +00:00
|
|
|
}
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the image.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
/// <param name="item">The item.</param>
|
2014-08-08 04:36:51 +00:00
|
|
|
/// <param name="isHeadRequest">if set to <c>true</c> [is head request].</param>
|
2013-02-21 01:33:05 +00:00
|
|
|
/// <returns>System.Object.</returns>
|
2014-08-08 04:36:51 +00:00
|
|
|
/// <exception cref="ResourceNotFoundException"></exception>
|
2018-09-12 17:26:21 +00:00
|
|
|
public Task<object> GetImage(ImageRequest request, Guid itemId, BaseItem item, bool isHeadRequest)
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
2015-04-16 14:59:39 +00:00
|
|
|
if (request.PercentPlayed.HasValue)
|
|
|
|
{
|
|
|
|
if (request.PercentPlayed.Value <= 0)
|
|
|
|
{
|
|
|
|
request.PercentPlayed = null;
|
|
|
|
}
|
|
|
|
else if (request.PercentPlayed.Value >= 100)
|
|
|
|
{
|
|
|
|
request.PercentPlayed = null;
|
|
|
|
request.AddPlayedIndicator = true;
|
|
|
|
}
|
|
|
|
}
|
2019-03-07 14:54:30 +00:00
|
|
|
|
2015-04-16 14:59:39 +00:00
|
|
|
if (request.PercentPlayed.HasValue)
|
|
|
|
{
|
|
|
|
request.UnplayedCount = null;
|
|
|
|
}
|
2019-03-07 14:54:30 +00:00
|
|
|
|
|
|
|
if (request.UnplayedCount.HasValue
|
|
|
|
&& request.UnplayedCount.Value <= 0)
|
2015-04-16 14:59:39 +00:00
|
|
|
{
|
2019-03-07 14:54:30 +00:00
|
|
|
request.UnplayedCount = null;
|
2015-04-16 14:59:39 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (item == null)
|
|
|
|
{
|
|
|
|
item = _libraryManager.GetItemById(itemId);
|
|
|
|
|
|
|
|
if (item == null)
|
|
|
|
{
|
2019-02-28 22:22:57 +00:00
|
|
|
throw new ResourceNotFoundException(string.Format("Item {0} not found.", itemId.ToString("N", CultureInfo.InvariantCulture)));
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 20:30:41 +00:00
|
|
|
var imageInfo = GetImageInfo(request, item);
|
|
|
|
if (imageInfo == null)
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
2020-05-13 02:10:35 +00:00
|
|
|
throw new ResourceNotFoundException(string.Format("{0} does not have an image of type {1}", item.Name, request.Type));
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 02:10:35 +00:00
|
|
|
bool cropWhitespace;
|
2015-10-26 05:29:32 +00:00
|
|
|
if (request.CropWhitespace.HasValue)
|
|
|
|
{
|
2020-05-13 02:10:35 +00:00
|
|
|
cropWhitespace = request.CropWhitespace.Value;
|
2015-10-26 05:29:32 +00:00
|
|
|
}
|
2019-03-07 14:54:30 +00:00
|
|
|
else
|
|
|
|
{
|
2020-05-13 02:10:35 +00:00
|
|
|
cropWhitespace = request.Type == ImageType.Logo || request.Type == ImageType.Art;
|
2019-03-07 14:54:30 +00:00
|
|
|
}
|
2015-10-26 05:29:32 +00:00
|
|
|
|
2017-09-07 18:17:18 +00:00
|
|
|
var outputFormats = GetOutputFormats(request);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
TimeSpan? cacheDuration = null;
|
|
|
|
|
2016-08-13 19:52:58 +00:00
|
|
|
if (!string.IsNullOrEmpty(request.Tag))
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
|
|
|
cacheDuration = TimeSpan.FromDays(365);
|
|
|
|
}
|
|
|
|
|
2014-02-28 05:22:36 +00:00
|
|
|
var responseHeaders = new Dictionary<string, string>
|
|
|
|
{
|
|
|
|
{"transferMode.dlna.org", "Interactive"},
|
|
|
|
{"realTimeInfo.dlna.org", "DLNA.ORG_TLAG=*"}
|
|
|
|
};
|
2014-02-13 01:52:24 +00:00
|
|
|
|
2020-01-21 19:26:30 +00:00
|
|
|
return GetImageResult(
|
|
|
|
item,
|
2018-09-12 17:26:21 +00:00
|
|
|
itemId,
|
2014-08-30 14:26:29 +00:00
|
|
|
request,
|
|
|
|
imageInfo,
|
2020-05-13 02:10:35 +00:00
|
|
|
cropWhitespace,
|
2015-11-09 18:18:37 +00:00
|
|
|
outputFormats,
|
2014-07-17 22:21:35 +00:00
|
|
|
cacheDuration,
|
2014-08-08 04:36:51 +00:00
|
|
|
responseHeaders,
|
2016-06-19 06:18:29 +00:00
|
|
|
isHeadRequest);
|
2014-07-17 22:21:35 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 02:10:35 +00:00
|
|
|
public Task<object> GetImage(ImageRequest request, User user, bool isHeadRequest)
|
|
|
|
{
|
|
|
|
var imageInfo = GetImageInfo(request, user);
|
|
|
|
|
|
|
|
TimeSpan? cacheDuration = null;
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(request.Tag))
|
|
|
|
{
|
|
|
|
cacheDuration = TimeSpan.FromDays(365);
|
|
|
|
}
|
|
|
|
|
|
|
|
var responseHeaders = new Dictionary<string, string>
|
|
|
|
{
|
|
|
|
{"transferMode.dlna.org", "Interactive"},
|
|
|
|
{"realTimeInfo.dlna.org", "DLNA.ORG_TLAG=*"}
|
|
|
|
};
|
|
|
|
|
|
|
|
var outputFormats = GetOutputFormats(request);
|
|
|
|
|
2020-05-27 02:43:03 +00:00
|
|
|
return GetImageResult(user.Id,
|
2020-05-13 02:10:35 +00:00
|
|
|
request,
|
|
|
|
imageInfo,
|
|
|
|
outputFormats,
|
|
|
|
cacheDuration,
|
|
|
|
responseHeaders,
|
|
|
|
isHeadRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<object> GetImageResult(
|
|
|
|
Guid itemId,
|
|
|
|
ImageRequest request,
|
|
|
|
ItemImageInfo info,
|
|
|
|
IReadOnlyCollection<ImageFormat> supportedFormats,
|
|
|
|
TimeSpan? cacheDuration,
|
|
|
|
IDictionary<string, string> headers,
|
|
|
|
bool isHeadRequest)
|
|
|
|
{
|
2020-05-20 16:09:52 +00:00
|
|
|
info.Type = ImageType.Profile;
|
2020-05-13 02:10:35 +00:00
|
|
|
var options = new ImageProcessingOptions
|
|
|
|
{
|
|
|
|
CropWhiteSpace = true,
|
|
|
|
Height = request.Height,
|
|
|
|
ImageIndex = request.Index ?? 0,
|
|
|
|
Image = info,
|
|
|
|
Item = null, // Hack alert
|
|
|
|
ItemId = itemId,
|
|
|
|
MaxHeight = request.MaxHeight,
|
|
|
|
MaxWidth = request.MaxWidth,
|
|
|
|
Quality = request.Quality ?? 100,
|
|
|
|
Width = request.Width,
|
|
|
|
AddPlayedIndicator = request.AddPlayedIndicator,
|
|
|
|
PercentPlayed = 0,
|
|
|
|
UnplayedCount = request.UnplayedCount,
|
|
|
|
Blur = request.Blur,
|
|
|
|
BackgroundColor = request.BackgroundColor,
|
|
|
|
ForegroundLayer = request.ForegroundLayer,
|
|
|
|
SupportedOutputFormats = supportedFormats
|
|
|
|
};
|
|
|
|
|
|
|
|
var imageResult = await _imageProcessor.ProcessImage(options).ConfigureAwait(false);
|
|
|
|
|
|
|
|
headers[HeaderNames.Vary] = HeaderNames.Accept;
|
|
|
|
|
|
|
|
return await ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions
|
|
|
|
{
|
|
|
|
CacheDuration = cacheDuration,
|
|
|
|
ResponseHeaders = headers,
|
|
|
|
ContentType = imageResult.Item2,
|
|
|
|
DateLastModified = imageResult.Item3,
|
|
|
|
IsHeadRequest = isHeadRequest,
|
|
|
|
Path = imageResult.Item1,
|
|
|
|
|
|
|
|
FileShare = FileShare.Read
|
|
|
|
|
|
|
|
}).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
|
2020-01-21 19:26:30 +00:00
|
|
|
private async Task<object> GetImageResult(
|
|
|
|
BaseItem item,
|
2018-09-12 17:26:21 +00:00
|
|
|
Guid itemId,
|
2014-07-17 22:21:35 +00:00
|
|
|
ImageRequest request,
|
|
|
|
ItemImageInfo image,
|
2015-10-26 05:29:32 +00:00
|
|
|
bool cropwhitespace,
|
2019-08-11 14:52:37 +00:00
|
|
|
IReadOnlyCollection<ImageFormat> supportedFormats,
|
2014-07-17 22:21:35 +00:00
|
|
|
TimeSpan? cacheDuration,
|
2014-08-30 14:26:29 +00:00
|
|
|
IDictionary<string, string> headers,
|
2014-08-08 04:36:51 +00:00
|
|
|
bool isHeadRequest)
|
2014-07-17 22:21:35 +00:00
|
|
|
{
|
2020-04-04 20:01:10 +00:00
|
|
|
if (!image.IsLocalFile)
|
|
|
|
{
|
|
|
|
item ??= _libraryManager.GetItemById(itemId);
|
|
|
|
image = await _libraryManager.ConvertImageToLocal(item, image, request.Index ?? 0).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
|
2014-07-17 22:21:35 +00:00
|
|
|
var options = new ImageProcessingOptions
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
2014-07-17 22:21:35 +00:00
|
|
|
CropWhiteSpace = cropwhitespace,
|
|
|
|
Height = request.Height,
|
|
|
|
ImageIndex = request.Index ?? 0,
|
|
|
|
Image = image,
|
|
|
|
Item = item,
|
2018-09-12 17:26:21 +00:00
|
|
|
ItemId = itemId,
|
2014-07-17 22:21:35 +00:00
|
|
|
MaxHeight = request.MaxHeight,
|
|
|
|
MaxWidth = request.MaxWidth,
|
2015-10-27 14:02:30 +00:00
|
|
|
Quality = request.Quality ?? 100,
|
2014-07-17 22:21:35 +00:00
|
|
|
Width = request.Width,
|
|
|
|
AddPlayedIndicator = request.AddPlayedIndicator,
|
2014-09-01 20:10:54 +00:00
|
|
|
PercentPlayed = request.PercentPlayed ?? 0,
|
2014-07-17 22:21:35 +00:00
|
|
|
UnplayedCount = request.UnplayedCount,
|
2016-12-03 07:58:48 +00:00
|
|
|
Blur = request.Blur,
|
2014-11-09 03:18:14 +00:00
|
|
|
BackgroundColor = request.BackgroundColor,
|
2016-02-23 19:48:58 +00:00
|
|
|
ForegroundLayer = request.ForegroundLayer,
|
2015-11-09 18:18:37 +00:00
|
|
|
SupportedOutputFormats = supportedFormats
|
2014-07-17 22:21:35 +00:00
|
|
|
};
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2015-11-09 18:18:37 +00:00
|
|
|
var imageResult = await _imageProcessor.ProcessImage(options).ConfigureAwait(false);
|
2014-07-17 22:21:35 +00:00
|
|
|
|
2019-03-05 18:20:28 +00:00
|
|
|
headers[HeaderNames.Vary] = HeaderNames.Accept;
|
2015-05-22 19:16:14 +00:00
|
|
|
|
2016-06-19 06:18:29 +00:00
|
|
|
return await ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions
|
2014-08-30 14:26:29 +00:00
|
|
|
{
|
|
|
|
CacheDuration = cacheDuration,
|
|
|
|
ResponseHeaders = headers,
|
2015-11-09 18:18:37 +00:00
|
|
|
ContentType = imageResult.Item2,
|
2016-07-17 16:59:40 +00:00
|
|
|
DateLastModified = imageResult.Item3,
|
2014-08-30 14:26:29 +00:00
|
|
|
IsHeadRequest = isHeadRequest,
|
2015-12-13 04:53:56 +00:00
|
|
|
Path = imageResult.Item1,
|
|
|
|
|
2020-01-08 16:52:50 +00:00
|
|
|
FileShare = FileShare.Read
|
2016-06-19 06:18:29 +00:00
|
|
|
}).ConfigureAwait(false);
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
|
2017-09-07 18:17:18 +00:00
|
|
|
private ImageFormat[] GetOutputFormats(ImageRequest request)
|
2014-11-09 03:18:14 +00:00
|
|
|
{
|
2019-03-07 14:54:30 +00:00
|
|
|
if (!string.IsNullOrWhiteSpace(request.Format)
|
|
|
|
&& Enum.TryParse(request.Format, true, out ImageFormat format))
|
2014-11-09 03:18:14 +00:00
|
|
|
{
|
2020-04-05 17:00:35 +00:00
|
|
|
return new[] { format };
|
2014-11-09 03:18:14 +00:00
|
|
|
}
|
|
|
|
|
2017-09-07 18:17:18 +00:00
|
|
|
return GetClientSupportedFormats();
|
2014-11-09 03:18:14 +00:00
|
|
|
}
|
|
|
|
|
2014-11-29 19:51:30 +00:00
|
|
|
private ImageFormat[] GetClientSupportedFormats()
|
2014-11-09 03:18:14 +00:00
|
|
|
{
|
2019-03-07 14:54:30 +00:00
|
|
|
var supportedFormats = Request.AcceptTypes ?? Array.Empty<string>();
|
|
|
|
if (supportedFormats.Length > 0)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < supportedFormats.Length; i++)
|
|
|
|
{
|
|
|
|
int index = supportedFormats[i].IndexOf(';');
|
|
|
|
if (index != -1)
|
|
|
|
{
|
|
|
|
supportedFormats[i] = supportedFormats[i].Substring(0, index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
var acceptParam = Request.QueryString["accept"];
|
2015-06-03 15:26:39 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
var supportsWebP = SupportsFormat(supportedFormats, acceptParam, "webp", false);
|
2015-11-11 19:06:56 +00:00
|
|
|
|
|
|
|
if (!supportsWebP)
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var userAgent = Request.UserAgent ?? string.Empty;
|
2015-11-11 19:06:56 +00:00
|
|
|
if (userAgent.IndexOf("crosswalk", StringComparison.OrdinalIgnoreCase) != -1 &&
|
|
|
|
userAgent.IndexOf("android", StringComparison.OrdinalIgnoreCase) != -1)
|
|
|
|
{
|
|
|
|
supportsWebP = true;
|
|
|
|
}
|
2015-06-03 15:26:39 +00:00
|
|
|
}
|
2014-11-26 04:12:29 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
var formats = new List<ImageFormat>(4);
|
|
|
|
|
2015-06-03 15:26:39 +00:00
|
|
|
if (supportsWebP)
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
formats.Add(ImageFormat.Webp);
|
|
|
|
}
|
|
|
|
|
|
|
|
formats.Add(ImageFormat.Jpg);
|
|
|
|
formats.Add(ImageFormat.Png);
|
|
|
|
|
|
|
|
if (SupportsFormat(supportedFormats, acceptParam, "gif", true))
|
|
|
|
{
|
|
|
|
formats.Add(ImageFormat.Gif);
|
|
|
|
}
|
|
|
|
|
|
|
|
return formats.ToArray();
|
|
|
|
}
|
|
|
|
|
2019-03-07 14:54:30 +00:00
|
|
|
private bool SupportsFormat(IEnumerable<string> requestAcceptTypes, string acceptParam, string format, bool acceptAll)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
var mimeType = "image/" + format;
|
|
|
|
|
|
|
|
if (requestAcceptTypes.Contains(mimeType))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (acceptAll && requestAcceptTypes.Contains("*/*"))
|
|
|
|
{
|
|
|
|
return true;
|
2014-11-09 03:18:14 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
return string.Equals(Request.QueryString["accept"], format, StringComparison.OrdinalIgnoreCase);
|
2014-11-09 03:18:14 +00:00
|
|
|
}
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the image path.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
/// <returns>System.String.</returns>
|
2020-05-27 03:08:27 +00:00
|
|
|
private static ItemImageInfo GetImageInfo(ImageRequest request, BaseItem item)
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
|
|
|
var index = request.Index ?? 0;
|
|
|
|
|
2014-02-07 20:30:41 +00:00
|
|
|
return item.GetImageInfo(request.Type, index);
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
2013-02-25 04:39:53 +00:00
|
|
|
|
2020-05-27 03:08:27 +00:00
|
|
|
private static ItemImageInfo GetImageInfo(ImageRequest request, User user)
|
2020-05-13 02:10:35 +00:00
|
|
|
{
|
|
|
|
var info = new ItemImageInfo
|
|
|
|
{
|
|
|
|
Path = user.ProfileImage.Path,
|
|
|
|
Type = ImageType.Primary,
|
|
|
|
DateModified = user.ProfileImage.LastModified,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (request.Width.HasValue)
|
|
|
|
{
|
|
|
|
info.Width = request.Width.Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request.Height.HasValue)
|
|
|
|
{
|
|
|
|
info.Height = request.Height.Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2013-02-25 04:39:53 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Posts the image.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="entity">The entity.</param>
|
|
|
|
/// <param name="inputStream">The input stream.</param>
|
|
|
|
/// <param name="imageType">Type of the image.</param>
|
|
|
|
/// <param name="mimeType">Type of the MIME.</param>
|
|
|
|
/// <returns>Task.</returns>
|
2013-12-19 21:51:32 +00:00
|
|
|
public async Task PostImage(BaseItem entity, Stream inputStream, ImageType imageType, string mimeType)
|
2013-02-25 04:39:53 +00:00
|
|
|
{
|
2020-05-27 03:08:27 +00:00
|
|
|
var memoryStream = await GetMemoryStream(inputStream);
|
2013-02-25 04:39:53 +00:00
|
|
|
|
2020-04-05 16:26:11 +00:00
|
|
|
// Handle image/png; charset=utf-8
|
|
|
|
mimeType = mimeType.Split(';').FirstOrDefault();
|
2013-06-29 15:47:36 +00:00
|
|
|
|
2020-04-05 16:26:11 +00:00
|
|
|
await _providerManager.SaveImage(entity, memoryStream, mimeType, imageType, null, CancellationToken.None).ConfigureAwait(false);
|
2013-05-18 17:07:20 +00:00
|
|
|
|
2020-04-05 16:26:11 +00:00
|
|
|
entity.UpdateToRepository(ItemUpdateType.ImageUpdate, CancellationToken.None);
|
2013-02-25 04:39:53 +00:00
|
|
|
}
|
2020-05-13 02:10:35 +00:00
|
|
|
|
2020-05-27 03:08:27 +00:00
|
|
|
private static async Task<MemoryStream> GetMemoryStream(Stream inputStream)
|
2020-05-13 02:10:35 +00:00
|
|
|
{
|
|
|
|
using var reader = new StreamReader(inputStream);
|
|
|
|
var text = await reader.ReadToEndAsync().ConfigureAwait(false);
|
|
|
|
|
|
|
|
var bytes = Convert.FromBase64String(text);
|
2020-05-27 03:08:27 +00:00
|
|
|
return new MemoryStream(bytes)
|
2020-05-13 02:10:35 +00:00
|
|
|
{
|
|
|
|
Position = 0
|
|
|
|
};
|
2020-05-27 03:08:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private async Task PostImage(User user, Stream inputStream, string mimeType)
|
|
|
|
{
|
|
|
|
var memoryStream = await GetMemoryStream(inputStream);
|
2020-05-13 02:10:35 +00:00
|
|
|
|
|
|
|
// Handle image/png; charset=utf-8
|
|
|
|
mimeType = mimeType.Split(';').FirstOrDefault();
|
2020-05-13 02:25:45 +00:00
|
|
|
var userDataPath = Path.Combine(ServerConfigurationManager.ApplicationPaths.UserConfigurationDirectoryPath, user.Username);
|
2020-05-20 16:09:52 +00:00
|
|
|
user.ProfileImage = new Jellyfin.Data.Entities.ImageInfo(Path.Combine(userDataPath, "profile" + MimeTypes.ToExtension(mimeType)));
|
2020-05-13 02:10:35 +00:00
|
|
|
|
|
|
|
await _providerManager
|
2020-05-20 16:09:52 +00:00
|
|
|
.SaveImage(user, memoryStream, mimeType, user.ProfileImage.Path)
|
2020-05-13 02:10:35 +00:00
|
|
|
.ConfigureAwait(false);
|
|
|
|
await _userManager.UpdateUserAsync(user);
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
}
|