private IOptimizerResult ProcessStream(byte[] imageBytes)
        {
            if (imageBytes == null)
            {
                return(new JpegOptimizerResult
                {
                    SizeBefore = 0,
                    SizeAfter = 0,
                    Success = true
                });
            }
            var stream = new MemoryStream(imageBytes);

            var tempFilePath = GetTempFilePath();

            using (var fileStream = File.OpenWrite(tempFilePath))
            {
                stream.CopyTo(fileStream);
            }

            var result = new JpegOptimizerResult
            {
                SizeBefore = (int)stream.Length
            };

            var jpegtran = Process.Start(Path, string.Format("-optimize -copy none -progressive -outfile \"{0}\" \"{0}\"", tempFilePath));

            if (jpegtran != null && jpegtran.WaitForExit(ToolTimeout))
            {
                if (jpegtran.ExitCode != 0)
                {
                    result.Success      = false;
                    result.ErrorMessage = "jpegtran exited with unexpected exit code " + jpegtran.ExitCode;
                    return(result);
                }

                result.Success   = true;
                result.SizeAfter = (int)new FileInfo(tempFilePath).Length;

                if (result.SizeBefore > result.SizeAfter)
                {
                    // read the file to memory so we can nuke the temp file
                    using (var fileStream = File.OpenRead(tempFilePath))
                    {
                        result.OptimizedBytes = new byte[fileStream.Length];
                        fileStream.Read(result.OptimizedBytes, 0, result.OptimizedBytes.Length);
                    }
                }
                File.Delete(tempFilePath);

                return(result);
            }

            result.Success      = false;
            result.ErrorMessage = string.Format("jpegtran took longer than {0} to execute, which we consider a failure.", ToolTimeout);

            return(result);
        }
        private IOptimizerResult ProcessStream(byte[] imageBytes)
        {
            if (imageBytes == null)
            {
                return new JpegOptimizerResult
                {
                    SizeBefore = 0,
                    SizeAfter = 0,
                    Success = true
                };
            }
            var stream = new MemoryStream(imageBytes);

            var tempFilePath = GetTempFilePath();

            using (var fileStream = File.OpenWrite(tempFilePath))
            {
                stream.CopyTo(fileStream);
            }

            var result = new JpegOptimizerResult
            {
                SizeBefore = (int)stream.Length
            };

            var jpegtran = Process.Start(Path, string.Format("-optimize -copy none -progressive -outfile \"{0}\" \"{0}\"", tempFilePath));
            if (jpegtran != null && jpegtran.WaitForExit(ToolTimeout))
            {
                if (jpegtran.ExitCode != 0)
                {
                    result.Success = false;
                    result.ErrorMessage = "jpegtran exited with unexpected exit code " + jpegtran.ExitCode;
                    return result;
                }

                result.Success = true;
                result.SizeAfter = (int)new FileInfo(tempFilePath).Length;

                if (result.SizeBefore > result.SizeAfter)
                {
                    // read the file to memory so we can nuke the temp file
                    using (var fileStream = File.OpenRead(tempFilePath))
                    {
                        result.OptimizedBytes = new byte[fileStream.Length];
                        fileStream.Read(result.OptimizedBytes, 0, result.OptimizedBytes.Length);
                    }
                }
                File.Delete(tempFilePath);

                return result;
            }

            result.Success = false;
            result.ErrorMessage = string.Format("jpegtran took longer than {0} to execute, which we consider a failure.", ToolTimeout);

            return result;
        }
Beispiel #3
0
        public override IOptimizerResult Optimize(MediaStream stream)
        {
            var tempFilePath = GetTempFilePath();

            using (var fileStream = File.OpenWrite(tempFilePath))
            {
                stream.Stream.CopyTo(fileStream);
            }

            var result = new JpegOptimizerResult();

            result.SizeBefore = (int)stream.Length;

            var jpegtran = Process.Start(ExePath, "-optimize -copy none -progressive -outfile \"{0}\" \"{0}\"".FormatWith(tempFilePath));

            if (jpegtran != null && jpegtran.WaitForExit(ToolTimeout))
            {
                if (jpegtran.ExitCode != 0)
                {
                    result.Success      = false;
                    result.ErrorMessage = "jpegtran exited with unexpected exit code " + jpegtran.ExitCode;
                    return(result);
                }

                result.Success   = true;
                result.SizeAfter = (int)new FileInfo(tempFilePath).Length;

                // read the file to memory so we can nuke the temp file
                using (var fileStream = File.OpenRead(tempFilePath))
                {
                    result.ResultStream = new MemoryStream();
                    fileStream.CopyTo(result.ResultStream);
                    result.ResultStream.Seek(0, SeekOrigin.Begin);
                }

                File.Delete(tempFilePath);

                return(OptimizationSuccessful(result));
            }

            result.Success      = false;
            result.ErrorMessage = "jpegtran took longer than {0} to execute, which we consider a failure.".FormatWith(ToolTimeout);
            // kill the process and clean up the tempfile as we have discarded the optimizer
            jpegtran.Kill();
            try
            {
                File.Delete(tempFilePath);
            } catch (IOException)
            {
                // Silently discard any io errors in the file deletion...
            }

            return(result);
        }
Beispiel #4
0
        public override IOptimizerResult Optimize(MediaStream stream)
        {
            var tempFilePath = GetTempFilePath();

            using (var fileStream = File.OpenWrite(tempFilePath))
            {
                stream.Stream.CopyTo(fileStream);
            }

            var result = new JpegOptimizerResult();

            result.SizeBefore = (int)stream.Length;

            var jpegtran = Process.Start(ExePath, "-optimize -copy none -progressive -outfile \"{0}\" \"{0}\"".FormatWith(tempFilePath));
            if (jpegtran != null && jpegtran.WaitForExit(ToolTimeout))
            {
                if (jpegtran.ExitCode != 0)
                {
                    result.Success = false;
                    result.ErrorMessage = "jpegtran exited with unexpected exit code " + jpegtran.ExitCode;
                    return result;
                }

                result.Success = true;
                result.SizeAfter = (int)new FileInfo(tempFilePath).Length;

                // read the file to memory so we can nuke the temp file
                using (var fileStream = File.OpenRead(tempFilePath))
                {
                    result.ResultStream = new MemoryStream();
                    fileStream.CopyTo(result.ResultStream);
                    result.ResultStream.Seek(0, SeekOrigin.Begin);
                }

                File.Delete(tempFilePath);

                return OptimizationSuccessful(result);
            }

            result.Success = false;
            result.ErrorMessage = "jpegtran took longer than {0} to execute, which we consider a failure.".FormatWith(ToolTimeout);
            // kill the process and clean up the tempfile as we have discarded the optimizer
            jpegtran.Kill();
            try
            {
                File.Delete(tempFilePath);
            } catch(IOException)
            {
                // Silently discard any io errors in the file deletion...
            }

            return result;
        }
Beispiel #5
0
        //jpegtran -optimize -progressive -copy none -outfile "<filename>" "<filename>"
        public IOptimizerResult Optimize()
        {
            var tempFilePath = GetTempFilePath();

            using (var fileStream = File.OpenWrite(tempFilePath))
            {
                _jpegStream.CopyTo(fileStream);
            }

            var result = new JpegOptimizerResult();

            result.SizeBefore = (int)new FileInfo(tempFilePath).Length;

            var jpegtran = Process.Start(ToolPath, "-optimize -copy none -progressive -outfile \"{0}\" \"{0}\"".FormatWith(tempFilePath));

            if (jpegtran != null && jpegtran.WaitForExit(ToolTimeout))
            {
                if (jpegtran.ExitCode != 0)
                {
                    result.Success      = false;
                    result.ErrorMessage = "jpegtran exited with unexpected exit code " + jpegtran.ExitCode;
                    return(result);
                }

                result.Success   = true;
                result.SizeAfter = (int)new FileInfo(tempFilePath).Length;

                // read the file to memory so we can nuke the temp file
                using (var fileStream = File.OpenRead(tempFilePath))
                {
                    result.ResultStream = new MemoryStream();
                    fileStream.CopyTo(result.ResultStream);
                }

                return(result);
            }

            result.Success      = false;
            result.ErrorMessage = "jpegtran took longer than {0} to execute, which we consider a failure.".FormatWith(ToolTimeout);

            return(result);
        }