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