Don't clean already cleaned images & restructure project

This commit is contained in:
Denis-Cosmin Nutiu 2022-02-12 21:45:59 +02:00
parent bb7d563a08
commit 9bd612cde3
4 changed files with 37 additions and 24 deletions

View file

@ -1,5 +1,6 @@
using CommandLine;
using Image.Files;
using Image.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;

View file

@ -1,5 +1,4 @@
using System;
using System.IO;
using System.IO;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
@ -12,20 +11,26 @@ namespace Image.Files
/// <summary>
/// Creates the directory if it doesn't exist.
/// </summary>
/// <param name="destinationDirectory">The destination directory.</param>
public static void CreateDestinationDirectory(string destinationDirectory)
/// <param name="directoryPath">The destination directory's path.</param>
public static void CreateDestinationDirectory(string directoryPath)
{
#if NETSTANDARD2_1
Console.WriteLine("");
#elif NETSTANDARD2_0
Console.WriteLine("");
#elif NET5_0
if (Directory.Exists(destinationDirectory)) return;
//Logger.LogWarning("Output directory does not exist. Creating.");
Directory.CreateDirectory(destinationDirectory);
#endif
if (Directory.Exists(directoryPath)) return;
Logger.LogWarning("Output directory does not exist. Creating.");
Directory.CreateDirectory(directoryPath);
}
/// <summary>
/// CheckIfFileExists checks if file exists.
/// </summary>
/// <param name="filePath">The path of the file to be checked.</param>
/// <returns>Returns true if file exists, False otherwise.</returns>
public static bool CheckIfFileExists(string filePath)
{
var result = File.Exists(filePath);
Logger.LogDebug(result
? $"CheckIfFileExists - {filePath} exists."
: $"CheckIfFileExists - {filePath} doesn't exists.");
return result;
}
}
}

View file

@ -3,11 +3,12 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Image.Core;
using Image.Files;
using ImageMagick;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
namespace ConsoleInterface
namespace Image.Tasks
{
/// <summary>
/// TaskExecutor is a helper class for executing tasks in parallel.
@ -39,21 +40,27 @@ namespace ConsoleInterface
/// <summary>
/// Cleans an image. Errors are silenced by default.
/// </summary>
/// <param name="fileName">The file name of the image to be cleaned.</param>
/// <param name="newFilename">The new file name of the cleaned image.</param>
/// <param name="filePath">The file path of the image to be cleaned.</param>
/// <param name="newFilePath">The new file path of the cleaned image.</param>
/// <returns>True of the image was cleaned, false otherwise.</returns>
public bool CleanImage(string fileName, string newFilename)
public bool CleanImage(string filePath, string newFilePath)
{
try
{
var fileExists = FileSystemHelpers.CheckIfFileExists(newFilePath);
if (fileExists)
{
Logger.LogWarning($"File {newFilePath} exists, skipping");
return false;
}
ICompressor compressor = NullCompressor.Instance;
var imageMagick = new MagickImage(fileName);
var imageMagick = new MagickImage(filePath);
if (_options.EnableCompression) compressor = LosslessCompressor.Instance;
Logger.LogDebug(
$"Cleaning {fileName}, compression {_options.EnableCompression}, outputFormatter {nameof(_options.FileOutputFormatter)}.");
$"Cleaning {filePath}, compression {_options.EnableCompression}, outputFormatter {nameof(_options.FileOutputFormatter)}.");
IMetadataRemover metadataRemover = new ExifMetadataRemoverAndCompressor(imageMagick, compressor);
metadataRemover.CleanImage(newFilename);
metadataRemover.CleanImage(newFilePath);
return true;
}
catch (Exception e)
@ -89,9 +96,9 @@ namespace ConsoleInterface
var result = Task.WhenAll(tasks);
result.Wait();
var successTasks = tasks.Count(t => t.IsCompletedSuccessfully && t.Result);
var successTasks = tasks.Count(t => t.IsCompleted && t.Result);
var errorTasks = tasks.Count - successTasks;
Logger.LogInformation($"All tasks completed. Success: {successTasks}, Errors: {errorTasks}");
Logger.LogInformation($"All tasks completed. Success: {successTasks}, Failures: {errorTasks}");
}
}
}

View file

@ -1,7 +1,7 @@
using System;
using Image.Files;
namespace ConsoleInterface
namespace Image.Tasks
{
/// <summary>
/// TaskExecutorOptions is a class containing various parameters for the <see cref="TaskExecutor" /> class.