GenerateStaticFileHelper.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. namespace Common
  7. {
  8. /// <summary>
  9. /// 生成Html静态文件
  10. /// </summary>
  11. public sealed class GenerateStaticFileHelper : IDisposable
  12. {
  13. #region 配置
  14. /// <summary>
  15. /// 生成静态页面的绝对路径,带文件名
  16. /// </summary>
  17. private readonly string _staticFilePath;
  18. /// <summary>
  19. /// 模板文件字符串
  20. /// </summary>
  21. private readonly string _template;
  22. /// <summary>
  23. /// 页面编码,默认UTF-8
  24. /// </summary>
  25. private readonly string _encode;
  26. /// <summary>
  27. /// 写入页面数据流
  28. /// </summary>
  29. private static StreamWriter _sw = null;
  30. /// <summary>
  31. /// 读取页面数据流
  32. /// </summary>
  33. private static StreamReader _sr = null;
  34. #endregion
  35. static GenerateStaticFileHelper()
  36. {
  37. }
  38. /// <summary>
  39. /// 构造
  40. /// </summary>
  41. /// <param name="templatePath">模板文件绝对路径,带文件名</param>
  42. /// <param name="staticFilePath">生成静态页面的相对路径,带文件名</param>
  43. /// <param name="encode">页面编码,默认UTF-8</param>
  44. public GenerateStaticFileHelper(string templatePath, string staticFilePath, string encode = "UTF-8")
  45. {
  46. //生成存放html的文件夹
  47. Utils.CreateDirectory(Utils.GetFilePathNoExtensionAndFileName(staticFilePath));
  48. this._staticFilePath = staticFilePath;
  49. this._encode = encode;
  50. //模板文件内容
  51. string cacheTemplateString = ReadTemplateFile(templatePath, this._encode);
  52. this._template = cacheTemplateString;
  53. }
  54. /// <summary>
  55. /// 生成静态网页
  56. /// </summary>
  57. /// <param name="source">特征字符串-待填充的数据</param>
  58. public bool CreateStaticHtmlFile(Dictionary<string, string> source)
  59. {
  60. if (string.IsNullOrEmpty(_template)) return false;
  61. //替换完成后页面内容
  62. string res = ReplaceTemplateTag(source, _template);
  63. //生成静态页面
  64. bool isSucc = CreateStaticHtmlFile(_staticFilePath, this._encode, res);
  65. return isSucc;
  66. }
  67. /// <summary>
  68. /// 读取文件字节流
  69. /// </summary>
  70. /// <param name="templatePath">模板相对路径</param>
  71. /// <param name="encode">页面编码</param>
  72. /// <returns>template静态模板的html字符串</returns>
  73. private static string ReadTemplateFile(string templatePath, string encode)
  74. {
  75. try
  76. {
  77. Encoding code = Encoding.GetEncoding(encode);
  78. if (Utils.IsExistFile(templatePath))
  79. {
  80. //读取
  81. using (_sr = new StreamReader(templatePath, code))
  82. {
  83. return _sr.ReadToEnd();
  84. }
  85. }
  86. }
  87. catch (Exception ex)
  88. {
  89. return "";
  90. }
  91. finally
  92. {
  93. if (_sr != null) _sr.Close();
  94. }
  95. return "";
  96. }
  97. /// <summary>
  98. /// 替换特征字符串
  99. /// </summary>
  100. /// <param name="source">特征字符串-待填充的数据</param>
  101. /// <param name="streamstr">template静态模板的html字符串</param>
  102. /// <returns></returns>
  103. private static string ReplaceTemplateTag(Dictionary<string, string> source, string streamstr)
  104. {
  105. if (source != null && source.Count > 0)
  106. {
  107. streamstr = source.Aggregate(streamstr, (current, data) => current.Replace(data.Key, data.Value));
  108. }
  109. return streamstr;
  110. }
  111. /// <summary>
  112. /// 生成静态页面
  113. /// </summary>
  114. /// <param name="staticFilePath">生成静态页面的相对路径</param>
  115. /// <param name="encode">页面编码</param>
  116. /// <param name="outhtmlstr">输出的html字符串</param>
  117. /// <returns></returns>
  118. private static bool CreateStaticHtmlFile(string staticFilePath, string encode, string outhtmlstr)
  119. {
  120. try
  121. {
  122. Encoding code = Encoding.GetEncoding(encode);
  123. //如果存在则删除原来的文件
  124. Utils.DeleteFile(staticFilePath);
  125. using (_sw = new StreamWriter(staticFilePath, false, code))
  126. {
  127. _sw.Write(outhtmlstr);
  128. _sw.Flush();
  129. }
  130. return true;
  131. }
  132. catch (Exception ex)
  133. {
  134. return false;
  135. }
  136. finally
  137. {
  138. if (_sw != null) _sw.Close();
  139. }
  140. }
  141. /// <summary>
  142. /// 生成静态页面的文件名
  143. /// </summary>
  144. /// <param name="filename">文件名</param>
  145. /// <param name="extension">文件扩展名</param>
  146. /// <returns></returns>
  147. private static string CreateNewFilename(string filename = "", string extension = "htm")
  148. {
  149. long tick = DateTime.Now.Ticks;
  150. string time = DateTime.Now.ToString("yyyyMMddHHmmss");
  151. Random ra = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
  152. string rnd = ra.Next(1, 999).ToString();
  153. string fileName = string.Format("{0}-{1}-{2}-{3}", filename, time, rnd, extension);
  154. return fileName;
  155. }
  156. /// <summary>
  157. /// 释放资源
  158. /// </summary>
  159. public void Dispose()
  160. {
  161. _sw.Dispose();
  162. _sr.Dispose();
  163. }
  164. }
  165. }