static void Config(BlogSystemContext dataBaseContext, IDistributedCache cache, Configs configs) { var configUnit = new ConfigUnit(dataBaseContext, cache) { Email = configs.Email, Password = PasswordHelper.CreateDbPassword(configs.Password, false), BlogName = configs.BlogName, UserName = configs.UserName, TemplateGuid = Guid.Parse("dd5f4fa2-545d-4d95-927b-94df4103483e") }; configUnit.SaveAll(); }
public async Task InvokeAsync(HttpContext context, BlogSystemContext dataBaseContext, IOptions <SiteOptions> options, IHostingEnvironment hostingEnvironment, IDistributedCache cache) { var requestUrl = HttpUtility.UrlDecode(context.Request.Path); //如果路径不是以/blog就跳过 if (!requestUrl.ToLower().StartsWith("/blog", StringComparison.Ordinal)) { await _next(context); return; } //如果没有设置模板直接返回404 Guid?templateGuid; try { if ((templateGuid = new ConfigUnit(dataBaseContext, cache).TemplateGuid) == null) { context.Response.StatusCode = 404; return; } } catch (NotDataBaseException) { //数据库不存在,跳转到初始化页面 context.Response.Redirect(options.Value.GetAdminUrl("Setup", context.Request.PathBase)); return; } requestUrl = requestUrl.Remove(0, 5); if (string.IsNullOrEmpty(requestUrl)) { requestUrl = "/"; } var templateFile = GetUploadedFile(GetUploadedFiles(dataBaseContext, cache, templateGuid.Value), requestUrl); if (templateFile == null) { await _next(context); return; } var uploadUnit = new UploadUnit(dataBaseContext, hostingEnvironment); var enableGzip = false; var acceptEncoding = context.Request.Headers["Accept-Encoding"].FirstOrDefault(); if (acceptEncoding != null && acceptEncoding.Split(',').Any(q => q == "gzip")) { enableGzip = true; } var fileInfo = uploadUnit.GetFileInfo(templateFile.UploadedFile, enableGzip); if (fileInfo == null || !fileInfo.Exists) { enableGzip = !enableGzip; fileInfo = uploadUnit.GetFileInfo(templateFile.UploadedFile, enableGzip); if (fileInfo == null || !fileInfo.Exists) { context.Response.StatusCode = 404; return; } } //向Cookie添加站点设置信息 context.Response.Cookies.Append("SimpleBlog_Settings", JsonConvert.SerializeObject(new { adminUrl = options.Value.GetAdminUrl(string.Empty, context.Request.PathBase), apiUrl = options.Value.GetApiUrl(string.Empty, context.Request.PathBase) }), new CookieOptions() { HttpOnly = false, Path = string.IsNullOrEmpty(options.Value.HomeUrl) ? options.Value.GetHomeUrl(string.Empty, context.Request.PathBase) : "/" }); context.Response.ContentType = templateFile.MIME; context.Response.ContentLength = fileInfo.Length; if (enableGzip) { context.Response.Headers.Add("Content-Encoding", "gzip"); } context.Response.Headers.Add("Content-Disposition", $"{(context.Request.Query["download"].Any() ? "attachment;" : string.Empty)}filename*={Encoding.Default.BodyName}''{HttpUtility.UrlEncode(templateFile.Name)}"); await context.Response.SendFileAsync(fileInfo.FullName); }