Commit 2be44adf authored by Alessio Parma's avatar Alessio Parma

added new parameters into client

parent 4c5e68f3
......@@ -25,7 +25,7 @@
using System.Linq;
using PommaLabs.MimeTypes;
namespace PommaLabs.Thumbnailer.Client
namespace PommaLabs.Thumbnailer.Client.Core
{
/// <summary>
/// Helper which extends <see cref="MimeTypeMap"/>.
......
......@@ -26,7 +26,7 @@
using Microsoft.Extensions.Logging;
using PommaLabs.MimeTypes;
namespace PommaLabs.Thumbnailer.Client
namespace PommaLabs.Thumbnailer.Client.Core
{
/// <summary>
/// Default parameter values and validations.
......
......@@ -23,6 +23,7 @@
using Microsoft.Extensions.DependencyInjection.Extensions;
using PommaLabs.Thumbnailer.Client;
using PommaLabs.Thumbnailer.Client.Core;
using PommaLabs.Thumbnailer.Client.Services.Clients;
namespace Microsoft.Extensions.DependencyInjection
......@@ -40,8 +41,7 @@ public static class ThumbnailerServiceCollectionExtensions
public static IServiceCollection AddThumbnailerClient(this IServiceCollection services)
{
services.TryAddSingleton<Validator>();
services.AddSingleton<IThumbnailerClient, ConcreteThumbnailerClient>();
services.TryAddSingleton<IThumbnailerClient, ConcreteThumbnailerClient>();
return services;
}
}
......
......@@ -24,8 +24,9 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using PommaLabs.Thumbnailer.Client.Core;
namespace PommaLabs.Thumbnailer.Client.Services.Clients
namespace PommaLabs.Thumbnailer.Client
{
/// <summary>
/// Thumbnailer client.
......@@ -36,17 +37,29 @@ public interface IThumbnailerClient
/// Produces a thumbnail of given source file.
/// </summary>
/// <param name="fileUri">Source file URI.</param>
/// <param name="sidePx">
/// Makes the thumbnail as a square of <paramref name="sidePx"/> pixels, preserving the
/// aspect ratio and filling with a transparent background.
/// <param name="widthPx">
/// Max thumbnail width. Thumbnail will be generated preserving the source file aspect
/// ratio and, if <paramref name="fill"/> is true, a trasparent background will be added
/// so that thumbnail width and height are exactly as requested.
/// </param>
/// <param name="heightPx">
/// Max thumbnail height. Thumbnail will be generated preserving the source file aspect
/// ratio and, if <paramref name="fill"/> is true, a trasparent background will be added
/// so that thumbnail width and height are exactly as requested.
/// </param>
/// <param name="shavePx">Shaves <paramref name="shavePx"/> from source file edges.</param>
/// <param name="fill">
/// If true, a trasparent background will be added so that thumbnail width and height are
/// exactly as requested, preserving source file aspect ratio.
/// </param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>A thumbnail of given source file.</returns>
Task<byte[]> GetThumbnailAsync(
Uri fileUri,
ushort sidePx = Validator.ThumbnailSidePx,
ushort widthPx = Validator.ThumbnailSidePx,
ushort heightPx = Validator.ThumbnailSidePx,
ushort shavePx = Validator.ThumbnailShavePx,
bool fill = default,
CancellationToken cancellationToken = default);
/// <summary>
......@@ -55,17 +68,30 @@ public interface IThumbnailerClient
/// <param name="contents">File bytes.</param>
/// <param name="contentType">File content type.</param>
/// <param name="fileName">File name.</param>
/// <param name="sidePx">
/// Makes the thumbnail as a square of <paramref name="sidePx"/> pixels, preserving the
/// aspect ratio and filling with a transparent background.
/// <param name="widthPx">
/// Max thumbnail width. Thumbnail will be generated preserving the source file aspect
/// ratio and, if <paramref name="fill"/> is true, a trasparent background will be added
/// so that thumbnail width and height are exactly as requested.
/// </param>
/// <param name="heightPx">
/// Max thumbnail height. Thumbnail will be generated preserving the source file aspect
/// ratio and, if <paramref name="fill"/> is true, a trasparent background will be added
/// so that thumbnail width and height are exactly as requested.
/// </param>
/// <param name="shavePx">Shaves <paramref name="shavePx"/> from source file edges.</param>
/// <param name="fill">
/// If true, a trasparent background will be added so that thumbnail width and height are
/// exactly as requested, preserving source file aspect ratio.
/// </param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>A thumbnail of given source file.</returns>
Task<byte[]> GetThumbnailAsync(
byte[] contents, string contentType, string? fileName = default,
ushort sidePx = Validator.ThumbnailSidePx,
byte[] contents, string contentType,
string? fileName = default,
ushort widthPx = Validator.ThumbnailSidePx,
ushort heightPx = Validator.ThumbnailSidePx,
ushort shavePx = Validator.ThumbnailShavePx,
bool fill = default,
CancellationToken cancellationToken = default);
/// <summary>
......@@ -103,7 +129,8 @@ public interface IThumbnailerClient
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>An optimized version of given source image file.</returns>
Task<byte[]> OptimizeImageAsync(
byte[] contents, string contentType, string? fileName = default,
byte[] contents, string contentType,
string? fileName = default,
CancellationToken cancellationToken = default);
}
}
......@@ -30,9 +30,8 @@
using Flurl.Http;
using Microsoft.Extensions.Options;
using PommaLabs.MimeTypes;
using PommaLabs.Thumbnailer.Client.Models.Configurations;
using PommaLabs.Thumbnailer.Client.Core;
using PommaLabs.Thumbnailer.Client.Models.Enumerations;
using PommaLabs.Thumbnailer.Client.Models.Exceptions;
namespace PommaLabs.Thumbnailer.Client.Services.Clients
{
......@@ -62,8 +61,10 @@ public sealed class ConcreteThumbnailerClient : IThumbnailerClient
/// <inheritdoc/>
public async Task<byte[]> GetThumbnailAsync(
Uri fileUri,
ushort sidePx = Validator.ThumbnailSidePx,
ushort widthPx = Validator.ThumbnailSidePx,
ushort heightPx = Validator.ThumbnailSidePx,
ushort shavePx = Validator.ThumbnailShavePx,
bool fill = default,
CancellationToken cancellationToken = default)
{
_validator.ValidateFileUri(fileUri);
......@@ -72,8 +73,10 @@ public sealed class ConcreteThumbnailerClient : IThumbnailerClient
.Combine(_clientConfiguration.Value.BaseUri.AbsoluteUri, "api/v1/thumbnail")
.WithHeader(ApiKeyHeaderName, _clientConfiguration.Value.ApiKey)
.SetQueryParam(nameof(fileUri), fileUri.AbsoluteUri)
.SetQueryParam(nameof(sidePx), sidePx.ToString(CultureInfo.InvariantCulture))
.SetQueryParam(nameof(widthPx), widthPx.ToString(CultureInfo.InvariantCulture))
.SetQueryParam(nameof(heightPx), heightPx.ToString(CultureInfo.InvariantCulture))
.SetQueryParam(nameof(shavePx), shavePx.ToString(CultureInfo.InvariantCulture))
.SetQueryParam(nameof(fill), fill.ToString(CultureInfo.InvariantCulture))
.GetBytesAsync(cancellationToken)
.ConfigureAwait(false))
.ConfigureAwait(false);
......@@ -81,9 +84,12 @@ public sealed class ConcreteThumbnailerClient : IThumbnailerClient
/// <inheritdoc/>
public async Task<byte[]> GetThumbnailAsync(
byte[] contents, string contentType, string? fileName = default,
ushort sidePx = Validator.ThumbnailSidePx,
byte[] contents, string contentType,
string? fileName = default,
ushort widthPx = Validator.ThumbnailSidePx,
ushort heightPx = Validator.ThumbnailSidePx,
ushort shavePx = Validator.ThumbnailShavePx,
bool fill = default,
CancellationToken cancellationToken = default)
{
_validator.ValidateFileBytes(contents);
......@@ -94,8 +100,10 @@ public sealed class ConcreteThumbnailerClient : IThumbnailerClient
return await EvaluateResponseAsync(OperationType.ThumbnailGeneration, async () => await Url
.Combine(_clientConfiguration.Value.BaseUri.AbsoluteUri, "api/v1/thumbnail")
.WithHeader(ApiKeyHeaderName, _clientConfiguration.Value.ApiKey)
.SetQueryParam(nameof(sidePx), sidePx.ToString(CultureInfo.InvariantCulture))
.SetQueryParam(nameof(widthPx), widthPx.ToString(CultureInfo.InvariantCulture))
.SetQueryParam(nameof(heightPx), heightPx.ToString(CultureInfo.InvariantCulture))
.SetQueryParam(nameof(shavePx), shavePx.ToString(CultureInfo.InvariantCulture))
.SetQueryParam(nameof(fill), fill.ToString(CultureInfo.InvariantCulture))
.PostMultipartAsync(mp => mp.AddFile("file", new MemoryStream(contents), fileName, contentType), cancellationToken)
.ReceiveBytes()
.ConfigureAwait(false))
......@@ -132,7 +140,8 @@ public Task<bool> IsThumbnailGenerationSupportedAsync(string contentType)
/// <inheritdoc/>
public async Task<byte[]> OptimizeImageAsync(
byte[] contents, string contentType, string? fileName = default,
byte[] contents, string contentType,
string? fileName = default,
CancellationToken cancellationToken = default)
{
_validator.ValidateFileBytes(contents);
......
......@@ -23,7 +23,7 @@
using System;
namespace PommaLabs.Thumbnailer.Client.Models.Configurations
namespace PommaLabs.Thumbnailer.Client
{
/// <summary>
/// Thumbnailer client configuration.
......
......@@ -26,7 +26,7 @@
using System.Runtime.Serialization;
using PommaLabs.Thumbnailer.Client.Models.Enumerations;
namespace PommaLabs.Thumbnailer.Client.Models.Exceptions
namespace PommaLabs.Thumbnailer.Client
{
/// <summary>
/// Represents an error produced by Thumbnailer service.
......
......@@ -24,7 +24,7 @@
using System;
using PommaLabs.KVLite;
using PommaLabs.MimeTypes;
using PommaLabs.Thumbnailer.Client;
using PommaLabs.Thumbnailer.Client.Core;
namespace PommaLabs.Thumbnailer.Models.DTO
{
......
......@@ -24,7 +24,7 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using PommaLabs.Thumbnailer.Client;
using PommaLabs.Thumbnailer.Client.Core;
using PommaLabs.Thumbnailer.Models.DTO;
namespace PommaLabs.Thumbnailer.Services.Managers.Download
......
......@@ -23,7 +23,7 @@
using System.Threading;
using System.Threading.Tasks;
using PommaLabs.Thumbnailer.Client;
using PommaLabs.Thumbnailer.Client.Core;
using PommaLabs.Thumbnailer.Models.DTO;
namespace PommaLabs.Thumbnailer.Services.Managers.Optimization
......
......@@ -23,7 +23,7 @@
using System.Threading;
using System.Threading.Tasks;
using PommaLabs.Thumbnailer.Client;
using PommaLabs.Thumbnailer.Client.Core;
using PommaLabs.Thumbnailer.Models.DTO;
namespace PommaLabs.Thumbnailer.Services.Managers.Thumbnail
......
......@@ -69,7 +69,8 @@ public sealed class OptimizeController : ControllerBase
/// <returns>An optimized version of given source image file.</returns>
[HttpGet, Route("")]
public async Task<PhysicalFileResult> OptimizeImageAsync(
[FromQuery] Uri fileUri, CancellationToken cancellationToken = default)
[FromQuery] Uri fileUri,
CancellationToken cancellationToken = default)
{
var localFile = await _downloadManager.DownloadFileAsync(fileUri, cancellationToken).ConfigureAwait(false);
var optimized = await _optimizationManager.OptimizeImageAsync(localFile, cancellationToken).ConfigureAwait(false);
......@@ -86,7 +87,8 @@ public sealed class OptimizeController : ControllerBase
[HttpPost, Route(""), Consumes(Constants.MultipartFormData)]
[DisableRequestSizeLimit /* Upload limit is globally set in Startup code. */]
public async Task<PhysicalFileResult> OptimizeImageAsync(
[FromServices] ITempFileStore tempFileStore, IFormFile file,
[FromServices] ITempFileStore tempFileStore,
[FromForm] IFormFile file,
CancellationToken cancellationToken = default)
{
var localFile = await tempFileStore.HandleFileUploadAsync(file, cancellationToken).ConfigureAwait(false);
......@@ -105,7 +107,8 @@ public sealed class OptimizeController : ControllerBase
[HttpPost, Route("base64"), Consumes(MimeTypeMap.APPLICATION.JSON, MimeTypeMap.APPLICATION.XML)]
[DisableRequestSizeLimit /* Upload limit is globally set in Startup code. */]
public async Task<PhysicalFileResult> OptimizeImageAsync(
[FromServices] ITempFileStore tempFileStore, UploadedFileDTO file,
[FromServices] ITempFileStore tempFileStore,
[FromBody] UploadedFileDTO file,
CancellationToken cancellationToken = default)
{
var localFile = await tempFileStore.HandleFileUploadAsync(file, cancellationToken).ConfigureAwait(false);
......
......@@ -28,7 +28,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PommaLabs.MimeTypes;
using PommaLabs.Thumbnailer.Client;
using PommaLabs.Thumbnailer.Client.Core;
using PommaLabs.Thumbnailer.Models.DTO;
using PommaLabs.Thumbnailer.Services.Managers.Download;
using PommaLabs.Thumbnailer.Services.Managers.Optimization;
......@@ -102,7 +102,8 @@ public sealed class ThumbnailController : ControllerBase
[FromQuery] ushort heightPx = Validator.ThumbnailSidePx,
[FromQuery] ushort sidePx = Validator.ThumbnailSidePx,
[FromQuery] ushort shavePx = Validator.ThumbnailShavePx,
bool fill = true, CancellationToken cancellationToken = default)
[FromQuery] bool fill = true,
CancellationToken cancellationToken = default)
{
FixWidthAndHeight(ref widthPx, ref heightPx, sidePx);
var localFile = await _downloadManager.DownloadFileAsync(fileUri, cancellationToken).ConfigureAwait(false);
......@@ -143,12 +144,14 @@ public sealed class ThumbnailController : ControllerBase
[HttpPost, Route(""), Consumes(Constants.MultipartFormData)]
[DisableRequestSizeLimit /* Upload limit is globally set in Startup code. */]
public async Task<PhysicalFileResult> GenerateThumbnailAsync(
[FromServices] ITempFileStore tempFileStore, IFormFile file,
[FromServices] ITempFileStore tempFileStore,
[FromForm] IFormFile file,
[FromQuery] ushort widthPx = Validator.ThumbnailSidePx,
[FromQuery] ushort heightPx = Validator.ThumbnailSidePx,
[FromQuery] ushort sidePx = Validator.ThumbnailSidePx,
[FromQuery] ushort shavePx = Validator.ThumbnailShavePx,
bool fill = true, CancellationToken cancellationToken = default)
[FromQuery] bool fill = true,
CancellationToken cancellationToken = default)
{
FixWidthAndHeight(ref widthPx, ref heightPx, sidePx);
var localFile = await tempFileStore.HandleFileUploadAsync(file, cancellationToken).ConfigureAwait(false);
......@@ -190,12 +193,14 @@ public sealed class ThumbnailController : ControllerBase
[HttpPost, Route("base64"), Consumes(MimeTypeMap.APPLICATION.JSON, MimeTypeMap.APPLICATION.XML)]
[DisableRequestSizeLimit /* Upload limit is globally set in Startup code. */]
public async Task<PhysicalFileResult> GenerateThumbnailAsync(
[FromServices] ITempFileStore tempFileStore, UploadedFileDTO file,
[FromServices] ITempFileStore tempFileStore,
[FromBody] UploadedFileDTO file,
[FromQuery] ushort widthPx = Validator.ThumbnailSidePx,
[FromQuery] ushort heightPx = Validator.ThumbnailSidePx,
[FromQuery] ushort sidePx = Validator.ThumbnailSidePx,
[FromQuery] ushort shavePx = Validator.ThumbnailShavePx,
bool fill = true, CancellationToken cancellationToken = default)
[FromQuery] bool fill = true,
CancellationToken cancellationToken = default)
{
FixWidthAndHeight(ref widthPx, ref heightPx, sidePx);
var localFile = await tempFileStore.HandleFileUploadAsync(file, cancellationToken).ConfigureAwait(false);
......
......@@ -34,8 +34,7 @@
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using PommaLabs.Thumbnailer.Client;
using PommaLabs.Thumbnailer.Client.Models.Configurations;
using PommaLabs.Thumbnailer.Client.Services.Clients;
using PommaLabs.Thumbnailer.Client.Core;
namespace PommaLabs.Thumbnailer.Tester
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment