Пример #1
0
        /// <summary>
        /// 根据选中的栏目id生成栏目首页
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task ColumnIndexGenerate(List <int> ids)
        {
            if (ids.Count <= 0)
            {
                throw new UserFriendlyException("请选择要生成的栏目或者点击全部生成");
            }
            //拿到待生成的栏目集合
            var list = await _columnAppService.GetGenerateColumnByIds <GenerateColumnIndexOutput>(ids);

            var taskArray = new Task[list.Count];
            //拿到网站配置
            var siteOption =
                await _cacheManager.GetCache <string, SiteOptionDto>(EasyFastConsts.TemplateCacheKey)
                .GetAsync("siteOptionCache", () => _siteConfigAppService.GetSiteOption());

            for (int i = 0; i < list.Count; i++)
            {
                string fileName = list[i].IndexTemplate.Substring(list[i].IndexTemplate.LastIndexOf("\\", StringComparison.Ordinal) + 1);
                if (!File.Exists(list[i].IndexTemplate))
                {
                    throw new UserFriendlyException($"请在{siteOption.TemplateDir}目录下查看是否存在{fileName}模板文件");
                }
                var i1 = i;
                //拿到栏目的模板文件 以文件的最后修改时间做缓存
                var template = _cacheManager.GetCache <string, string>(EasyFastConsts.TemplateCacheKey).Get($"{fileName}{File.GetLastWriteTime(list[i].IndexTemplate)}",
                                                                                                            () => File.ReadAllText(list[i1].IndexTemplate, Encoding.UTF8));

                //生成规则 暂时仅支持{Id} {Name} {Year} {Month} {Day} [Title]
                var rulePath = RuleParseHelper.Parse(new StringBuilder(list[i1].IndexHtmlRule), list[i1].Id.ToString(), list[i1].Name);
                //地址 CurrentPath +  HtmlDir + Column Dir + rulePath  例 MapPath(~)\Column\Index_1.html
                taskArray[i] = _htmlGenerateManager.GenerateHtml(template, EasyFastConsts.BaseDirectory + siteOption.HTMLDir + list[i1].Dir + rulePath);
            }
            //等待所有的task执行完成
            await Task.WhenAll(taskArray);
        }
Пример #2
0
        /// <summary>
        /// 添加或修改单页
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task AddOrUpdateSingleAsync(SingleColumnDto model)
        {
            //拿到网站配置
            var siteOption =
                await _cacheManager.GetCache <string, SiteOptionDto>(EasyFastConsts.TemplateCacheKey)
                .GetAsync("siteOptionCache", () => _siteConfigAppService.GetSiteOption());

            if (string.IsNullOrWhiteSpace(model.IndexHtmlRule))
            {
                model.IndexHtmlRule = $"{model.Dir}index.html";
            }

            if (model.IsIndexHtml && !string.IsNullOrWhiteSpace(siteOption.HTMLDir) && !model.IndexHtmlRule.Contains(siteOption.HTMLDir))
            {
                model.IndexHtmlRule = siteOption.HTMLDir + model.IndexHtmlRule;
            }
            await _columnRepository.InsertOrUpdateAsync(model.MapTo <Core.Entities.Column>());
        }
Пример #3
0
        /// <summary>
        /// 根据选中的栏目id生成栏目首页
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="isAll">生成选项</param>
        /// <returns></returns>
        public async Task GenerateColumnIndex(List <int> ids, bool isAll)
        {
            if (!isAll)
            {
                if (ids.Count <= 0)
                {
                    throw new UserFriendlyException("请选择要生成的栏目或者点击全部生成");
                }
            }
            //拿到待生成的栏目集合
            var list = await _columnAppService.GetGenerateColumn <GenerateColumnIndexOutput>(new GenerateIndexSpecification(isAll, ids));

            var taskArray = new Task[list.Count];
            //拿到网站配置
            var siteOption =
                await _cacheManager.GetCache <string, SiteOptionDto>(EasyFastConsts.TemplateCacheKey)
                .GetAsync("siteOptionCache", () => _siteConfigAppService.GetSiteOption());

            for (var i = 0; i < list.Count; i++)
            {
                var fileName = list[i].IndexTemplate.Substring(list[i].IndexTemplate.LastIndexOf("\\", StringComparison.Ordinal) + 1);
                var fullPath = $"{EasyFastConsts.BaseDirectory}{siteOption.TemplateDir}{list[i].IndexTemplate}";
                if (!File.Exists(fullPath))
                {
                    throw new UserFriendlyException($"请在{siteOption.TemplateDir}目录下查看是否存在{fileName}模板文件");
                }
                var i1 = i;
                //拿到栏目的模板文件 以文件的最后修改时间做缓存
                var template = _cacheManager.GetCache <string, string>(EasyFastConsts.TemplateCacheKey).Get($"{fullPath}{File.GetLastWriteTime(fullPath)}",
                                                                                                            () => File.ReadAllText(fullPath, Encoding.UTF8));

                //生成规则 暂时仅支持{Id} {Name} {Year} {Month} {Day} [Title]
                var rulePath = RuleParseHelper.Parse(new StringBuilder(list[i1].IndexHtmlRule), list[i1].Id.ToString(), list[i1].Name, null);
                //地址 CurrentPath +  HtmlDir + Column Dir + rulePath  例 MapPath(~)\Index_1.html
                taskArray[i] = Task.Factory.StartNew(() => _htmlGenerateManager.GenerateHtml(template, EasyFastConsts.BaseDirectory + rulePath.Trim(), null));
            }
            //等待所有的task执行完成
            await Task.WhenAll(taskArray);

            Debug.WriteLine("执行成功!");
        }