From 9bd612cde3cea6e4772d4539fb1b8c283b24edec Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Sat, 12 Feb 2022 21:45:59 +0200 Subject: [PATCH] Don't clean already cleaned images & restructure project --- ConsoleInterface/Program.cs | 1 + ImageCore/Files/FileSystemHelpers.cs | 33 +++++++++++-------- .../Tasks}/TaskExecutor.cs | 25 +++++++++----- .../Tasks}/TaskExecutorOptions.cs | 2 +- 4 files changed, 37 insertions(+), 24 deletions(-) rename {ConsoleInterface => ImageCore/Tasks}/TaskExecutor.cs (80%) rename {ConsoleInterface => ImageCore/Tasks}/TaskExecutorOptions.cs (97%) diff --git a/ConsoleInterface/Program.cs b/ConsoleInterface/Program.cs index 56bbf5b..076e9f5 100644 --- a/ConsoleInterface/Program.cs +++ b/ConsoleInterface/Program.cs @@ -1,5 +1,6 @@ using CommandLine; using Image.Files; +using Image.Tasks; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; diff --git a/ImageCore/Files/FileSystemHelpers.cs b/ImageCore/Files/FileSystemHelpers.cs index a9abc45..2d02555 100644 --- a/ImageCore/Files/FileSystemHelpers.cs +++ b/ImageCore/Files/FileSystemHelpers.cs @@ -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 /// /// Creates the directory if it doesn't exist. /// - /// The destination directory. - public static void CreateDestinationDirectory(string destinationDirectory) + /// The destination directory's path. + 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); + } + /// + /// CheckIfFileExists checks if file exists. + /// + /// The path of the file to be checked. + /// Returns true if file exists, False otherwise. + public static bool CheckIfFileExists(string filePath) + { + var result = File.Exists(filePath); + Logger.LogDebug(result + ? $"CheckIfFileExists - {filePath} exists." + : $"CheckIfFileExists - {filePath} doesn't exists."); + return result; } } } \ No newline at end of file diff --git a/ConsoleInterface/TaskExecutor.cs b/ImageCore/Tasks/TaskExecutor.cs similarity index 80% rename from ConsoleInterface/TaskExecutor.cs rename to ImageCore/Tasks/TaskExecutor.cs index 3ba11a0..8dbcfb7 100644 --- a/ConsoleInterface/TaskExecutor.cs +++ b/ImageCore/Tasks/TaskExecutor.cs @@ -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 { /// /// TaskExecutor is a helper class for executing tasks in parallel. @@ -39,21 +40,27 @@ namespace ConsoleInterface /// /// Cleans an image. Errors are silenced by default. /// - /// The file name of the image to be cleaned. - /// The new file name of the cleaned image. + /// The file path of the image to be cleaned. + /// The new file path of the cleaned image. /// True of the image was cleaned, false otherwise. - 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}"); } } } \ No newline at end of file diff --git a/ConsoleInterface/TaskExecutorOptions.cs b/ImageCore/Tasks/TaskExecutorOptions.cs similarity index 97% rename from ConsoleInterface/TaskExecutorOptions.cs rename to ImageCore/Tasks/TaskExecutorOptions.cs index 93f30ec..7f41816 100644 --- a/ConsoleInterface/TaskExecutorOptions.cs +++ b/ImageCore/Tasks/TaskExecutorOptions.cs @@ -1,7 +1,7 @@ using System; using Image.Files; -namespace ConsoleInterface +namespace Image.Tasks { /// /// TaskExecutorOptions is a class containing various parameters for the class.