コード例 #1
0
 public static void Delete(BlogSystemContext dataBaseContext, IHostingEnvironment hostingEnvironment,
                           IEnumerable <Guid> deleteGuid)
 {
     deleteGuid.ToList().ForEach(e =>
     {
         var template = dataBaseContext.Templates.FirstOrDefault(q => q.Guid == e);
         if (template == null)
         {
             return;
         }
         var uploadUnit = new UploadUnit(dataBaseContext, hostingEnvironment);
         //清除所有模板文件
         dataBaseContext.TemplateFiles
         .Where(t => t.TemplateGuid == template.Guid)
         .Select(o => o.Guid)
         .ToList().ForEach(d =>
         {
             var templateFile = dataBaseContext.TemplateFiles.FirstOrDefault(q => q.Guid == d);
             if (templateFile == null)
             {
                 return;
             }
             var uploadedFileGuid = templateFile.UploadedFileGuid;
             dataBaseContext.TemplateFiles.Remove(templateFile);
             uploadUnit.CheckAndDeleteFile(uploadedFileGuid);
         });
         dataBaseContext.Templates.Remove(template);
     });
 }
コード例 #2
0
        public static void Delete(BlogSystemContext dataBaseContext, IHostingEnvironment hostingEnvironment,
                                  IEnumerable <Guid> deleteGuid)
        {
            var uploadUnit = new UploadUnit(dataBaseContext, hostingEnvironment);

            deleteGuid.ToList().ForEach(d =>
            {
                var article = dataBaseContext.Articles.FirstOrDefault(q => q.Guid == d);
                if (article == null)
                {
                    return;
                }
                dataBaseContext.Articles.Remove(article);
            });
        }
コード例 #3
0
        public static List <Guid> Delete(BlogSystemContext dataBaseContext, IHostingEnvironment hostingEnvironment,
                                         IEnumerable <Guid> deleteGuid)
        {
            var templateGuids = new List <Guid>();
            var uploadUnit    = new UploadUnit(dataBaseContext, hostingEnvironment);

            deleteGuid.ToList().ForEach(d =>
            {
                var templateFile = dataBaseContext.TemplateFiles.FirstOrDefault(q => q.Guid == d);
                if (templateFile == null)
                {
                    return;
                }
                var uploadedFileGuid = templateFile.UploadedFileGuid;
                dataBaseContext.TemplateFiles.Remove(templateFile);
                uploadUnit.CheckAndDeleteFile(uploadedFileGuid);
                templateGuids.Add(templateFile.Guid);
            });
            return(templateGuids);
        }
コード例 #4
0
        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);
        }