private string SaveImages(string content) { var originalImageSrcs = RegexUtils.GetOriginalImageSrcs(content); foreach (var originalImageSrc in originalImageSrcs) { if (!PageUtils.IsProtocolUrl(originalImageSrc)) { continue; } var extName = "png"; if (StringUtils.Contains(originalImageSrc, "wx_fmt=")) { extName = originalImageSrc.Substring(originalImageSrc.LastIndexOf("=", StringComparison.Ordinal) + 1); } var materialFileName = PathUtils.GetMaterialFileNameByExtName(extName); var virtualDirectoryPath = PathUtils.GetMaterialVirtualDirectoryPath(UploadType.Image); var directoryPath = PathUtils.Combine(_settingsManager.WebRootPath, virtualDirectoryPath); var filePath = PathUtils.Combine(directoryPath, materialFileName); WebClientUtils.Download(originalImageSrc, filePath); var imageUrl = PageUtils.Combine(virtualDirectoryPath, materialFileName); content = content.Replace(" data-src=", "src="); content = content.Replace(originalImageSrc, imageUrl); } return(content); }
public async Task <ActionResult <IntResult> > Submit([FromBody] SubmitRequest request) { if (!await _authManager.HasAppPermissionsAsync(MenuUtils.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(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.LocalDirectoryName, 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.CloudThemeName}.zip"); FileUtils.DeleteFileIfExists(filePath); var downloadUrl = CloudUtils.Dl.GetThemesDownloadUrl(request.CloudThemeUserName, request.CloudThemeName); WebClientUtils.Download(downloadUrl, filePath); caching.SetProcess(request.Guid, "模板压缩包下载成功,开始解压缩,可能需要几分钟,请耐心等待..."); var siteTemplateDir = $"T_{request.CloudThemeName}"; 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 }); }
public async Task PullMaterialAsync(string accessTokenOrAppId, MaterialType materialType, int groupId) { var count = await MediaApi.GetMediaCountAsync(accessTokenOrAppId); if (materialType == MaterialType.Message) { if (count.news_count > 0) { var newsList = await MediaApi.GetNewsMediaListAsync(accessTokenOrAppId, 0, count.news_count); newsList.item.Reverse(); foreach (var message in newsList.item) { if (await _materialMessageRepository.IsExistsAsync(message.media_id)) { continue; } //var news = await MediaApi.GetForeverNewsAsync(accessTokenOrAppId, message.media_id); var messageItems = new List <MaterialMessageItem>(); foreach (var item in message.content.news_item) { var imageUrl = string.Empty; if (!string.IsNullOrEmpty(item.thumb_media_id) && !string.IsNullOrEmpty(item.thumb_url)) { await using var ms = new MemoryStream(); await MediaApi.GetForeverMediaAsync(accessTokenOrAppId, item.thumb_media_id, ms); ms.Seek(0, SeekOrigin.Begin); var extName = "png"; if (StringUtils.Contains(item.thumb_url, "wx_fmt=")) { extName = item.thumb_url.Substring(item.thumb_url.LastIndexOf("=", StringComparison.Ordinal) + 1); } var materialFileName = PathUtils.GetMaterialFileNameByExtName(extName); var virtualDirectoryPath = PathUtils.GetMaterialVirtualDirectoryPath(UploadType.Image); var directoryPath = PathUtils.Combine(_settingsManager.WebRootPath, virtualDirectoryPath); var filePath = PathUtils.Combine(directoryPath, materialFileName); await FileUtils.WriteStreamAsync(filePath, ms); imageUrl = PageUtils.Combine(virtualDirectoryPath, materialFileName); } else if (!string.IsNullOrEmpty(item.thumb_url)) { var extName = "png"; if (StringUtils.Contains(item.thumb_url, "wx_fmt=")) { extName = item.thumb_url.Substring(item.thumb_url.LastIndexOf("=", StringComparison.Ordinal) + 1); } var materialFileName = PathUtils.GetMaterialFileNameByExtName(extName); var virtualDirectoryPath = PathUtils.GetMaterialVirtualDirectoryPath(UploadType.Image); var directoryPath = PathUtils.Combine(_settingsManager.WebRootPath, virtualDirectoryPath); var filePath = PathUtils.Combine(directoryPath, materialFileName); WebClientUtils.Download(item.thumb_url, filePath); imageUrl = PageUtils.Combine(virtualDirectoryPath, materialFileName); } var commentType = CommentType.Block; if (item.need_open_comment == 1) { commentType = item.only_fans_can_comment == 1 ? CommentType.OnlyFans : CommentType.Everyone; } messageItems.Add(new MaterialMessageItem { MessageId = 0, MaterialType = MaterialType.Article, MaterialId = 0, Taxis = 0, ThumbMediaId = item.thumb_media_id, Author = item.author, Title = item.title, ContentSourceUrl = item.content_source_url, Content = SaveImages(item.content), Digest = item.digest, ShowCoverPic = item.show_cover_pic == "1", ThumbUrl = imageUrl, Url = item.url, CommentType = commentType }); } await _materialMessageRepository.InsertAsync(groupId, message.media_id, messageItems); } } } else if (materialType == MaterialType.Image) { if (count.image_count > 0) { var list = await MediaApi.GetOthersMediaListAsync(accessTokenOrAppId, UploadMediaFileType.image, 0, count.image_count); foreach (var image in list.item) { if (await _materialImageRepository.IsExistsAsync(image.media_id)) { continue; } await using var ms = new MemoryStream(); await MediaApi.GetForeverMediaAsync(accessTokenOrAppId, image.media_id, ms); ms.Seek(0, SeekOrigin.Begin); var extName = image.url.Substring(image.url.LastIndexOf("=", StringComparison.Ordinal) + 1); var materialFileName = PathUtils.GetMaterialFileNameByExtName(extName); var virtualDirectoryPath = PathUtils.GetMaterialVirtualDirectoryPath(UploadType.Image); var directoryPath = PathUtils.Combine(_settingsManager.WebRootPath, virtualDirectoryPath); var filePath = PathUtils.Combine(directoryPath, materialFileName); await FileUtils.WriteStreamAsync(filePath, ms); var material = new MaterialImage { GroupId = groupId, Title = image.name, Url = PageUtils.Combine(virtualDirectoryPath, materialFileName), MediaId = image.media_id }; await _materialImageRepository.InsertAsync(material); } } } else if (materialType == MaterialType.Audio) { if (count.voice_count > 0) { var list = await MediaApi.GetOthersMediaListAsync(accessTokenOrAppId, UploadMediaFileType.voice, 0, count.voice_count); foreach (var voice in list.item) { if (await _materialAudioRepository.IsExistsAsync(voice.media_id)) { continue; } await using var ms = new MemoryStream(); await MediaApi.GetForeverMediaAsync(accessTokenOrAppId, voice.media_id, ms); ms.Seek(0, SeekOrigin.Begin); var extName = voice.url.Substring(voice.url.LastIndexOf("=", StringComparison.Ordinal) + 1); var materialFileName = PathUtils.GetMaterialFileNameByExtName(extName); var virtualDirectoryPath = PathUtils.GetMaterialVirtualDirectoryPath(UploadType.Audio); var directoryPath = PathUtils.Combine(_settingsManager.WebRootPath, virtualDirectoryPath); var filePath = PathUtils.Combine(directoryPath, materialFileName); await FileUtils.WriteStreamAsync(filePath, ms); var audio = new MaterialAudio { GroupId = groupId, Title = voice.name, FileType = extName.ToUpper().Replace(".", string.Empty), Url = PageUtils.Combine(virtualDirectoryPath, materialFileName), MediaId = voice.media_id }; await _materialAudioRepository.InsertAsync(audio); } } } else if (materialType == MaterialType.Video) { if (count.video_count > 0) { var list = await MediaApi.GetOthersMediaListAsync(accessTokenOrAppId, UploadMediaFileType.video, 0, count.video_count); foreach (var video in list.item) { if (await _materialVideoRepository.IsExistsAsync(video.media_id)) { continue; } await using var ms = new MemoryStream(); await MediaApi.GetForeverMediaAsync(accessTokenOrAppId, video.media_id, ms); ms.Seek(0, SeekOrigin.Begin); var extName = "mp4"; if (!string.IsNullOrEmpty(video.url)) { extName = video.url.Substring(video.url.LastIndexOf("=", StringComparison.Ordinal) + 1); } var materialFileName = PathUtils.GetMaterialFileNameByExtName(extName); var virtualDirectoryPath = PathUtils.GetMaterialVirtualDirectoryPath(UploadType.Video); var directoryPath = PathUtils.Combine(_settingsManager.WebRootPath, virtualDirectoryPath); var filePath = PathUtils.Combine(directoryPath, materialFileName); await FileUtils.WriteStreamAsync(filePath, ms); var material = new MaterialVideo { GroupId = groupId, Title = video.name, FileType = extName.ToUpper().Replace(".", string.Empty), Url = PageUtils.Combine(virtualDirectoryPath, materialFileName), MediaId = video.media_id }; await _materialVideoRepository.InsertAsync(material); } } } }