Skip to content

Commit 2d5e0d4

Browse files
Revert unnecessary changes
1 parent 67599ae commit 2d5e0d4

2 files changed

Lines changed: 104 additions & 61 deletions

File tree

samples/ImageSharp.Web.Sample/Startup.cs

Lines changed: 67 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using Microsoft.Extensions.Configuration;
99
using Microsoft.Extensions.DependencyInjection;
1010
using Microsoft.Extensions.Hosting;
11-
using Microsoft.IO;
1211
using SixLabors.ImageSharp.Web.Caching;
1312
using SixLabors.ImageSharp.Web.Commands;
1413
using SixLabors.ImageSharp.Web.DependencyInjection;
@@ -38,67 +37,97 @@ public class Startup
3837
/// </summary>
3938
/// <param name="services">The collection of service desscriptors.</param>
4039
public void ConfigureServices(IServiceCollection services)
41-
=> services.AddImageSharp(); // Add the default services and options
40+
{
41+
services.AddImageSharp()
42+
.SetRequestParser<QueryCollectionRequestParser>()
43+
.Configure<PhysicalFileSystemCacheOptions>(options =>
44+
{
45+
options.CacheRootPath = null;
46+
options.CacheFolder = "is-cache";
47+
options.CacheFolderDepth = 8;
48+
})
49+
.SetCache<PhysicalFileSystemCache>()
50+
.SetCacheKey<UriRelativeLowerInvariantCacheKey>()
51+
.SetCacheHash<SHA256CacheHash>()
52+
.Configure<PhysicalFileSystemProviderOptions>(options =>
53+
{
54+
options.ProviderRootPath = null;
55+
})
56+
.AddProvider<PhysicalFileSystemProvider>()
57+
.AddProcessor<ResizeWebProcessor>()
58+
.AddProcessor<FormatWebProcessor>()
59+
.AddProcessor<BackgroundColorWebProcessor>()
60+
.AddProcessor<QualityWebProcessor>();
61+
62+
// Add the default service and options.
63+
//
64+
// services.AddImageSharp();
65+
66+
// Or add the default service and custom options.
67+
//
68+
// this.ConfigureDefaultServicesAndCustomOptions(services);
69+
70+
// Or we can fine-grain control adding the default options and configure all other services.
71+
//
72+
// this.ConfigureCustomServicesAndDefaultOptions(services);
73+
74+
// Or we can fine-grain control adding custom options and configure all other services
75+
// There are also factory methods for each builder that will allow building from configuration files.
76+
//
77+
// this.ConfigureCustomServicesAndCustomOptions(services);
78+
}
4279

43-
// Or add the default services and custom options
4480
private void ConfigureDefaultServicesAndCustomOptions(IServiceCollection services)
45-
=> services.AddImageSharp(options =>
81+
{
82+
services.AddImageSharp(options =>
4683
{
4784
options.Configuration = Configuration.Default;
48-
options.MemoryStreamManager = new RecyclableMemoryStreamManager();
49-
options.UseInvariantParsingCulture = true;
5085
options.BrowserMaxAge = TimeSpan.FromDays(7);
5186
options.CacheMaxAge = TimeSpan.FromDays(365);
52-
options.CacheHashLength = 12;
87+
options.CacheHashLength = 8;
5388
options.OnParseCommandsAsync = _ => Task.CompletedTask;
5489
options.OnBeforeSaveAsync = _ => Task.CompletedTask;
5590
options.OnProcessedAsync = _ => Task.CompletedTask;
5691
options.OnPrepareResponseAsync = _ => Task.CompletedTask;
5792
});
93+
}
5894

59-
// Or we can fine-grain control adding the default options and configure all other services
6095
private void ConfigureCustomServicesAndDefaultOptions(IServiceCollection services)
61-
=> services.AddImageSharp()
62-
.RemoveProcessor<FormatWebProcessor>()
63-
.RemoveProcessor<BackgroundColorWebProcessor>();
96+
{
97+
services.AddImageSharp()
98+
.RemoveProcessor<FormatWebProcessor>()
99+
.RemoveProcessor<BackgroundColorWebProcessor>();
100+
}
64101

65-
// Or we can fine-grain control adding custom options and configure all other services
66-
// There are also factory methods for each builder that will allow building from configuration files
67102
private void ConfigureCustomServicesAndCustomOptions(IServiceCollection services)
68-
=> services.AddImageSharp(options =>
103+
{
104+
services.AddImageSharp(options =>
69105
{
70106
options.Configuration = Configuration.Default;
71-
options.MemoryStreamManager = new RecyclableMemoryStreamManager();
72-
options.UseInvariantParsingCulture = true;
73107
options.BrowserMaxAge = TimeSpan.FromDays(7);
74108
options.CacheMaxAge = TimeSpan.FromDays(365);
75-
options.CacheHashLength = 12;
109+
options.CacheHashLength = 8;
76110
options.OnParseCommandsAsync = _ => Task.CompletedTask;
77111
options.OnBeforeSaveAsync = _ => Task.CompletedTask;
78112
options.OnProcessedAsync = _ => Task.CompletedTask;
79113
options.OnPrepareResponseAsync = _ => Task.CompletedTask;
80114
})
81-
.SetRequestParser<QueryCollectionRequestParser>()
82-
.SetCache<PhysicalFileSystemCache>()
83-
.Configure<PhysicalFileSystemCacheOptions>(options =>
84-
{
85-
options.CacheFolder = "is-cache";
86-
options.CacheFolderDepth = 8;
87-
})
88-
.SetCacheKey<UriRelativeLowerInvariantCacheKey>()
89-
.SetCacheHash<SHA256CacheHash>()
90-
.ClearProviders()
91-
.AddProvider<PhysicalFileSystemProvider>()
92-
.Configure<PhysicalFileSystemProviderOptions>(options =>
93-
{
94-
options.ProviderRootPath = null;
95-
options.ProcessingBehavior = ProcessingBehavior.CommandOnly;
96-
})
97-
.ClearProcessors()
98-
.AddProcessor<ResizeWebProcessor>()
99-
.AddProcessor<FormatWebProcessor>()
100-
.AddProcessor<BackgroundColorWebProcessor>()
101-
.AddProcessor<QualityWebProcessor>();
115+
.SetRequestParser<QueryCollectionRequestParser>()
116+
.Configure<PhysicalFileSystemCacheOptions>(options =>
117+
{
118+
options.CacheFolder = "different-cache";
119+
})
120+
.SetCache<PhysicalFileSystemCache>()
121+
.SetCacheKey<UriRelativeLowerInvariantCacheKey>()
122+
.SetCacheHash<SHA256CacheHash>()
123+
.ClearProviders()
124+
.AddProvider<PhysicalFileSystemProvider>()
125+
.ClearProcessors()
126+
.AddProcessor<ResizeWebProcessor>()
127+
.AddProcessor<FormatWebProcessor>()
128+
.AddProcessor<BackgroundColorWebProcessor>()
129+
.AddProcessor<QualityWebProcessor>();
130+
}
102131

103132
/// <summary>
104133
/// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

src/ImageSharp.Web/DependencyInjection/ServiceCollectionExtensions.cs

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

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

2931
/// <summary>
3032
/// Adds ImageSharp services to the specified <see cref="IServiceCollection" /> with the given options.
3133
/// </summary>
3234
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
33-
/// <param name="configureOptions">An <see cref="Action{ImageSharpMiddlewareOptions}"/> to configure the provided <see cref="ImageSharpMiddlewareOptions"/>.</param>
35+
/// <param name="setupAction">An <see cref="Action{ImageSharpMiddlewareOptions}"/> to configure the provided <see cref="ImageSharpMiddlewareOptions"/>.</param>
3436
/// <returns>An <see cref="IImageSharpBuilder"/> that can be used to further configure the ImageSharp services.</returns>
35-
public static IImageSharpBuilder AddImageSharp(this IServiceCollection services, Action<ImageSharpMiddlewareOptions> configureOptions)
36-
=> services.AddImageSharp().Configure(configureOptions);
37+
public static IImageSharpBuilder AddImageSharp(
38+
this IServiceCollection services,
39+
Action<ImageSharpMiddlewareOptions> setupAction)
40+
{
41+
Guard.NotNull(services, nameof(services));
42+
Guard.NotNull(setupAction, nameof(setupAction));
43+
44+
IImageSharpBuilder builder = new ImageSharpBuilder(services);
45+
46+
AddDefaultServices(builder, setupAction);
3747

38-
private static IImageSharpBuilder AddDefaultServices(this IImageSharpBuilder builder)
48+
return builder;
49+
}
50+
51+
private static void AddDefaultServices(
52+
IImageSharpBuilder builder,
53+
Action<ImageSharpMiddlewareOptions> setupAction)
3954
{
40-
builder.Services.AddOptions();
55+
builder.Services.Configure(setupAction);
56+
4157
builder.Services.AddSingleton<FormatUtilities>();
58+
4259
builder.Services.AddSingleton<AsyncKeyReaderWriterLock<string>>();
4360

44-
// Command parsing
45-
builder.Services.AddSingleton<CommandParser>();
4661
builder.SetRequestParser<QueryCollectionRequestParser>();
4762

63+
builder.SetCache<PhysicalFileSystemCache>();
64+
65+
builder.SetCacheKey<UriRelativeLowerInvariantCacheKey>();
66+
67+
builder.SetCacheHash<SHA256CacheHash>();
68+
69+
builder.AddProvider<PhysicalFileSystemProvider>();
70+
71+
builder.AddProcessor<ResizeWebProcessor>()
72+
.AddProcessor<FormatWebProcessor>()
73+
.AddProcessor<BackgroundColorWebProcessor>()
74+
.AddProcessor<QualityWebProcessor>();
75+
4876
builder.AddConverter<IntegralNumberConverter<sbyte>>();
4977
builder.AddConverter<IntegralNumberConverter<byte>>();
5078
builder.AddConverter<IntegralNumberConverter<short>>();
@@ -91,21 +119,7 @@ private static IImageSharpBuilder AddDefaultServices(this IImageSharpBuilder bui
91119
builder.AddConverter<ColorConverter>();
92120
builder.AddConverter<EnumConverter>();
93121

94-
// Cache
95-
builder.SetCache<PhysicalFileSystemCache>();
96-
builder.SetCacheKey<UriRelativeLowerInvariantCacheKey>();
97-
builder.SetCacheHash<SHA256CacheHash>();
98-
99-
// Providers
100-
builder.AddProvider<PhysicalFileSystemProvider>();
101-
102-
// Processors
103-
builder.AddProcessor<ResizeWebProcessor>()
104-
.AddProcessor<FormatWebProcessor>()
105-
.AddProcessor<BackgroundColorWebProcessor>()
106-
.AddProcessor<QualityWebProcessor>();
107-
108-
return builder;
122+
builder.Services.AddSingleton<CommandParser>();
109123
}
110124
}
111125
}

0 commit comments

Comments
 (0)