Add code documentation.
This commit is contained in:
parent
8860d2ef2d
commit
9730a996ca
16 changed files with 233 additions and 96 deletions
|
@ -10,40 +10,60 @@ namespace ConsoleInterface
|
|||
{
|
||||
internal static class Program
|
||||
{
|
||||
/// <summary>
|
||||
/// The console interface for the project and the main entrypoint.
|
||||
/// </summary>
|
||||
/// <param name="args">Command line provided args.</param>
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
Parser.Default.ParseArguments<Options>(args).WithParsed(RunOptions);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// RunOptions will be called after the command-line arguments were successfully parsed.
|
||||
/// </summary>
|
||||
private static void RunOptions(Options options)
|
||||
{
|
||||
var loggerFactory = LoggerFactory.Create(b => b.AddConsole());
|
||||
TaskExecutor.Logger = loggerFactory.CreateLogger(nameof(TaskExecutor));
|
||||
FilesRetriever.Logger = loggerFactory.CreateLogger(nameof(FilesRetriever));
|
||||
OriginalFilenameOutputFormatter.Logger =
|
||||
loggerFactory.CreateLogger(nameof(OriginalFilenameOutputFormatter));
|
||||
LocalSystemFilesRetriever.Logger = loggerFactory.CreateLogger(nameof(LocalSystemFilesRetriever));
|
||||
OriginalFilenameFileOutputPathFormatter.Logger =
|
||||
loggerFactory.CreateLogger(nameof(OriginalFilenameFileOutputPathFormatter));
|
||||
|
||||
var outputFormatter = OriginalFilenameOutputFormatter.Create(options.DestinationDirectory);
|
||||
var outputFormatter = OriginalFilenameFileOutputPathFormatter.Create(options.DestinationDirectory);
|
||||
var executor = TaskExecutor.Create(new TaskExecutorOptions
|
||||
{
|
||||
EnableCompression = options.CompressFiles is true,
|
||||
OutputFormatter = outputFormatter
|
||||
FileOutputPathFormatter = outputFormatter
|
||||
});
|
||||
var filesRetriever = FilesRetriever.Create();
|
||||
var filesRetriever = LocalSystemFilesRetriever.Create();
|
||||
|
||||
|
||||
executor.ParallelCleanImages(filesRetriever.GetFilenamesFromPath(options.SourceDirectory));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Options is a class defining command line options supported by this program.
|
||||
/// </summary>
|
||||
public class Options
|
||||
{
|
||||
/// <summary>
|
||||
/// CompressFiles indicates whether files should be compressed after being cleaned.
|
||||
/// </summary>
|
||||
[Option('c', "compress", Required = false, HelpText = "Compress images after cleaning.", Default = true)]
|
||||
public bool? CompressFiles { get; set; }
|
||||
|
||||
[Option('d', "dest", Required = false, HelpText = "The destination directory.", Default = "./cleaned")]
|
||||
/// <summary>
|
||||
/// DestinationDirectory represents the destination directory for the cleaned images.
|
||||
/// </summary>
|
||||
[Option('d', "dest", Required = false, HelpText = "The destination directory for the cleaned images.",
|
||||
Default = "./cleaned")]
|
||||
public string DestinationDirectory { get; set; }
|
||||
|
||||
[Value(0, MetaName = "source", HelpText = "The source directory.", Default = ".")]
|
||||
/// <summary>
|
||||
/// SourceDirectory represents the source directory of images.
|
||||
/// </summary>
|
||||
[Value(0, MetaName = "source", HelpText = "The source directory of images.", Default = ".")]
|
||||
public string SourceDirectory { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,27 +0,0 @@
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
namespace Image
|
||||
{
|
||||
public class FilesRetriever
|
||||
{
|
||||
public static ILogger Logger = NullLogger.Instance;
|
||||
|
||||
private FilesRetriever()
|
||||
{
|
||||
}
|
||||
|
||||
public static FilesRetriever Create()
|
||||
{
|
||||
return new FilesRetriever();
|
||||
}
|
||||
|
||||
public IEnumerable<string> GetFilenamesFromPath(string path)
|
||||
{
|
||||
Logger.LogInformation($"Getting files from {path}.");
|
||||
return Directory.GetFiles(path, "*.*");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,7 +1,14 @@
|
|||
namespace Image
|
||||
{
|
||||
/// <summary>
|
||||
/// ICompressor is an interface for implementing image compressors.
|
||||
/// </summary>
|
||||
public interface ICompressor
|
||||
{
|
||||
/// <summary>
|
||||
/// The method compresses an image in place.
|
||||
/// </summary>
|
||||
/// <param name="fileName">The file name of the image to be compressed.</param>
|
||||
public void Compress(string fileName);
|
||||
}
|
||||
}
|
15
ImageCore/IFileOutputPathFormatter.cs
Normal file
15
ImageCore/IFileOutputPathFormatter.cs
Normal file
|
@ -0,0 +1,15 @@
|
|||
namespace Image
|
||||
{
|
||||
/// <summary>
|
||||
/// IOutputFormatter is an interface for generating the output path and destination file name.
|
||||
/// </summary>
|
||||
public interface IFileOutputPathFormatter
|
||||
{
|
||||
/// <summary>
|
||||
/// Generates an absolute output path given the initial absolute file path.
|
||||
/// </summary>
|
||||
/// <param name="initialFilePath">The initial file path.</param>
|
||||
/// <returns>The formatted absolute output path.</returns>
|
||||
string GetOutputPath(string initialFilePath);
|
||||
}
|
||||
}
|
17
ImageCore/IFilesRetriever.cs
Normal file
17
ImageCore/IFilesRetriever.cs
Normal file
|
@ -0,0 +1,17 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace Image
|
||||
{
|
||||
/// <summary>
|
||||
/// An to interface enabling implementation of filename retrievers.
|
||||
/// </summary>
|
||||
public interface IFilesRetriever
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns all filenames from given path.
|
||||
/// </summary>
|
||||
/// <param name="directoryPath">The path.</param>
|
||||
/// <returns>An enumerable containing all file names.</returns>
|
||||
IEnumerable<string> GetFilenamesFromPath(string directoryPath);
|
||||
}
|
||||
}
|
|
@ -1,7 +1,14 @@
|
|||
namespace Image
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface for implementing metadata removers.
|
||||
/// </summary>
|
||||
public interface IMetadataRemover
|
||||
{
|
||||
void CleanImage(string newFileName);
|
||||
/// <summary>
|
||||
/// CleanImage cleans an image and saves it..
|
||||
/// </summary>
|
||||
/// <param name="newFilePath">The file path to save the clean image.</param>
|
||||
void CleanImage(string newFilePath);
|
||||
}
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
namespace Image
|
||||
{
|
||||
public interface IOutputFormatter
|
||||
{
|
||||
string FormatOutputPath(string filePath);
|
||||
}
|
||||
}
|
|
@ -6,9 +6,9 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Magick.NET-Q16-AnyCPU" Version="8.6.1" />
|
||||
<PackageReference Include="Magick.NET.Core" Version="8.6.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
|
||||
<PackageReference Include="Magick.NET-Q16-AnyCPU" Version="8.6.1"/>
|
||||
<PackageReference Include="Magick.NET.Core" Version="8.6.1"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0"/>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
35
ImageCore/LocalSystemFilesRetriever.cs
Normal file
35
ImageCore/LocalSystemFilesRetriever.cs
Normal file
|
@ -0,0 +1,35 @@
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
namespace Image
|
||||
{
|
||||
/// <summary>
|
||||
/// LocalSystemFilesRetriever reads files from the provided directory on the local system.
|
||||
/// </summary>
|
||||
public class LocalSystemFilesRetriever : IFilesRetriever
|
||||
{
|
||||
public static ILogger Logger = NullLogger.Instance;
|
||||
|
||||
private LocalSystemFilesRetriever()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Give a directory path it returns all the filenames.
|
||||
/// </summary>
|
||||
/// <param name="directoryPath">An absolute path pointing to a directory.</param>
|
||||
/// <returns>A list of file names found in the directory.</returns>
|
||||
public IEnumerable<string> GetFilenamesFromPath(string directoryPath)
|
||||
{
|
||||
Logger.LogInformation($"Getting files from {directoryPath}.");
|
||||
return Directory.GetFiles(directoryPath, "*.*");
|
||||
}
|
||||
|
||||
public static LocalSystemFilesRetriever Create()
|
||||
{
|
||||
return new LocalSystemFilesRetriever();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,6 +2,9 @@
|
|||
|
||||
namespace Image
|
||||
{
|
||||
/// <summary>
|
||||
/// LosslessCompressor compresses an image using lossless compression provided by ImageMagick.
|
||||
/// </summary>
|
||||
public class LosslessCompressor : ICompressor
|
||||
{
|
||||
public static readonly LosslessCompressor Instance = new LosslessCompressor();
|
||||
|
@ -11,7 +14,10 @@ namespace Image
|
|||
{
|
||||
_imageOptimizer = new ImageOptimizer();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// <inheritdoc />
|
||||
/// </summary>
|
||||
public void Compress(string fileName)
|
||||
{
|
||||
_imageOptimizer.LosslessCompress(fileName);
|
||||
|
|
|
@ -2,22 +2,34 @@
|
|||
|
||||
namespace Image
|
||||
{
|
||||
/// <summary>
|
||||
/// MetadataRemover removes metadata from an image. The exif profile.
|
||||
/// </summary>
|
||||
public class MetadataRemover : IMetadataRemover
|
||||
{
|
||||
private readonly ICompressor _compressor;
|
||||
private readonly IMagickImage _magickImage;
|
||||
|
||||
/// <summary>
|
||||
/// Constructs an instance of MetadataRemover.
|
||||
/// </summary>
|
||||
/// <param name="magickImage">MagicImage instance.</param>
|
||||
/// <param name="compressor">Compressor instance.</param>
|
||||
public MetadataRemover(IMagickImage magickImage, ICompressor compressor)
|
||||
{
|
||||
_magickImage = magickImage;
|
||||
_compressor = compressor;
|
||||
}
|
||||
|
||||
public void CleanImage(string newFileName)
|
||||
/// <summary>
|
||||
/// Cleans the images and compresses it.
|
||||
/// </summary>
|
||||
/// <param name="newFilePath">The file path to save the clean image.</param>
|
||||
public void CleanImage(string newFilePath)
|
||||
{
|
||||
_magickImage.RemoveProfile("exif");
|
||||
_magickImage.Write(newFileName);
|
||||
_compressor.Compress(newFileName);
|
||||
_magickImage.Write(newFilePath);
|
||||
_compressor.Compress(newFilePath);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,9 +1,15 @@
|
|||
namespace Image
|
||||
{
|
||||
/// <summary>
|
||||
/// Does nothing. Using this Compressor will have no effect.
|
||||
/// </summary>
|
||||
public class NullCompressor : ICompressor
|
||||
{
|
||||
public static readonly NullCompressor Instance = new NullCompressor();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// <inheritdoc />
|
||||
/// </summary>
|
||||
public void Compress(string fileName)
|
||||
{
|
||||
}
|
||||
|
|
52
ImageCore/OriginalFilenameFileOutputPathFormatter.cs
Normal file
52
ImageCore/OriginalFilenameFileOutputPathFormatter.cs
Normal file
|
@ -0,0 +1,52 @@
|
|||
using System.IO;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
namespace Image
|
||||
{
|
||||
/// <summary>
|
||||
/// OriginalFilenameFileOutputPathFormatter keeps the original file name of the image when formatting the new output
|
||||
/// path.
|
||||
/// </summary>
|
||||
public class OriginalFilenameFileOutputPathFormatter : IFileOutputPathFormatter
|
||||
{
|
||||
public static ILogger Logger = NullLogger.Instance;
|
||||
private readonly string _outputDirectory;
|
||||
|
||||
/// <summary>
|
||||
/// Creates an instance of OriginalFilenameFileOutputPathFormatter.
|
||||
/// </summary>
|
||||
/// <param name="outputDirectory">The output directory.</param>
|
||||
public OriginalFilenameFileOutputPathFormatter(string outputDirectory)
|
||||
{
|
||||
if (!Directory.Exists(outputDirectory))
|
||||
{
|
||||
Logger.LogWarning("Output directory does not exists. Creating.");
|
||||
Directory.CreateDirectory(outputDirectory);
|
||||
}
|
||||
|
||||
_outputDirectory = outputDirectory;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a path containing the file name in the output directory.
|
||||
/// </summary>
|
||||
/// <param name="initialFilePath">The initial path of the image.</param>
|
||||
/// <returns>An absolute path of the form output_directory/initialFileName.jpg</returns>
|
||||
public string GetOutputPath(string initialFilePath)
|
||||
{
|
||||
var fileName = Path.GetFileName(initialFilePath)?.Split(".")[0];
|
||||
var path = Path.Join(_outputDirectory, $"{fileName}.jpg");
|
||||
return path;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an instance of OriginalFilenameFileOutputPathFormatter.
|
||||
/// </summary>
|
||||
/// <param name="outputDirectory">The output directory.</param>
|
||||
public static OriginalFilenameFileOutputPathFormatter Create(string outputDirectory)
|
||||
{
|
||||
return new OriginalFilenameFileOutputPathFormatter(outputDirectory);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
using System.IO;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
namespace Image
|
||||
{
|
||||
public class OriginalFilenameOutputFormatter : IOutputFormatter
|
||||
{
|
||||
public static ILogger Logger = NullLogger.Instance;
|
||||
private readonly string _rootDirectory;
|
||||
|
||||
public OriginalFilenameOutputFormatter(string rootDirectory)
|
||||
{
|
||||
if (!Directory.Exists(rootDirectory))
|
||||
{
|
||||
Logger.LogWarning("Output directory does not exists. Creating.");
|
||||
Directory.CreateDirectory(rootDirectory);
|
||||
}
|
||||
|
||||
_rootDirectory = rootDirectory;
|
||||
}
|
||||
|
||||
public string FormatOutputPath(string filePath)
|
||||
{
|
||||
var fileName = Path.GetFileName(filePath)?.Split(".")[0];
|
||||
var path = Path.Join(_rootDirectory, $"{fileName}.jpg");
|
||||
return path;
|
||||
}
|
||||
|
||||
public static OriginalFilenameOutputFormatter Create(string rootDirectory)
|
||||
{
|
||||
return new OriginalFilenameOutputFormatter(rootDirectory);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -8,34 +8,49 @@ using Microsoft.Extensions.Logging.Abstractions;
|
|||
|
||||
namespace Image
|
||||
{
|
||||
/// <summary>
|
||||
/// TaskExecutor is a helper class for executing tasks in parallel.
|
||||
/// </summary>
|
||||
public class TaskExecutor
|
||||
{
|
||||
public static ILogger Logger = NullLogger.Instance;
|
||||
private readonly TaskExecutorOptions _options;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of TaskExecutor.
|
||||
/// </summary>
|
||||
/// <param name="options">The TaskExecutor options.</param>
|
||||
/// <exception cref="ArgumentException">Raised when the options are null.</exception>
|
||||
private TaskExecutor(TaskExecutorOptions options)
|
||||
{
|
||||
_options = options ?? throw new ArgumentException("Options cannot be null!");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of TaskExecutor by calling the private constructor.
|
||||
/// </summary>
|
||||
/// <param name="options">The TaskExecutor options.</param>
|
||||
public static TaskExecutor Create(TaskExecutorOptions options)
|
||||
{
|
||||
return new TaskExecutor(options);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
/// <returns>True of the image was cleaned, false otherwise.</returns>
|
||||
public bool CleanImage(string fileName, string newFilename)
|
||||
{
|
||||
try
|
||||
{
|
||||
ICompressor compressor = NullCompressor.Instance;
|
||||
var imageMagick = new MagickImage(fileName);
|
||||
if (_options.EnableCompression)
|
||||
{
|
||||
compressor = LosslessCompressor.Instance;
|
||||
}
|
||||
if (_options.EnableCompression) compressor = LosslessCompressor.Instance;
|
||||
|
||||
Logger.LogDebug(
|
||||
$"Cleaning {fileName}, compression {_options.EnableCompression}, outputFormatter {nameof(_options.OutputFormatter)}.");
|
||||
$"Cleaning {fileName}, compression {_options.EnableCompression}, outputFormatter {nameof(_options.FileOutputPathFormatter)}.");
|
||||
IMetadataRemover metadataRemover = new MetadataRemover(imageMagick, compressor);
|
||||
metadataRemover.CleanImage(newFilename);
|
||||
return true;
|
||||
|
@ -47,6 +62,10 @@ namespace Image
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cleans images in parallel using the built in Task Parallel Library.
|
||||
/// </summary>
|
||||
/// <param name="fileNames">An enumerable of file names.</param>
|
||||
public void ParallelCleanImages(IEnumerable<string> fileNames)
|
||||
{
|
||||
Logger.LogInformation("Starting parallel image cleaning.");
|
||||
|
@ -61,7 +80,7 @@ namespace Image
|
|||
foreach (var fileName in filenamesArray)
|
||||
{
|
||||
var task = new Task<bool>(() =>
|
||||
CleanImage(fileName, _options.OutputFormatter.FormatOutputPath(fileName)));
|
||||
CleanImage(fileName, _options.FileOutputPathFormatter.GetOutputPath(fileName)));
|
||||
tasks.Add(task);
|
||||
task.Start();
|
||||
}
|
||||
|
|
|
@ -2,16 +2,26 @@
|
|||
|
||||
namespace Image
|
||||
{
|
||||
/// <summary>
|
||||
/// TaskExecutorOptions is a class containing various parameters for the <see cref="TaskExecutor" /> class.
|
||||
/// </summary>
|
||||
public class TaskExecutorOptions
|
||||
{
|
||||
private IOutputFormatter _outputFormatter;
|
||||
private IFileOutputPathFormatter _fileOutputPathFormatter;
|
||||
|
||||
public IOutputFormatter OutputFormatter
|
||||
/// <summary>
|
||||
/// The file output path formatter. It cannot be null.
|
||||
/// A implementation of <see cref="IFileOutputPathFormatter" />.
|
||||
/// </summary>
|
||||
public IFileOutputPathFormatter FileOutputPathFormatter
|
||||
{
|
||||
get => _outputFormatter;
|
||||
set => _outputFormatter = value ?? throw new ArgumentException("Output formatter cannot be null!");
|
||||
get => _fileOutputPathFormatter;
|
||||
set => _fileOutputPathFormatter = value ?? throw new ArgumentException("Output formatter cannot be null!");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A boolean indicating if compression should be performed after cleaning the images.
|
||||
/// </summary>
|
||||
public bool EnableCompression { get; set; } = true;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue