Skip to content

Commit

Permalink
The ImageOptimizer now returns more information about the optimized f…
Browse files Browse the repository at this point in the history
…iles.
  • Loading branch information
devedse committed Jul 28, 2017
1 parent 530c767 commit bd457f7
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 75 deletions.
2 changes: 1 addition & 1 deletion DeveImageOptimizer.Tests/ImageOptimizerWorks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private async Task OptimizeFileTest(string fileName)
{
var worked = await fop.OptimizeFile(image1temppath);

Assert.True(worked);
Assert.True(worked.Successful);

var fileOptimized = new FileInfo(image1temppath);
var fileUnoptimized = new FileInfo(image1path);
Expand Down
3 changes: 1 addition & 2 deletions DeveImageOptimizer/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ public static class Constants
{
public static string[] ValidExtensions { get; } = new string[] { ".PNG", ".JPG", ".JPEG", ".GIF", ".BMP" };
public const string TempDirectoryName = "Temp";

//public const string VipsDir = @"C:\Users\Davy\Downloads\VIPS";

public const string VipsDir = @".nuget\packages\vipsnuget\1.0.5\tools";
}
}
104 changes: 68 additions & 36 deletions DeveImageOptimizer/FileProcessing/FileOptimizerProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using DeveImageOptimizer.Helpers;
using DeveImageOptimizer.State;
using ExifLibrary;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
Expand All @@ -19,58 +22,87 @@ public FileOptimizerProcessor(string pathToFileOptimizer, string tempDirectory)
Directory.CreateDirectory(tempDirectory);
}

public async Task<bool> OptimizeFile(string fileToOptimize, bool saveFailedOptimizedFile = false)
public async Task<OptimizedFileResult> OptimizeFile(string fileToOptimize, bool saveFailedOptimizedFile = false)
{
var fileName = Path.GetFileName(fileToOptimize);
var tempFilePath = Path.Combine(_tempDirectory, RandomFileNameHelper.RandomizeFileName(fileName));
long originalFileSize = new FileInfo(fileToOptimize).Length;

await AsyncFileHelper.CopyFileAsync(fileToOptimize, tempFilePath, true);
var tempFiles = new List<string>();
bool imagesEqual = false;

Orientation jpegFileOrientation = Orientation.Normal;
bool shouldUseJpgWorkaround = FileTypeHelper.IsJpgFile(tempFilePath);
if (shouldUseJpgWorkaround)
{
jpegFileOrientation = await ExifImageRotator.UnrotateImageAsync(tempFilePath);
}
var errors = new List<string>();

var processStartInfo = new ProcessStartInfo(_pathToFileOptimizer, tempFilePath)
try
{
CreateNoWindow = true,
};
//processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
//processStartInfo.CreateNoWindow = true;
var fileName = Path.GetFileName(fileToOptimize);
var tempFilePath = Path.Combine(_tempDirectory, RandomFileNameHelper.RandomizeFileName(fileName));
tempFiles.Add(tempFilePath);

await ProcessRunner.RunProcessAsync(processStartInfo);
await AsyncFileHelper.CopyFileAsync(fileToOptimize, tempFilePath, true);

if (shouldUseJpgWorkaround)
{
await ExifImageRotator.RerotateImageAsync(tempFilePath, jpegFileOrientation);
}
Orientation jpegFileOrientation = Orientation.Normal;
bool shouldUseJpgWorkaround = FileTypeHelper.IsJpgFile(tempFilePath);
if (shouldUseJpgWorkaround)
{
jpegFileOrientation = await ExifImageRotator.UnrotateImageAsync(tempFilePath);
}

var imagesEqual = await ImageComparer2.AreImagesEqualAsync(fileToOptimize, tempFilePath);
var processStartInfo = new ProcessStartInfo(_pathToFileOptimizer, tempFilePath)
{
CreateNoWindow = true,
};
//processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
//processStartInfo.CreateNoWindow = true;

if (imagesEqual)
await ProcessRunner.RunProcessAsync(processStartInfo);

if (shouldUseJpgWorkaround)
{
await ExifImageRotator.RerotateImageAsync(tempFilePath, jpegFileOrientation);
}

imagesEqual = await ImageComparer2.AreImagesEqualAsync(fileToOptimize, tempFilePath);

if (imagesEqual)
{
await AsyncFileHelper.CopyFileAsync(tempFilePath, fileToOptimize, true);
}
else
{
errors.Add("Optimized image isn't equal to source image.");

if (saveFailedOptimizedFile)
{
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileToOptimize);
var fileExtension = Path.GetExtension(fileToOptimize);
var newFileName = $"{fileNameWithoutExtension}_FAILED{fileExtension}";

var directoryOfFileToOptimize = Path.GetDirectoryName(fileToOptimize);
var newFilePath = Path.Combine(directoryOfFileToOptimize, newFileName);

//Write a file as Blah_FAILED.png
await AsyncFileHelper.CopyFileAsync(tempFilePath, newFilePath, true);
}
}
}
catch (Exception ex)
{
await AsyncFileHelper.CopyFileAsync(tempFilePath, fileToOptimize, true);
errors.Add($"Exception happened when optimizing or comparing the image:{Environment.NewLine}{ex}");
}
else
finally
{
if (saveFailedOptimizedFile)
foreach (var tempFile in tempFiles)
{
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileToOptimize);
var fileExtension = Path.GetExtension(fileToOptimize);
var newFileName = $"{fileNameWithoutExtension}_FAILED{fileExtension}";

var directoryOfFileToOptimize = Path.GetDirectoryName(fileToOptimize);
var newFilePath = Path.Combine(directoryOfFileToOptimize, newFileName);

//Write a file as Blah_FAILED.png
await AsyncFileHelper.CopyFileAsync(tempFilePath, newFilePath, true);
FileHelperMethods.SafeDeleteTempFile(tempFile);
}
}

File.Delete(tempFilePath);
return imagesEqual;
//The fileToOptimize has been overwritten by the optimized file, so this is the optimized file size.
long optimizedFileSize = new FileInfo(fileToOptimize).Length;

var optimizedFileResult = new OptimizedFileResult(fileToOptimize, imagesEqual, originalFileSize, optimizedFileSize, errors);


return optimizedFileResult;
}
}
}
43 changes: 21 additions & 22 deletions DeveImageOptimizer/FileProcessing/FileProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,43 @@ public FileProcessor(FileOptimizerProcessor fileOptimizer, IFilesProcessingState
_processingStateData = processingStateData;
}

public async Task ProcessDirectory(string directory)
public async Task<IEnumerable<OptimizedFileResult>> ProcessDirectory(string directory)
{
await ProcessDirectoryInternal(directory);
}
var optimizedFileResultsForThisDirectory = new List<OptimizedFileResult>();

private async Task ProcessDirectoryInternal(string directory)
{
var files = Directory.GetFiles(directory);

foreach (var file in files)
{
await ProcessFile(file);
var extension = Path.GetExtension(file).ToUpperInvariant();
if (Constants.ValidExtensions.Contains(extension))
{
var optimizedFileResult = await ProcessFile(file);
optimizedFileResultsForThisDirectory.Add(optimizedFileResult);
}
}

var directories = Directory.GetDirectories(directory);
IEnumerable<OptimizedFileResult> concattedRetVal = optimizedFileResultsForThisDirectory;

var directories = Directory.GetDirectories(directory);
foreach (var subDirectory in directories)
{
await ProcessDirectoryInternal(subDirectory);
var results = await ProcessDirectory(subDirectory);
concattedRetVal = concattedRetVal.Concat(results);
}

return concattedRetVal;
}

private async Task ProcessFile(string file)
private async Task<OptimizedFileResult> ProcessFile(string file)
{
var extension = Path.GetExtension(file).ToUpperInvariant();
if (Constants.ValidExtensions.Contains(extension))
var optimizedFileResult = await _fileOptimizer.OptimizeFile(file);
if (_processingStateData != null)
{
var worked = await _fileOptimizer.OptimizeFile(file);

if (worked)
{
_processingStateData.AddProcessedFile(file);
}
else
{
_processingStateData.AddFailedFile(file);
}
_processingStateData.AddProcessedFile(optimizedFileResult);
}

return optimizedFileResult;
}
}
}
25 changes: 25 additions & 0 deletions DeveImageOptimizer/Helpers/FileHelperMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace DeveImageOptimizer.Helpers
{
public static class FileHelperMethods
{
public static void SafeDeleteTempFile(string path)
{
try
{
if (File.Exists(path))
{
File.Delete(path);
}
}
catch (Exception ex)
{
Console.WriteLine($"Warning: Couldn't remove tempfile at path: '{path}'. Exception:{Environment.NewLine}{ex}");
}
}
}
}
5 changes: 1 addition & 4 deletions DeveImageOptimizer/Helpers/ImageComparer2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,7 @@ private static bool AreImagesEqual(string image1Path, string image2Path, bool us
{
foreach (var tempFile in tempFiles)
{
if (File.Exists(tempFile))
{
File.Delete(tempFile);
}
FileHelperMethods.SafeDeleteTempFile(tempFile);
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions DeveImageOptimizer/ImageConversion/ImageConverter.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using DeveImageOptimizer.Helpers;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace DeveImageOptimizer.ImageConversion
{
Expand Down
9 changes: 2 additions & 7 deletions DeveImageOptimizer/State/IFilesProcessingState.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace DeveImageOptimizer.State
namespace DeveImageOptimizer.State
{
public interface IFilesProcessingState
{
void AddProcessedFile(string file);
void AddFailedFile(string file);
void AddProcessedFile(OptimizedFileResult optimizedFileResult);
}
}
24 changes: 24 additions & 0 deletions DeveImageOptimizer/State/OptimizedFileResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections.Generic;

namespace DeveImageOptimizer.State
{
public class OptimizedFileResult
{
public string Path { get; }
public bool Successful { get; }

public long OriginalSize { get; }
public long OptimizedSize { get; }

public List<string> Errors { get; set; }

public OptimizedFileResult(string path, bool successful, long originalSize, long optimizedSize, List<string> errors)
{
Path = path;
Successful = successful;
OriginalSize = originalSize;
OptimizedSize = optimizedSize;
Errors = errors;
}
}
}

0 comments on commit bd457f7

Please sign in to comment.