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(options => { // Set the limit to 512 MB options.Limits.MaxRequestBodySize = 536346624; }); services.Configure(options => { options.MaxRequestBodySize = 536346624; }); services.Configure(x => x.MultipartBodyLengthLimit = 536346624); services.Configure(Configuration.GetSection("OrBitFileServerNode")); services.AddSingleton(); services.AddSingleton(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptions 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(); }); } //访问 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(); }); //访问根目录 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); }); } }