Skip to content

Commit

Permalink
Added temp path, removing original file, rename temp to original.
Browse files Browse the repository at this point in the history
  • Loading branch information
ElSiipo committed Oct 10, 2019
1 parent 2a017bd commit 51955bd
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions CompressImagesFunction/Compressors/MozJpegCompress.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.IO;

namespace CompressImagesFunction.Compressors
{
Expand All @@ -10,23 +11,31 @@ public class MozJpegCompress : ICompress
public string[] SupportedExtensions =>
new[] { ".jpg", ".jpeg" };

public void LosslessCompress(string path) =>
Compress(path, LosslessPlugin);
public void LosslessCompress(string path)
{
var arguments = $"-outfile {path}";
Compress(LosslessPlugin, arguments);
}

public void LossyCompress(string path)
{
var tempPath = path + ".tmp";
var arguments = $"-quality 80 -outfile {tempPath} {path}";

public void LossyCompress(string path) =>
Compress(path, LossyPlugin, "-quality 80 ");
Compress(LossyPlugin, arguments);

private void Compress(string path, string compressionType, string switches = "")
File.Delete(path);
File.Move(tempPath, path);
}

private void Compress(string compressionType, string arguments)
{
var processStartInfo = new ProcessStartInfo
{
UseShellExecute = false,
CreateNoWindow = true,
FileName = compressionType,
Arguments = $"{switches}-outfile {path} {path}"

// FileName = "mozjpeg",
// Arguments = $"{compressionType} -quality 80 -outfile {path} {path}"
Arguments = arguments,
};
using (var process = Process.Start(processStartInfo))
{
Expand Down

0 comments on commit 51955bd

Please sign in to comment.