public static string ReadText(string filePath, ECharset charset) { var sr = new StreamReader(filePath, ECharsetUtils.GetEncoding(charset)); var text = sr.ReadToEnd(); sr.Close(); return(text); }
public static void WriteText(string filePath, ECharset charset, string content) { DirectoryUtils.CreateDirectoryIfNotExists(DirectoryUtils.GetDirectoryPath(filePath)); StreamWriter sw = new StreamWriter(filePath, false, ECharsetUtils.GetEncoding(charset)); sw.Write(content); sw.Flush(); sw.Close(); }
public static void AppendText(string filePath, ECharset charset, string content) { DirectoryUtils.CreateDirectoryIfNotExists(DirectoryUtils.GetDirectoryPath(filePath)); using (FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write)) { using (StreamWriter sw = new StreamWriter(fs, ECharsetUtils.GetEncoding(charset))) { sw.Write(content); } } }
public static string UrlEncode(string urlString, ECharset charset) { if (urlString == null || urlString == "$4") { return(string.Empty); } var newValue = urlString.Replace("\"", "'"); newValue = HttpUtility.UrlEncode(newValue, ECharsetUtils.GetEncoding(charset)); newValue = newValue.Replace("%2f", "/"); return(newValue); }
public static string ReadText(string filePath, ECharset charset) { if (IsFileExists(filePath)) { StreamReader sr = new StreamReader(filePath, ECharsetUtils.GetEncoding(charset)); string text = sr.ReadToEnd(); sr.Close(); return(text); } else { return(string.Empty); } }
//private string CreateIncludeFile(string virtualUrl, bool isCreateIfExists) //{ // var templateInfo = new TemplateInfo(0, SiteId, string.Empty, TemplateType.FileTemplate, string.Empty, string.Empty, string.Empty, ECharsetUtils.GetEnumType(SiteInfo.Additional.Charset), false); // var pageInfo = new PageInfo(SiteId, 0, SiteInfo, templateInfo, null); // var contextInfo = new ContextInfo(pageInfo); // var parsedVirtualUrl = virtualUrl.Substring(0, virtualUrl.LastIndexOf('.')) + "_parsed" + virtualUrl.Substring(virtualUrl.LastIndexOf('.')); // var filePath = PathUtility.MapPath(SiteInfo, parsedVirtualUrl); // if (!isCreateIfExists && FileUtils.IsFileExists(filePath)) return parsedVirtualUrl; // var contentBuilder = new StringBuilder(StlCacheManager.FileContent.GetIncludeContent(SiteInfo, virtualUrl, pageInfo.TemplateInfo.Charset)); // StlParserManager.ParseTemplateContent(contentBuilder, pageInfo, contextInfo); // var pageAfterBodyScripts = StlParserManager.GetPageInfoScript(pageInfo, true); // var pageBeforeBodyScripts = StlParserManager.GetPageInfoScript(pageInfo, false); // contentBuilder.Insert(0, pageBeforeBodyScripts); // contentBuilder.Append(pageAfterBodyScripts); // GenerateFile(filePath, pageInfo.TemplateInfo.Charset, contentBuilder); // return parsedVirtualUrl; //} /// <summary> /// 在操作系统中创建文件,如果文件存在,重新创建此文件 /// </summary> private static async Task GenerateFileAsync(string filePath, ECharset charset, StringBuilder contentBuilder) { if (string.IsNullOrEmpty(filePath)) { return; } try { await FileUtils.WriteTextAsync(filePath, ECharsetUtils.GetEncoding(charset), contentBuilder.ToString()); } catch { FileUtils.RemoveReadOnlyAndHiddenIfExists(filePath); await FileUtils.WriteTextAsync(filePath, ECharsetUtils.GetEncoding(charset), contentBuilder.ToString()); } }
// 获取指定网页的HTML代码 public static string GetRemoteFileSource(string Url, ECharset charset, string cookieString) { try { var uri = new Uri(PageUtils.AddProtocolToUrl(Url.Trim())); var hwReq = (HttpWebRequest)WebRequest.Create(uri); if (!string.IsNullOrEmpty(cookieString)) { hwReq.Headers.Add("Cookie", cookieString); } var hwRes = (HttpWebResponse)hwReq.GetResponse(); hwReq.Method = "Get"; //hwReq.ContentType = "text/html"; hwReq.KeepAlive = false; var reader = new System.IO.StreamReader(hwRes.GetResponseStream(), ECharsetUtils.GetEncoding(charset)); return(reader.ReadToEnd()); } catch { throw new Exception($"页面地址“{Url}”无法访问!"); } }
public static string UrlDecode(string urlString, ECharset charset) { return(HttpUtility.UrlDecode(urlString, ECharsetUtils.GetEncoding(charset))); }
public static void WriteText(string filePath, ECharset charset, string content) { WriteText(filePath, ECharsetUtils.GetEncoding(charset), content); }
private static string ParseImpl(PageInfo pageInfo, ContextInfo contextInfo, string version, string title, string description, string scopeTypeString, string groupChannel, string groupChannelNot, string groupContent, string groupContentNot, string tags, string channelIndex, string channelName, int totalNum, int startNum, string orderByString, bool isTop, bool isTopExists, bool isRecommend, bool isRecommendExists, bool isHot, bool isHotExists, bool isColor, bool isColorExists) { var parsedContent = string.Empty; var feed = new RssFeed(); feed.Encoding = ECharsetUtils.GetEncoding(pageInfo.TemplateInfo.Charset); if (string.IsNullOrEmpty(version)) { feed.Version = RssVersion.RSS20; } else { feed.Version = (RssVersion)TranslateUtils.ToEnum(typeof(RssVersion), version, RssVersion.RSS20); } var channel = new RssChannel(); channel.Title = title; channel.Description = description; EScopeType scopeType; if (!string.IsNullOrEmpty(scopeTypeString)) { scopeType = EScopeTypeUtils.GetEnumType(scopeTypeString); } else { scopeType = EScopeType.All; } var channelID = StlCacheManager.NodeId.GetNodeIdByChannelIdOrChannelIndexOrChannelName(pageInfo.PublishmentSystemId, contextInfo.ChannelID, channelIndex, channelName); var nodeInfo = NodeManager.GetNodeInfo(pageInfo.PublishmentSystemId, channelID); if (string.IsNullOrEmpty(channel.Title)) { channel.Title = nodeInfo.NodeName; } if (string.IsNullOrEmpty(channel.Description)) { channel.Description = nodeInfo.Content; if (string.IsNullOrEmpty(channel.Description)) { channel.Description = nodeInfo.NodeName; } else { channel.Description = StringUtils.MaxLengthText(channel.Description, 200); } } channel.Link = new Uri(PageUtils.AddProtocolToUrl(PageUtility.GetChannelUrl(pageInfo.PublishmentSystemInfo, nodeInfo))); var dataSource = StlDataUtility.GetContentsDataSource(pageInfo.PublishmentSystemInfo, channelID, 0, groupContent, groupContentNot, tags, false, false, false, false, false, false, false, false, startNum, totalNum, orderByString, isTopExists, isTop, isRecommendExists, isRecommend, isHotExists, isHot, isColorExists, isColor, string.Empty, scopeType, groupChannel, groupChannelNot, null); if (dataSource != null) { foreach (var dataItem in dataSource) { var item = new RssItem(); var contentInfo = new BackgroundContentInfo(dataItem); item.Title = StringUtils.Replace("&", contentInfo.Title, "&"); item.Description = contentInfo.Summary; if (string.IsNullOrEmpty(item.Description)) { item.Description = StringUtils.StripTags(contentInfo.Content); if (string.IsNullOrEmpty(item.Description)) { item.Description = contentInfo.Title; } else { item.Description = StringUtils.MaxLengthText(item.Description, 200); } } item.Description = StringUtils.Replace("&", item.Description, "&"); item.PubDate = contentInfo.AddDate; item.Link = new Uri(PageUtils.AddProtocolToUrl(PageUtility.GetContentUrl(pageInfo.PublishmentSystemInfo, contentInfo))); channel.Items.Add(item); } } feed.Channels.Add(channel); var builder = new StringBuilder(); var textWriter = new EncodedStringWriter(builder, ECharsetUtils.GetEncoding(pageInfo.TemplateInfo.Charset)); feed.Write(textWriter); parsedContent = builder.ToString(); return(parsedContent); }
private static string ParseImpl(PageInfo pageInfo, ContextInfo contextInfo, string title, string description, string scopeTypeString, string groupChannel, string groupChannelNot, string groupContent, string groupContentNot, string tags, string channelIndex, string channelName, int totalNum, int startNum, string orderByString, bool isTop, bool isTopExists, bool isRecommend, bool isRecommendExists, bool isHot, bool isHotExists, bool isColor, bool isColorExists) { var feed = new RssFeed { Encoding = ECharsetUtils.GetEncoding(pageInfo.TemplateInfo.Charset), Version = RssVersion.RSS20 }; var channel = new RssChannel { Title = title, Description = description }; var scopeType = !string.IsNullOrEmpty(scopeTypeString) ? EScopeTypeUtils.GetEnumType(scopeTypeString) : EScopeType.All; var channelId = StlDataUtility.GetChannelIdByChannelIdOrChannelIndexOrChannelName(pageInfo.SiteId, contextInfo.ChannelId, channelIndex, channelName); var nodeInfo = ChannelManager.GetChannelInfo(pageInfo.SiteId, channelId); if (string.IsNullOrEmpty(channel.Title)) { channel.Title = nodeInfo.ChannelName; } if (string.IsNullOrEmpty(channel.Description)) { channel.Description = nodeInfo.Content; channel.Description = string.IsNullOrEmpty(channel.Description) ? nodeInfo.ChannelName : StringUtils.MaxLengthText(channel.Description, 200); } channel.Link = new Uri(PageUtils.AddProtocolToUrl(PageUtility.GetChannelUrl(pageInfo.SiteInfo, nodeInfo, pageInfo.IsLocal))); var minContentInfoList = StlDataUtility.GetMinContentInfoList(pageInfo.SiteInfo, channelId, 0, groupContent, groupContentNot, tags, false, false, false, false, false, false, false, startNum, totalNum, orderByString, isTopExists, isTop, isRecommendExists, isRecommend, isHotExists, isHot, isColorExists, isColor, string.Empty, scopeType, groupChannel, groupChannelNot, null); if (minContentInfoList != null) { foreach (var minContentInfo in minContentInfoList) { var item = new RssItem(); var contentInfo = ContentManager.GetContentInfo(pageInfo.SiteInfo, minContentInfo.ChannelId, minContentInfo.Id); item.Title = StringUtils.Replace("&", contentInfo.Title, "&"); item.Description = contentInfo.Summary; if (string.IsNullOrEmpty(item.Description)) { item.Description = StringUtils.StripTags(contentInfo.Content); item.Description = string.IsNullOrEmpty(item.Description) ? contentInfo.Title : StringUtils.MaxLengthText(item.Description, 200); } item.Description = StringUtils.Replace("&", item.Description, "&"); item.PubDate = contentInfo.AddDate.Value; item.Link = new Uri(PageUtils.AddProtocolToUrl(PageUtility.GetContentUrl(pageInfo.SiteInfo, contentInfo, false))); channel.Items.Add(item); } } feed.Channels.Add(channel); var builder = new StringBuilder(); var textWriter = new EncodedStringWriter(builder, ECharsetUtils.GetEncoding(pageInfo.TemplateInfo.Charset)); feed.Write(textWriter); return(builder.ToString()); }