123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- using Microsoft.AspNetCore.Http.Features;
- using Microsoft.AspNetCore.Server.Kestrel.Core;
- using Microsoft.AspNetCore.StaticFiles;
- using Microsoft.Extensions.FileProviders;
- using Microsoft.Extensions.Options;
- using OrBit.MesFileServiceNode.Host.Middleware;
- using OrBit.MesFileServiceNode.Host.Model;
- public class Startup
- {
- public IConfiguration Configuration { get; }
- public Startup(IConfiguration configuration)
- {
- Configuration = configuration;
- }
- public void ConfigureServices(IServiceCollection services)
- {
- services.Configure<KestrelServerOptions>(options =>
- {
- // Set the limit to 512 MB
- options.Limits.MaxRequestBodySize = 536346624;
- });
- services.Configure<IISServerOptions>(options =>
- {
- options.MaxRequestBodySize = 536346624;
- });
- services.Configure<FormOptions>(x => x.MultipartBodyLengthLimit = 536346624);
- services.Configure<NodeConfig>(Configuration.GetSection("OrBitFileServerNode"));
- services.AddSingleton<ThumbnailMiddleware>();
- services.AddSingleton<UploadFileMiddleware>();
- }
- public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptions<NodeConfig> config)
- {
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
- var provider = new FileExtensionContentTypeProvider();
- if (config.Value.MimeModifiers != null && config.Value.MimeModifiers.Length > 0)
- {
- foreach (var mimeModifer in config.Value.MimeModifiers)
- {
- if (string.IsNullOrWhiteSpace(mimeModifer.Ext))
- {
- continue;
- }
- switch (mimeModifer.Opt)
- {
- case MimeModiferOpts.add:
- {
- if (!string.IsNullOrWhiteSpace(mimeModifer.Type))
- {
- provider.Mappings[mimeModifer.Ext] = mimeModifer.Type;
- }
- break;
- }
- case MimeModiferOpts.remove:
- provider.Mappings.Remove(mimeModifer.Ext);
- break;
- }
- }
- }
- //生成缩略图
- if (config.Value.EnableThumbnail)
- {
- app.UseWhen(
- context => context.Request.Path.StartsWithSegments(config.Value.VirtualPath,
- StringComparison.OrdinalIgnoreCase),
- appBuilder => { appBuilder.UseMiddleware<ThumbnailMiddleware>(); });
- }
- //访问
- app.UseStaticFiles(new StaticFileOptions
- {
- FileProvider = new PhysicalFileProvider(config.Value.PhysicalPath),
- RequestPath = config.Value.VirtualPath,
- ContentTypeProvider = provider,
- ServeUnknownFileTypes=true,//ServeUnknownFileTypes
- DefaultContentType = "application/octet-stream",//默认为文件流
- OnPrepareResponse = (ctx) =>
- {
- ctx.Context.Response.Headers.Append("Cache-Control",
- $"public, max-age={config.Value.CachePeriod}");
- ctx.Context.Response.Headers.Append("Access-Control-Allow-Origin", "*");
- },
- });
- app.UseWhen(
- context => context.Request.Path.StartsWithSegments("/uploadfile",
- StringComparison.OrdinalIgnoreCase),
- appBuilder => { appBuilder.UseMiddleware<UploadFileMiddleware>(); });
- //访问根目录
- app.UseWhen(context => context.Request.Path.Value.Equals("/"),
- builder => builder.Run(async ctx => { await ctx.Response.WriteAsync("Hello OrBit File Server!"); }));
- app.Run(async context =>
- {
- context.Response.StatusCode = 404;
- await context.Response.WriteAsync("not found:" + context.Request.Path.Value);
- });
- }
- }
|