Exemplo n.º 1
0
        private void BrotliCompress(string source, string destination)
        {
            using (var input = File.Open(source, FileMode.Open, FileAccess.Read, FileShare.Read))
                using (var output = File.Create(destination))
                    using (var bs = new BrotliSharpLib.BrotliStream(output, CompressionMode.Compress))
                    {
                        // By default, BrotliSharpLib uses a quality value of 1 and window size of 22 if the methods are not called.
                        bs.SetQuality(BrotliCompressionQuality);
                        /** bs.SetWindow(windowSize); **/
                        /** bs.SetCustomDictionary(customDict); **/
                        input.CopyTo(bs);

                        /* IMPORTANT: Only use the destination stream after closing/disposing the BrotliStream
                         * as the BrotliStream must be closed in order to signal that no more blocks are being written
                         * for the final block to be flushed out
                         */
                        bs.Dispose();
                    }
        }
Exemplo n.º 2
0
        public override void OnActionExecuted(ActionExecutedContext context)
        {
            if (context.Result is ViewResult viewResult)
            {
                string htmlToCache = viewResult.ToHtml(HttpContext);
                bool   isMobile    = HttpContext.IsMobileBrowser();
                int    statusCode  = HttpContext.Response.StatusCode;
                if (statusCode == StatusCodes.Status200OK)
                {
                    // do not cache dynamically calculated data - as real exam with random questions
                    if (!HttpContext.Request.Path.ToString().Contains("test", StringComparison.InvariantCultureIgnoreCase))
                    {
                        cache.Set <string>(HttpContext.Request.Path.ToString() + "_IsMobile_" + isMobile.ToString(), htmlToCache);

                        // convert string to stream
                        byte[] byteArray = Encoding.ASCII.GetBytes(htmlToCache);

                        using (MemoryStream fs = new MemoryStream(byteArray))
                            using (var ms = new MemoryStream())
                            {
                                using (var bs = new BrotliSharpLib.BrotliStream(ms, System.IO.Compression.CompressionMode.Compress))
                                {
                                    bs.SetQuality(11);
                                    fs.Position = 0;
                                    fs.CopyTo(bs);

                                    bs.Dispose();
                                    byte[] compressed = ms.ToArray();

                                    cache.Set <byte[]>(HttpContext.Request.Path.ToString() + "_IsMobile_" + isMobile.ToString() + "compressedBr", compressed);
                                }
                            }
                    }
                }
            }
            base.OnActionExecuted(context);
        }