123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Web.Mvc;
- using System.Web;
- using System.IO;
- namespace Bowin.Common.Mvc
- {
- public class StaticFileWriteFilterAttribute : ActionFilterAttribute
- {
- public override void OnResultExecuted(ResultExecutedContext filterContext)
- {
- //filterContext.HttpContext.Response.Write("OnResultExecuted</br>");
- if (filterContext.HttpContext.Response.StatusCode == 200)
- {
- filterContext.HttpContext.Response.Filter = new StaticFileWriteResponseFilterWrapper(filterContext.HttpContext.Response.Filter, filterContext);
- }
- // filterContext.HttpContext.Response.Charset = "utf8";
- base.OnResultExecuted(filterContext);
- }
- class StaticFileWriteResponseFilterWrapper : System.IO.Stream
- {
- private Stream inner;
- private FileStream writer;
- private ControllerContext context;
- private int expireSconds;
- private bool filter;
- private string tempPath, path;
- public StaticFileWriteResponseFilterWrapper(System.IO.Stream s, ControllerContext context, int expireSeconds = 600)
- {
- this.filter = false;
- this.inner = s;
- this.context = context;
- this.expireSconds = expireSeconds;
- this.EnsureStaticFile();
- }
- void EnsureStaticFile()
- {
- this.path = this.context.HttpContext.Server.MapPath(HttpContext.Current.Request.Path);
- if (!Path.HasExtension(path))
- {
- return;
- }
- if (!".html".Equals(Path.GetExtension(HttpContext.Current.Request.Path)))
- {
- return;
- }
- if (File.Exists(path))
- {
- var delay = DateTime.UtcNow - File.GetCreationTimeUtc(path);
- if (delay.TotalSeconds <= this.expireSconds)
- {
- return;
- }
- File.Delete(path);
- }
- else
- {
- var dir = Path.GetDirectoryName(path);
- if (!Directory.Exists(dir))
- {
- try
- {
- Directory.CreateDirectory(Path.GetDirectoryName(path));
- }
- catch
- { }
- }
- }
- this.filter = true;
- this.tempPath = this.path + "_" + DateTime.Now.Ticks;
- try
- {
- writer = new FileStream(tempPath, FileMode.Create, FileAccess.Write);
- }
- catch
- {
- this.filter = false;
- }
- }
- public override bool CanRead
- {
- get { return inner.CanRead; }
- }
- public override bool CanSeek
- {
- get { return inner.CanSeek; }
- }
- public override bool CanWrite
- {
- get { return inner.CanWrite; }
- }
- public override void Flush()
- {
- inner.Flush();
- }
- public override long Length
- {
- get { return inner.Length; }
- }
- public override long Position
- {
- get
- {
- return inner.Position;
- }
- set
- {
- inner.Position = value;
- }
- }
- public override int Read(byte[] buffer, int offset, int count)
- {
- return inner.Read(buffer, offset, count);
- }
- public override long Seek(long offset, System.IO.SeekOrigin origin)
- {
- return inner.Seek(offset, origin);
- }
- public override void SetLength(long value)
- {
- inner.SetLength(value);
- }
- public override void Write(byte[] buffer, int offset, int count)
- {
- try
- {
- inner.Write(buffer, offset, count);
- }
- catch (Exception ex)
- {
- }
- try
- {
- this.writer.Write(buffer, offset, count);
- }
- catch (Exception ex)
- {
- }
- }
- protected override void Dispose(bool disposing)
- {
- if (this.filter)
- {
- try
- {
- if (this.writer != null)
- {
- this.writer.Dispose();
- this.writer = null;
- }
- File.Delete(this.path);
- File.Move(this.tempPath, this.path);
- #region 生成文件日志
- #endregion
- }
- catch
- { }
- }
- base.Dispose(disposing);
- }
- }
- }
- }
|