public NameValueCollection CreateSiteByOnlineTemplateName(int siteId, bool isImportContents, bool isImportTableStyles, string onlineTemplateName, bool isUseTables, string userKeyPrefix, string administratorName) { var cacheTotalCountKey = userKeyPrefix + CacheTotalCount; var cacheCurrentCountKey = userKeyPrefix + CacheCurrentCount; var cacheMessageKey = userKeyPrefix + CacheMessage; CacheUtils.Insert(cacheTotalCountKey, "4"); //存储需要的页面总数 CacheUtils.Insert(cacheCurrentCountKey, "0"); //存储当前的页面总数 CacheUtils.Insert(cacheMessageKey, string.Empty); //存储消息 //返回“运行结果”、“错误信息”及“执行JS脚本”的字符串数组 NameValueCollection retval; try { CacheUtils.Insert(cacheCurrentCountKey, "1"); CacheUtils.Insert(cacheMessageKey, "开始下载模板压缩包,可能需要几分钟,请耐心等待..."); var filePath = PathUtility.GetSiteTemplatesPath($"T_{onlineTemplateName}.zip"); FileUtils.DeleteFileIfExists(filePath); var downloadUrl = OnlineTemplateManager.GetDownloadUrl(onlineTemplateName); WebClientUtils.SaveRemoteFileToLocal(downloadUrl, filePath); CacheUtils.Insert(cacheCurrentCountKey, "2"); CacheUtils.Insert(cacheMessageKey, "模板压缩包下载成功,开始解压缩,可能需要几分钟,请耐心等待..."); var siteTemplateDir = $"T_{onlineTemplateName}"; var directoryPath = PathUtility.GetSiteTemplatesPath(siteTemplateDir); DirectoryUtils.DeleteDirectoryIfExists(directoryPath); ZipUtils.UnpackFiles(filePath, directoryPath); CacheUtils.Insert(cacheCurrentCountKey, "3"); //存储当前的页面总数 CacheUtils.Insert(cacheMessageKey, "站点模板下载成功,正在导入数据..."); //存储消息 SiteTemplateManager.Instance.ImportSiteTemplateToEmptySite(siteId, siteTemplateDir, isUseTables, isImportContents, isImportTableStyles, administratorName); CreateManager.CreateAll(siteId); CacheUtils.Insert(cacheCurrentCountKey, "4"); //存储当前的页面总数 CacheUtils.Insert(cacheMessageKey, "创建成功!"); //存储消息 var siteInfo = SiteManager.GetSiteInfo(siteId); retval = AjaxManager.GetWaitingTaskNameValueCollection($"站点 <strong>{siteInfo.SiteName}<strong> 创建成功!", string.Empty, $"top.location.href='{PageInitialization.GetRedirectUrl()}';"); } catch (Exception ex) { retval = AjaxManager.GetWaitingTaskNameValueCollection(string.Empty, ex.Message, string.Empty); LogUtils.AddErrorLog(ex); } CacheUtils.Remove(cacheTotalCountKey); //取消存储需要的页面总数 CacheUtils.Remove(cacheCurrentCountKey); //取消存储当前的页面总数 CacheUtils.Remove(cacheMessageKey); //取消存储消息 CacheUtils.ClearAll(); return(retval); }
public async Task <ActionResult <IntResult> > Submit([FromBody] SubmitRequest request) { if (!await _authManager.HasAppPermissionsAsync(Types.AppPermissions.SettingsSitesAdd)) { return(Unauthorized()); } if (!request.Root) { if (_pathManager.IsSystemDirectory(request.SiteDir)) { return(this.Error("文件夹名称不能为系统文件夹名称,请更改文件夹名称!")); } if (!DirectoryUtils.IsDirectoryNameCompliant(request.SiteDir)) { return(this.Error("文件夹名称不符合系统要求,请更改文件夹名称!")); } var sitePath = await _pathManager.GetSitePathAsync(request.ParentId); var directories = DirectoryUtils.GetDirectoryNames(sitePath); if (ListUtils.ContainsIgnoreCase(directories, request.SiteDir)) { return(this.Error("已存在相同的文件夹,请更改文件夹名称!")); } var list = await _siteRepository.GetSiteDirsAsync(request.ParentId); if (ListUtils.ContainsIgnoreCase(list, request.SiteDir)) { return(this.Error("已存在相同的站点文件夹,请更改文件夹名称!")); } } var channelInfo = new Channel(); channelInfo.ChannelName = channelInfo.IndexName = "首页"; channelInfo.ParentId = 0; channelInfo.ContentModelPluginId = string.Empty; var tableName = string.Empty; if (StringUtils.EqualsIgnoreCase(request.SiteType, Types.SiteTypes.Web) || StringUtils.EqualsIgnoreCase(request.SiteType, Types.SiteTypes.Wx)) { if (request.TableRule == TableRule.Choose) { tableName = request.TableChoose; } else if (request.TableRule == TableRule.HandWrite) { tableName = request.TableHandWrite; if (!await _settingsManager.Database.IsTableExistsAsync(tableName)) { await _contentRepository.CreateContentTableAsync(tableName, _contentRepository.GetTableColumns(tableName)); } else { await _settingsManager.Database.AlterTableAsync(tableName, _contentRepository.GetTableColumns(tableName)); } } } var adminId = _authManager.AdminId; var siteId = await _siteRepository.InsertSiteAsync(_pathManager, channelInfo, new Site { SiteName = request.SiteName, SiteType = request.SiteType, SiteDir = request.SiteDir, TableName = tableName, ParentId = request.ParentId, Root = request.Root }, adminId); if (string.IsNullOrEmpty(tableName)) { tableName = await _contentRepository.CreateNewContentTableAsync(); await _siteRepository.UpdateTableNameAsync(siteId, tableName); } if (await _authManager.IsSiteAdminAsync() && !await _authManager.IsSuperAdminAsync()) { var siteIdList = await _authManager.GetSiteIdsAsync() ?? new List <int>(); siteIdList.Add(siteId); var adminInfo = await _administratorRepository.GetByUserIdAsync(adminId); await _administratorRepository.UpdateSiteIdsAsync(adminInfo, siteIdList); } var caching = new CacheUtils(_cacheManager); var site = await _siteRepository.GetAsync(siteId); caching.SetProcess(request.Guid, "任务初始化..."); if (request.CreateType == "local") { var manager = new SiteTemplateManager(_pathManager, _databaseManager, caching); await manager.ImportSiteTemplateToEmptySiteAsync(site, request.CreateTemplateId, request.IsImportContents, request.IsImportTableStyles, adminId, request.Guid); caching.SetProcess(request.Guid, "生成站点页面..."); await _createManager.CreateByAllAsync(site.Id); caching.SetProcess(request.Guid, "清除系统缓存..."); _cacheManager.Clear(); } else if (request.CreateType == "cloud") { caching.SetProcess(request.Guid, "开始下载模板压缩包,可能需要几分钟,请耐心等待..."); var filePath = _pathManager.GetSiteTemplatesPath($"T_{request.CreateTemplateId}.zip"); FileUtils.DeleteFileIfExists(filePath); var downloadUrl = OnlineTemplateManager.GetDownloadUrl(request.CreateTemplateId); WebClientUtils.Download(downloadUrl, filePath); caching.SetProcess(request.Guid, "模板压缩包下载成功,开始解压缩,可能需要几分钟,请耐心等待..."); var siteTemplateDir = $"T_{request.CreateTemplateId}"; var directoryPath = _pathManager.GetSiteTemplatesPath(siteTemplateDir); DirectoryUtils.DeleteDirectoryIfExists(directoryPath); _pathManager.ExtractZip(filePath, directoryPath); caching.SetProcess(request.Guid, "模板压缩包解压成功,正在导入数据..."); var manager = new SiteTemplateManager(_pathManager, _databaseManager, caching); await manager.ImportSiteTemplateToEmptySiteAsync(site, siteTemplateDir, request.IsImportContents, request.IsImportTableStyles, adminId, request.Guid); caching.SetProcess(request.Guid, "生成站点页面..."); await _createManager.CreateByAllAsync(site.Id); caching.SetProcess(request.Guid, "清除系统缓存..."); _cacheManager.Clear(); } return(new IntResult { Value = siteId }); }