Skip to content

Commit

Permalink
Revert unnecessary changes
Browse files Browse the repository at this point in the history
  • Loading branch information
JimBobSquarePants committed Apr 22, 2022
1 parent 67599ae commit 2d5e0d4
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 61 deletions.
105 changes: 67 additions & 38 deletions samples/ImageSharp.Web.Sample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.IO;
using SixLabors.ImageSharp.Web.Caching;
using SixLabors.ImageSharp.Web.Commands;
using SixLabors.ImageSharp.Web.DependencyInjection;
Expand Down Expand Up @@ -38,67 +37,97 @@ public class Startup
/// </summary>
/// <param name="services">The collection of service desscriptors.</param>
public void ConfigureServices(IServiceCollection services)
=> services.AddImageSharp(); // Add the default services and options
{
services.AddImageSharp()
.SetRequestParser<QueryCollectionRequestParser>()
.Configure<PhysicalFileSystemCacheOptions>(options =>
{
options.CacheRootPath = null;
options.CacheFolder = "is-cache";
options.CacheFolderDepth = 8;
})
.SetCache<PhysicalFileSystemCache>()
.SetCacheKey<UriRelativeLowerInvariantCacheKey>()
.SetCacheHash<SHA256CacheHash>()
.Configure<PhysicalFileSystemProviderOptions>(options =>
{
options.ProviderRootPath = null;
})
.AddProvider<PhysicalFileSystemProvider>()
.AddProcessor<ResizeWebProcessor>()
.AddProcessor<FormatWebProcessor>()
.AddProcessor<BackgroundColorWebProcessor>()
.AddProcessor<QualityWebProcessor>();

// Add the default service and options.
//
// services.AddImageSharp();

// Or add the default service and custom options.
//
// this.ConfigureDefaultServicesAndCustomOptions(services);

// Or we can fine-grain control adding the default options and configure all other services.
//
// this.ConfigureCustomServicesAndDefaultOptions(services);

// Or we can fine-grain control adding custom options and configure all other services
// There are also factory methods for each builder that will allow building from configuration files.
//
// this.ConfigureCustomServicesAndCustomOptions(services);
}

// Or add the default services and custom options
private void ConfigureDefaultServicesAndCustomOptions(IServiceCollection services)
=> services.AddImageSharp(options =>
{
services.AddImageSharp(options =>
{
options.Configuration = Configuration.Default;
options.MemoryStreamManager = new RecyclableMemoryStreamManager();
options.UseInvariantParsingCulture = true;
options.BrowserMaxAge = TimeSpan.FromDays(7);
options.CacheMaxAge = TimeSpan.FromDays(365);
options.CacheHashLength = 12;
options.CacheHashLength = 8;
options.OnParseCommandsAsync = _ => Task.CompletedTask;
options.OnBeforeSaveAsync = _ => Task.CompletedTask;
options.OnProcessedAsync = _ => Task.CompletedTask;
options.OnPrepareResponseAsync = _ => Task.CompletedTask;
});
}

// Or we can fine-grain control adding the default options and configure all other services
private void ConfigureCustomServicesAndDefaultOptions(IServiceCollection services)
=> services.AddImageSharp()
.RemoveProcessor<FormatWebProcessor>()
.RemoveProcessor<BackgroundColorWebProcessor>();
{
services.AddImageSharp()
.RemoveProcessor<FormatWebProcessor>()
.RemoveProcessor<BackgroundColorWebProcessor>();
}

// Or we can fine-grain control adding custom options and configure all other services
// There are also factory methods for each builder that will allow building from configuration files
private void ConfigureCustomServicesAndCustomOptions(IServiceCollection services)
=> services.AddImageSharp(options =>
{
services.AddImageSharp(options =>
{
options.Configuration = Configuration.Default;
options.MemoryStreamManager = new RecyclableMemoryStreamManager();
options.UseInvariantParsingCulture = true;
options.BrowserMaxAge = TimeSpan.FromDays(7);
options.CacheMaxAge = TimeSpan.FromDays(365);
options.CacheHashLength = 12;
options.CacheHashLength = 8;
options.OnParseCommandsAsync = _ => Task.CompletedTask;
options.OnBeforeSaveAsync = _ => Task.CompletedTask;
options.OnProcessedAsync = _ => Task.CompletedTask;
options.OnPrepareResponseAsync = _ => Task.CompletedTask;
})
.SetRequestParser<QueryCollectionRequestParser>()
.SetCache<PhysicalFileSystemCache>()
.Configure<PhysicalFileSystemCacheOptions>(options =>
{
options.CacheFolder = "is-cache";
options.CacheFolderDepth = 8;
})
.SetCacheKey<UriRelativeLowerInvariantCacheKey>()
.SetCacheHash<SHA256CacheHash>()
.ClearProviders()
.AddProvider<PhysicalFileSystemProvider>()
.Configure<PhysicalFileSystemProviderOptions>(options =>
{
options.ProviderRootPath = null;
options.ProcessingBehavior = ProcessingBehavior.CommandOnly;
})
.ClearProcessors()
.AddProcessor<ResizeWebProcessor>()
.AddProcessor<FormatWebProcessor>()
.AddProcessor<BackgroundColorWebProcessor>()
.AddProcessor<QualityWebProcessor>();
.SetRequestParser<QueryCollectionRequestParser>()
.Configure<PhysicalFileSystemCacheOptions>(options =>
{
options.CacheFolder = "different-cache";
})
.SetCache<PhysicalFileSystemCache>()
.SetCacheKey<UriRelativeLowerInvariantCacheKey>()
.SetCacheHash<SHA256CacheHash>()
.ClearProviders()
.AddProvider<PhysicalFileSystemProvider>()
.ClearProcessors()
.AddProcessor<ResizeWebProcessor>()
.AddProcessor<FormatWebProcessor>()
.AddProcessor<BackgroundColorWebProcessor>()
.AddProcessor<QualityWebProcessor>();
}

/// <summary>
/// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using SixLabors.ImageSharp.Web.Caching;
using SixLabors.ImageSharp.Web.Commands;
using SixLabors.ImageSharp.Web.Commands.Converters;
Expand All @@ -24,27 +26,53 @@ public static class ServiceCollectionExtensions
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <returns>An <see cref="IImageSharpBuilder"/> that can be used to further configure the ImageSharp services.</returns>
public static IImageSharpBuilder AddImageSharp(this IServiceCollection services)
=> new ImageSharpBuilder(services).AddDefaultServices();
=> AddImageSharp(services, _ => { });

/// <summary>
/// Adds ImageSharp services to the specified <see cref="IServiceCollection" /> with the given options.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <param name="configureOptions">An <see cref="Action{ImageSharpMiddlewareOptions}"/> to configure the provided <see cref="ImageSharpMiddlewareOptions"/>.</param>
/// <param name="setupAction">An <see cref="Action{ImageSharpMiddlewareOptions}"/> to configure the provided <see cref="ImageSharpMiddlewareOptions"/>.</param>
/// <returns>An <see cref="IImageSharpBuilder"/> that can be used to further configure the ImageSharp services.</returns>
public static IImageSharpBuilder AddImageSharp(this IServiceCollection services, Action<ImageSharpMiddlewareOptions> configureOptions)
=> services.AddImageSharp().Configure(configureOptions);
public static IImageSharpBuilder AddImageSharp(
this IServiceCollection services,
Action<ImageSharpMiddlewareOptions> setupAction)
{
Guard.NotNull(services, nameof(services));
Guard.NotNull(setupAction, nameof(setupAction));

IImageSharpBuilder builder = new ImageSharpBuilder(services);

AddDefaultServices(builder, setupAction);

private static IImageSharpBuilder AddDefaultServices(this IImageSharpBuilder builder)
return builder;
}

private static void AddDefaultServices(
IImageSharpBuilder builder,
Action<ImageSharpMiddlewareOptions> setupAction)
{
builder.Services.AddOptions();
builder.Services.Configure(setupAction);

builder.Services.AddSingleton<FormatUtilities>();

builder.Services.AddSingleton<AsyncKeyReaderWriterLock<string>>();

// Command parsing
builder.Services.AddSingleton<CommandParser>();
builder.SetRequestParser<QueryCollectionRequestParser>();

builder.SetCache<PhysicalFileSystemCache>();

builder.SetCacheKey<UriRelativeLowerInvariantCacheKey>();

builder.SetCacheHash<SHA256CacheHash>();

builder.AddProvider<PhysicalFileSystemProvider>();

builder.AddProcessor<ResizeWebProcessor>()
.AddProcessor<FormatWebProcessor>()
.AddProcessor<BackgroundColorWebProcessor>()
.AddProcessor<QualityWebProcessor>();

builder.AddConverter<IntegralNumberConverter<sbyte>>();
builder.AddConverter<IntegralNumberConverter<byte>>();
builder.AddConverter<IntegralNumberConverter<short>>();
Expand Down Expand Up @@ -91,21 +119,7 @@ private static IImageSharpBuilder AddDefaultServices(this IImageSharpBuilder bui
builder.AddConverter<ColorConverter>();
builder.AddConverter<EnumConverter>();

// Cache
builder.SetCache<PhysicalFileSystemCache>();
builder.SetCacheKey<UriRelativeLowerInvariantCacheKey>();
builder.SetCacheHash<SHA256CacheHash>();

// Providers
builder.AddProvider<PhysicalFileSystemProvider>();

// Processors
builder.AddProcessor<ResizeWebProcessor>()
.AddProcessor<FormatWebProcessor>()
.AddProcessor<BackgroundColorWebProcessor>()
.AddProcessor<QualityWebProcessor>();

return builder;
builder.Services.AddSingleton<CommandParser>();
}
}
}

0 comments on commit 2d5e0d4

Please sign in to comment.