Startup.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using Microsoft.AspNetCore.Http.Features;
  2. using Microsoft.AspNetCore.Server.Kestrel.Core;
  3. using Microsoft.AspNetCore.StaticFiles;
  4. using Microsoft.Extensions.FileProviders;
  5. using Microsoft.Extensions.Options;
  6. using OrBit.MesFileServiceNode.Host.Middleware;
  7. using OrBit.MesFileServiceNode.Host.Model;
  8. public class Startup
  9. {
  10. public IConfiguration Configuration { get; }
  11. public Startup(IConfiguration configuration)
  12. {
  13. Configuration = configuration;
  14. }
  15. public void ConfigureServices(IServiceCollection services)
  16. {
  17. services.Configure<KestrelServerOptions>(options =>
  18. {
  19. // Set the limit to 512 MB
  20. options.Limits.MaxRequestBodySize = 536346624;
  21. });
  22. services.Configure<IISServerOptions>(options =>
  23. {
  24. options.MaxRequestBodySize = 536346624;
  25. });
  26. services.Configure<FormOptions>(x => x.MultipartBodyLengthLimit = 536346624);
  27. services.Configure<NodeConfig>(Configuration.GetSection("OrBitFileServerNode"));
  28. services.AddSingleton<ThumbnailMiddleware>();
  29. services.AddSingleton<UploadFileMiddleware>();
  30. }
  31. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptions<NodeConfig> config)
  32. {
  33. if (env.IsDevelopment())
  34. {
  35. app.UseDeveloperExceptionPage();
  36. }
  37. var provider = new FileExtensionContentTypeProvider();
  38. if (config.Value.MimeModifiers != null && config.Value.MimeModifiers.Length > 0)
  39. {
  40. foreach (var mimeModifer in config.Value.MimeModifiers)
  41. {
  42. if (string.IsNullOrWhiteSpace(mimeModifer.Ext))
  43. {
  44. continue;
  45. }
  46. switch (mimeModifer.Opt)
  47. {
  48. case MimeModiferOpts.add:
  49. {
  50. if (!string.IsNullOrWhiteSpace(mimeModifer.Type))
  51. {
  52. provider.Mappings[mimeModifer.Ext] = mimeModifer.Type;
  53. }
  54. break;
  55. }
  56. case MimeModiferOpts.remove:
  57. provider.Mappings.Remove(mimeModifer.Ext);
  58. break;
  59. }
  60. }
  61. }
  62. //生成缩略图
  63. if (config.Value.EnableThumbnail)
  64. {
  65. app.UseWhen(
  66. context => context.Request.Path.StartsWithSegments(config.Value.VirtualPath,
  67. StringComparison.OrdinalIgnoreCase),
  68. appBuilder => { appBuilder.UseMiddleware<ThumbnailMiddleware>(); });
  69. }
  70. //访问
  71. app.UseStaticFiles(new StaticFileOptions
  72. {
  73. FileProvider = new PhysicalFileProvider(config.Value.PhysicalPath),
  74. RequestPath = config.Value.VirtualPath,
  75. ContentTypeProvider = provider,
  76. ServeUnknownFileTypes=true,//ServeUnknownFileTypes
  77. DefaultContentType = "application/octet-stream",//默认为文件流
  78. OnPrepareResponse = (ctx) =>
  79. {
  80. ctx.Context.Response.Headers.Append("Cache-Control",
  81. $"public, max-age={config.Value.CachePeriod}");
  82. ctx.Context.Response.Headers.Append("Access-Control-Allow-Origin", "*");
  83. },
  84. });
  85. app.UseWhen(
  86. context => context.Request.Path.StartsWithSegments("/uploadfile",
  87. StringComparison.OrdinalIgnoreCase),
  88. appBuilder => { appBuilder.UseMiddleware<UploadFileMiddleware>(); });
  89. //访问根目录
  90. app.UseWhen(context => context.Request.Path.Value.Equals("/"),
  91. builder => builder.Run(async ctx => { await ctx.Response.WriteAsync("Hello OrBit File Server!"); }));
  92. app.Run(async context =>
  93. {
  94. context.Response.StatusCode = 404;
  95. await context.Response.WriteAsync("not found:" + context.Request.Path.Value);
  96. });
  97. }
  98. }