StaticFileWriteFilterAttribute.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web.Mvc;
  6. using System.Web;
  7. using System.IO;
  8. namespace Bowin.Common.Mvc
  9. {
  10. public class StaticFileWriteFilterAttribute : ActionFilterAttribute
  11. {
  12. public override void OnResultExecuted(ResultExecutedContext filterContext)
  13. {
  14. //filterContext.HttpContext.Response.Write("OnResultExecuted</br>");
  15. if (filterContext.HttpContext.Response.StatusCode == 200)
  16. {
  17. filterContext.HttpContext.Response.Filter = new StaticFileWriteResponseFilterWrapper(filterContext.HttpContext.Response.Filter, filterContext);
  18. }
  19. // filterContext.HttpContext.Response.Charset = "utf8";
  20. base.OnResultExecuted(filterContext);
  21. }
  22. class StaticFileWriteResponseFilterWrapper : System.IO.Stream
  23. {
  24. private Stream inner;
  25. private FileStream writer;
  26. private ControllerContext context;
  27. private int expireSconds;
  28. private bool filter;
  29. private string tempPath, path;
  30. public StaticFileWriteResponseFilterWrapper(System.IO.Stream s, ControllerContext context, int expireSeconds = 600)
  31. {
  32. this.filter = false;
  33. this.inner = s;
  34. this.context = context;
  35. this.expireSconds = expireSeconds;
  36. this.EnsureStaticFile();
  37. }
  38. void EnsureStaticFile()
  39. {
  40. this.path = this.context.HttpContext.Server.MapPath(HttpContext.Current.Request.Path);
  41. if (!Path.HasExtension(path))
  42. {
  43. return;
  44. }
  45. if (!".html".Equals(Path.GetExtension(HttpContext.Current.Request.Path)))
  46. {
  47. return;
  48. }
  49. if (File.Exists(path))
  50. {
  51. var delay = DateTime.UtcNow - File.GetCreationTimeUtc(path);
  52. if (delay.TotalSeconds <= this.expireSconds)
  53. {
  54. return;
  55. }
  56. File.Delete(path);
  57. }
  58. else
  59. {
  60. var dir = Path.GetDirectoryName(path);
  61. if (!Directory.Exists(dir))
  62. {
  63. try
  64. {
  65. Directory.CreateDirectory(Path.GetDirectoryName(path));
  66. }
  67. catch
  68. { }
  69. }
  70. }
  71. this.filter = true;
  72. this.tempPath = this.path + "_" + DateTime.Now.Ticks;
  73. try
  74. {
  75. writer = new FileStream(tempPath, FileMode.Create, FileAccess.Write);
  76. }
  77. catch
  78. {
  79. this.filter = false;
  80. }
  81. }
  82. public override bool CanRead
  83. {
  84. get { return inner.CanRead; }
  85. }
  86. public override bool CanSeek
  87. {
  88. get { return inner.CanSeek; }
  89. }
  90. public override bool CanWrite
  91. {
  92. get { return inner.CanWrite; }
  93. }
  94. public override void Flush()
  95. {
  96. inner.Flush();
  97. }
  98. public override long Length
  99. {
  100. get { return inner.Length; }
  101. }
  102. public override long Position
  103. {
  104. get
  105. {
  106. return inner.Position;
  107. }
  108. set
  109. {
  110. inner.Position = value;
  111. }
  112. }
  113. public override int Read(byte[] buffer, int offset, int count)
  114. {
  115. return inner.Read(buffer, offset, count);
  116. }
  117. public override long Seek(long offset, System.IO.SeekOrigin origin)
  118. {
  119. return inner.Seek(offset, origin);
  120. }
  121. public override void SetLength(long value)
  122. {
  123. inner.SetLength(value);
  124. }
  125. public override void Write(byte[] buffer, int offset, int count)
  126. {
  127. try
  128. {
  129. inner.Write(buffer, offset, count);
  130. }
  131. catch (Exception ex)
  132. {
  133. }
  134. try
  135. {
  136. this.writer.Write(buffer, offset, count);
  137. }
  138. catch (Exception ex)
  139. {
  140. }
  141. }
  142. protected override void Dispose(bool disposing)
  143. {
  144. if (this.filter)
  145. {
  146. try
  147. {
  148. if (this.writer != null)
  149. {
  150. this.writer.Dispose();
  151. this.writer = null;
  152. }
  153. File.Delete(this.path);
  154. File.Move(this.tempPath, this.path);
  155. #region 生成文件日志
  156. #endregion
  157. }
  158. catch
  159. { }
  160. }
  161. base.Dispose(disposing);
  162. }
  163. }
  164. }
  165. }