Commit 3f557407 authored by Alessio Parma's avatar Alessio Parma

removed optimization mode

parent ac335fd0
......@@ -25,7 +25,6 @@
using System.Threading;
using System.Threading.Tasks;
using PommaLabs.Thumbnailer.Client.Core;
using PommaLabs.Thumbnailer.Client.Models.Enumerations;
namespace PommaLabs.Thumbnailer.Client
{
......@@ -116,13 +115,10 @@ public interface IThumbnailerClient
/// Optimizes given source image file.
/// </summary>
/// <param name="fileUri">Source file URI.</param>
/// <param name="mode">Image optimization mode. If not specified, it defaults to <see cref="ImageOptimizationMode.Lossy"/>.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>An optimized version of given source image file.</returns>
Task<byte[]> OptimizeImageAsync(
Uri fileUri,
ImageOptimizationMode mode = default,
CancellationToken cancellationToken = default);
Uri fileUri, CancellationToken cancellationToken = default);
/// <summary>
/// Optimizes given source image file.
......@@ -130,13 +126,10 @@ public interface IThumbnailerClient
/// <param name="contents">File bytes.</param>
/// <param name="contentType">File content type.</param>
/// <param name="fileName">File name.</param>
/// <param name="mode">Image optimization mode. If not specified, it defaults to <see cref="ImageOptimizationMode.Lossy"/>.</param>
/// <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,
ImageOptimizationMode mode = default,
CancellationToken cancellationToken = default);
string? fileName = default, CancellationToken cancellationToken = default);
}
}
// File name: ImageOptimizationMode.cs
//
// Author(s): Alessio Parma <[email protected]>
//
// The MIT License (MIT)
//
// Copyright (c) 2019-2020 Alessio Parma <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
// associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
namespace PommaLabs.Thumbnailer.Client.Models.Enumerations
{
/// <summary>
/// Image optimization mode.
/// </summary>
public enum ImageOptimizationMode
{
/// <summary>
/// Lossy: image quality might decrease, but size reduction is greater than <see cref="Lossless"/>.
/// </summary>
Lossy = 0,
/// <summary>
/// Lossless: image quality is left unchanged, but size reduction is less than <see cref="Lossy"/>.
/// </summary>
Lossless = 1,
}
}
......@@ -25,7 +25,6 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Flurl;
......@@ -45,7 +44,6 @@ namespace PommaLabs.Thumbnailer.Client.Services.Clients
public sealed class ConcreteThumbnailerClient : IThumbnailerClient
{
private const string ApiKeyHeaderName = "X-Api-Key";
private const string OptimizationModeParamName = "mode";
private const string RapidApiKeyHeaderName = "X-RapidAPI-Key";
private static readonly HashSet<int> s_retriableStatusCodes = new HashSet<int> { 424, 429 };
......@@ -144,9 +142,7 @@ public Task<bool> IsThumbnailGenerationSupportedAsync(string contentType)
/// <inheritdoc/>
public async Task<byte[]> OptimizeImageAsync(
Uri fileUri,
ImageOptimizationMode mode,
CancellationToken cancellationToken)
Uri fileUri, CancellationToken cancellationToken)
{
_validator.ValidateFileUri(fileUri);
......@@ -157,7 +153,6 @@ public Task<bool> IsThumbnailGenerationSupportedAsync(string contentType)
.WithTimeout(_clientConfiguration.Value.Timeout)
.WithHeader(isRapidApi ? RapidApiKeyHeaderName : ApiKeyHeaderName, _clientConfiguration.Value.ApiKey)
.SetQueryParam(nameof(fileUri), fileUri.AbsoluteUri)
.SetQueryParam(OptimizationModeParamName, mode.ToString())
.GetBytesAsync(cancellationToken)
.ConfigureAwait(false), cancellationToken)
.ConfigureAwait(false);
......@@ -166,9 +161,7 @@ public Task<bool> IsThumbnailGenerationSupportedAsync(string contentType)
/// <inheritdoc/>
public async Task<byte[]> OptimizeImageAsync(
byte[] contents, string contentType,
string? fileName,
ImageOptimizationMode mode,
CancellationToken cancellationToken)
string? fileName, CancellationToken cancellationToken)
{
_validator.ValidateFileBytes(contents);
_validator.ValidateContentTypeForImageOptimization(contentType, @throw: true);
......@@ -180,7 +173,6 @@ public Task<bool> IsThumbnailGenerationSupportedAsync(string contentType)
.Combine(_clientConfiguration.Value.BaseUri.AbsoluteUri, $"{(isRapidApi ? string.Empty : "api/")}v1/optimize")
.WithTimeout(_clientConfiguration.Value.Timeout)
.WithHeader(isRapidApi ? RapidApiKeyHeaderName : ApiKeyHeaderName, _clientConfiguration.Value.ApiKey)
.SetQueryParam(OptimizationModeParamName, mode.ToString())
.PostMultipartAsync(mp => mp.AddFile("file", new MemoryStream(contents), fileName, contentType), cancellationToken)
.ReceiveBytes()
.ConfigureAwait(false), cancellationToken)
......
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