Exemple #1
0
        private static void CompressContent(HttpApplication context)
        {
            if (context.Context.Items.Contains("AlreadyCompressed"))
            {
                return;
            }

            context.Context.Items.Add("AlreadyCompressed", true);

            HttpResponse response = context.Response;

            if (response.Filter == null)
            {
                return;
            }

            CompressionConfiguration settings = CompressionConfiguration.GetSection();

            string acceptedTypes = context.Request.Headers["Accept-Encoding"];

            if (acceptedTypes == null)
            {
                return;
            }

            string contentType = response.ContentType.Split(';')[0].Trim();

            if (!settings.IsContentTypeCompressed(contentType))
            {
                return;
            }

            string compressionScheme = settings.GetCompressionType(acceptedTypes);

            bool compressed = false;

            switch (compressionScheme)
            {
            case "deflate":
                context.Response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress,
                                                            CompressionLevel.BestCompression, false);
                compressed = true;
                break;

            case "gzip":
                context.Response.Filter = new GZipStream(response.Filter, CompressionMode.Compress,
                                                         CompressionLevel.BestCompression, false);
                compressed = true;
                break;
            }

            if (!compressed)
            {
                return;
            }

            response.AppendHeader("Content-Encoding", compressionScheme);
            response.AppendHeader("Vary", "Accept-Encoding");
        }
Exemple #2
0
 public ImageEncoder(ImageFormat format, EncryptionConfiguration encryptionConfiguration,
                     CompressionConfiguration compressionConfiguration, EmbeddedImage embeddedImage = null)
 {
     _encryptionConfiguration  = encryptionConfiguration;
     _compressionConfiguration = compressionConfiguration;
     _embeddedImage            = embeddedImage;
     _format = format;
 }
        private async Task CreateVersionCompatData(
            ImageFormat format,
            FileInfo inputZipFIle,
            EncryptionConfiguration encryption,
            CompressionConfiguration compression,
            EmbeddedImage embeddedImage)
        {
            var    inputFileName = Path.GetFileNameWithoutExtension(inputZipFIle.Name);
            string outputName    = inputFileName.Substring(0, Math.Min(inputFileName.Length, 10)) + "-" + Enum.GetName(typeof(ImageFormat), format);

            if (embeddedImage != null)
            {
                outputName += "-E";
            }

            if (encryption != null)
            {
                outputName += "-" + Enum.GetName(typeof(EncryptionType), encryption.Type).Substring(0, 3);
            }

            if (compression != null)
            {
                outputName += "-" + Enum.GetName(typeof(CompressionType), compression.Type).Substring(0, 3);
            }


            var tempInputDataDirectory = _ouputTempVersionDataDirectory.CreateSubdirectory(outputName + "_original_data");

            using (var zipFile = ZipFile.Read(inputZipFIle.FullName))
            {
                zipFile.ExtractAll(tempInputDataDirectory.FullName);
            }


            DirectoryInfo outputDirectory = _ouputTempVersionDataDirectory.CreateSubdirectory(outputName);
            var           encoder         = new ImageEncoder(format, encryption, compression, embeddedImage);

            using (Stream encodedImageFile = File.Create(Path.Combine(outputDirectory.FullName, inputFileName + "." + encoder.Extension)))
            {
                encoder.AddDirectory(tempInputDataDirectory);
                await encoder.SaveAsync(encodedImageFile, new EncodingConfiguration(
                                            password : "******",
                                            tempStorageProvider : new MemoryStorageProvider(),
                                            bufferSize : 4096,
                                            fileGroupSize : 1024 * 500));

                inputZipFIle.CopyTo(Path.Combine(outputDirectory.FullName, "Original_Data.zip"), true);
            }

            tempInputDataDirectory.Delete(true);
        }
        public async Task EncodingThenDecoding_ProducesEquivalentResults(
            ImageFormat format,
            FileInfo inputZipFile,
            EncryptionConfiguration encryption,
            CompressionConfiguration compression,
            EmbeddedImage embeddedImage,
            IDictionary <string, string> metadata)
        {
            DirectoryInfo inputDirectory  = null;
            DirectoryInfo outputDirectory = null;

            try
            {
                using (var storageStream = new MemoryStream())
                {
                    inputDirectory = _inputRootDirectory.CreateSubdirectory(Path.GetFileNameWithoutExtension(inputZipFile.Name));
                    using (var zipFile = ZipFile.Read(inputZipFile.FullName))
                    {
                        zipFile.ExtractAll(inputDirectory.FullName);
                    }

                    var encoder = new ImageEncoder(format, encryption, compression, embeddedImage);

                    encoder.Metadata = metadata;
                    encoder.AddDirectory(inputDirectory);
                    await encoder.SaveAsync(storageStream, ImageConfigurations.EncodingConfiguration);

                    storageStream.Position = 0;

                    var decoder = await ImageDecoder.LoadAsync(storageStream, ImageConfigurations.DecodingConfiguration);

                    outputDirectory = _ouputRootDirectory.CreateSubdirectory(inputDirectory.Name);
                    await decoder.DecodeAsync(outputDirectory);

                    AssertEx.AreEqualByJson(metadata, decoder.Metadata);
                    DirectoryAssert.AreEquivalent(inputDirectory, outputDirectory);
                }
            }
            finally
            {
                foreach (var directory in new[] { inputDirectory, outputDirectory })
                {
                    if (directory != null)
                    {
                        directory.Delete(true);
                    }
                }
            }
        }
Exemple #5
0
        public void LZ4()
        {
            var inputObj = new CompressionConfiguration()
            {
                AlgorithmName = CompressionAlgorithms.LZ4.ToString()
            };

            var stream = SerialiseToMemory(inputObj);

            stream.Seek(0, SeekOrigin.Begin);
            var outputObj = DeserialiseFromMemory <CompressionConfiguration>(stream);

            bool equal = inputObj.Equals(outputObj);

            Assert.IsTrue(equal);
        }
Exemple #6
0
        public void Bzip2()
        {
            var inputObj = new CompressionConfiguration()
            {
                AlgorithmName          = CompressionAlgorithms.Deflate.ToString(),
                AlgorithmConfiguration = Bzip2ConfigurationUtility.Write(9)
            };

            var stream = SerialiseToMemory(inputObj);

            stream.Seek(0, SeekOrigin.Begin);
            var outputObj = DeserialiseFromMemory <CompressionConfiguration>(stream);

            bool equal = inputObj.Equals(outputObj);

            Assert.IsTrue(equal);
        }
Exemple #7
0
        public async Task <HttpResponseMessage> Encode()
        {
            EncodingJob encodingJob       = null;
            string      badRequestMessage = null;

            try
            {
                encodingJob = await BuildEncodingJob(Request);
            }
            catch (BadRequestException exception)
            {
                badRequestMessage = exception.Message;
            }
            catch {}
            if (encodingJob == null)
            {
                return(new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    Content = new StringContent(badRequestMessage)
                });
            }

            EncryptionConfiguration  encryptionConfiguration  = null;
            CompressionConfiguration compressionConfiguration = null;

            if (encodingJob.HasPassword)
            {
                encryptionConfiguration = new EncryptionConfiguration(
                    encodingJob.EncryptionAlgorithm.Value,
                    TranscodingConfiguration.IterationCount);
            }

            if (encodingJob.CompressionAlgorithm.HasValue)
            {
                compressionConfiguration = new CompressionConfiguration(encodingJob.CompressionAlgorithm.Value,
                                                                        encodingJob.CompressionLevel);
            }

            var imageEncoder = new ImageEncoder(encodingJob.Format, encryptionConfiguration, compressionConfiguration, encodingJob.EmbeddedImage);

            imageEncoder.AddDirectories(encodingJob.Directories);

            imageEncoder.Metadata.Add("creation-date", DateTime.UtcNow.ToString("s", CultureInfo.InvariantCulture));

            var guid = SecureGuid();

            try
            {
                DirectoryInfo jobDirectory = CreateJobDirectory(guid);
                var           outputPath   = Path.Combine(jobDirectory.FullName, encodingJob.FileName + "." + imageEncoder.Extension);
                using (Stream file = new FileStream(outputPath, FileMode.CreateNew))
                {
                    await imageEncoder.SaveAsync(file, BuildConfiguration(encodingJob.Password));
                }
            }
            catch
            {
                return(new HttpResponseMessage(HttpStatusCode.InternalServerError));
            }

            return(Request.CreateResponse(HttpStatusCode.OK, new { id = guid.ToString() }));
        }
        public CompressionPlugin(CompressionConfiguration configuration)
        {
            Guard.AgainstNull(nameof(configuration), configuration);

            this.configuration = configuration;
        }
Exemple #9
0
 public ImageEncoder(ImageFormat format, CompressionConfiguration compressionConfiguration, EmbeddedImage embeddedImage = null)
     : this(format, null, compressionConfiguration, embeddedImage)
 {
 }