Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. public class MinifyHtmlFilterAttribute : ActionFilterAttribute
  2. {
  3. public override void OnResultExecuted(ResultExecutedContext filterContext)
  4. {
  5. HttpContextBase context = filterContext.HttpContext;
  6. HttpRequestBase request = context.Request;
  7. HttpResponseBase response = context.Response;
  8. Encoding encoding = response.ContentEncoding;
  9. string mediaType = response.ContentType;
  10. string currentUrl = request.RawUrl;
  11. var minificationManager = HtmlMinificationManager.Current;
  12.  
  13. if (response.Filter != null
  14. && response.StatusCode == 200
  15. && minificationManager.IsSupportedMediaType(mediaType) //text/html
  16. && minificationManager.IsProcessablePage(currentUrl))
  17. {
  18. response.Filter = new HtmlMinificationFilterStream(response, minificationManager, currentUrl, encoding, mediaType);
  19. }
  20. }
  21. }
  22.  
  23. public class HtmlMinificationFilterStream : Stream
  24. {
  25. private readonly HttpResponseBase _response;
  26. private readonly Stream _stream;
  27. private readonly MemoryStream _cacheStream = new MemoryStream();
  28.  
  29. private readonly IMarkupMinificationManager _minificationManager;
  30.  
  31. private readonly string _currentUrl;
  32. private readonly Encoding _encoding;
  33. private readonly string _mediaType;
  34. private int _chunkCount = 0;
  35.  
  36. public override bool CanRead
  37. {
  38. get { return true; }
  39. }
  40.  
  41. public override bool CanSeek
  42. {
  43. get { return true; }
  44. }
  45.  
  46. public override bool CanWrite
  47. {
  48. get { return true; }
  49. }
  50.  
  51. public override long Length
  52. {
  53. get { return 0; }
  54. }
  55.  
  56. public override long Position
  57. {
  58. get;
  59. set;
  60. }
  61.  
  62. public HtmlMinificationFilterStream(HttpResponseBase response,
  63. IMarkupMinificationManager minificationManager,
  64. string currentUrl,
  65. Encoding encoding,
  66. string mediaType)
  67. {
  68. _response = response;
  69. _stream = response.Filter;
  70. _minificationManager = minificationManager;
  71. _currentUrl = currentUrl;
  72. _encoding = encoding;
  73. _mediaType = mediaType;
  74. }
  75.  
  76. public override int Read(byte[] buffer, int offset, int count)
  77. {
  78. return _stream.Read(buffer, offset, count);
  79. }
  80.  
  81. public override long Seek(long offset, SeekOrigin origin)
  82. {
  83. return _stream.Seek(offset, origin);
  84. }
  85.  
  86. public override void SetLength(long value)
  87. {
  88. _stream.SetLength(value);
  89. }
  90.  
  91. public override void Write(byte[] buffer, int offset, int count)
  92. {
  93. _cacheStream.Write(buffer, 0, count);
  94. _chunkCount++;
  95. }
  96.  
  97. public override void Flush()
  98. {
  99. _stream.Flush();
  100. }
  101.  
  102. public override void Close()
  103. {
  104. byte[] cacheBytes = _cacheStream.ToArray();
  105. int cacheSize = cacheBytes.Length;
  106. string content = _encoding.GetString(cacheBytes);
  107. var log = $" | Chunks: {_chunkCount} | Url: {_currentUrl} | Encoding: {_encoding} | MediaType: {_mediaType} | Content: {content}";
  108.  
  109. IMarkupMinifier minifier = _minificationManager.CreateMinifier();
  110. MarkupMinificationResult minificationResult = minifier.Minify(content, _currentUrl, _encoding, false);
  111. bool isMinified = false;
  112. if (minificationResult.Errors.Count == 0)
  113. {
  114. ExceptionHandler.LogException("MINIFICATION SUCCESS" + log, System.Diagnostics.EventLogEntryType.Warning);
  115. using (var writer = new StreamWriter(_stream, _encoding))
  116. {
  117. writer.Write(minificationResult.MinifiedContent);
  118. }
  119.  
  120. isMinified = true;
  121. }
  122. else
  123. {
  124. foreach (var error in minificationResult.Errors)
  125. {
  126. ExceptionHandler.LogException("Minification Error" + log + " | " + error.SourceFragment, System.Diagnostics.EventLogEntryType.Warning);
  127. }
  128. }
  129.  
  130. if (!isMinified)
  131. {
  132. _cacheStream.Seek(0, SeekOrigin.Begin);
  133. _cacheStream.CopyTo(_stream);
  134. }
  135.  
  136. _cacheStream.SetLength(0);
  137. _stream.Close();
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement