2022-04-02 14:22:42 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using Image;
|
2022-02-12 21:25:34 +00:00
|
|
|
|
using Image.Core;
|
2022-01-23 17:30:05 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
|
|
|
|
2022-04-02 14:22:42 +00:00
|
|
|
|
namespace ConsoleInterface
|
2022-01-23 17:30:05 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2022-04-03 15:16:17 +00:00
|
|
|
|
/// DirectoryOutputSink keeps the original file name of the image when formatting it.
|
|
|
|
|
/// DirectoryOutputSink also saves all the file names into a new directory.
|
2022-01-23 17:30:05 +00:00
|
|
|
|
/// path.
|
|
|
|
|
/// </summary>
|
2022-04-03 15:16:17 +00:00
|
|
|
|
public class DirectoryOutputSink : IOutputSink
|
2022-01-23 17:30:05 +00:00
|
|
|
|
{
|
|
|
|
|
public static ILogger Logger = NullLogger.Instance;
|
|
|
|
|
private readonly string _outputDirectory;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-04-03 15:16:17 +00:00
|
|
|
|
/// Creates an instance of DirectoryOutputSink.
|
2022-01-23 17:30:05 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="outputDirectory">The output directory.</param>
|
2022-04-03 15:16:17 +00:00
|
|
|
|
public DirectoryOutputSink(string outputDirectory)
|
2022-01-23 17:30:05 +00:00
|
|
|
|
{
|
2022-01-25 20:41:02 +00:00
|
|
|
|
if (outputDirectory.Equals(""))
|
2022-04-02 14:33:48 +00:00
|
|
|
|
{
|
2022-01-25 20:41:02 +00:00
|
|
|
|
outputDirectory = ".";
|
2022-04-02 14:33:48 +00:00
|
|
|
|
}
|
2022-02-12 21:25:34 +00:00
|
|
|
|
else
|
2022-04-02 14:33:48 +00:00
|
|
|
|
{
|
2022-02-12 21:25:34 +00:00
|
|
|
|
FileSystemHelpers.CreateDestinationDirectory(outputDirectory);
|
2022-04-02 14:33:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-23 17:30:05 +00:00
|
|
|
|
_outputDirectory = outputDirectory;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-12 21:25:34 +00:00
|
|
|
|
public bool Save(IMetadataRemover metadataRemover)
|
|
|
|
|
{
|
|
|
|
|
var newFilePath = GetOutputPath(metadataRemover.GetImagePath());
|
|
|
|
|
var fileExists = FileSystemHelpers.CheckIfFileExists(newFilePath);
|
|
|
|
|
if (fileExists)
|
|
|
|
|
{
|
|
|
|
|
Logger.LogWarning($"File {newFilePath} exists, skipping");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-04-02 14:22:42 +00:00
|
|
|
|
|
2022-02-12 21:25:34 +00:00
|
|
|
|
// Save the image under the same name in the new directory.
|
2022-04-03 15:16:17 +00:00
|
|
|
|
metadataRemover.CleanImage();
|
|
|
|
|
metadataRemover.SaveImage(newFilePath);
|
2022-02-12 21:25:34 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-02 14:22:42 +00:00
|
|
|
|
/// <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)
|
|
|
|
|
{
|
|
|
|
|
Logger.LogDebug($"KeepFilenameFormatter - {_outputDirectory} - {initialFilePath}");
|
|
|
|
|
if (string.IsNullOrEmpty(initialFilePath))
|
2022-04-02 14:33:48 +00:00
|
|
|
|
{
|
2022-04-02 14:22:42 +00:00
|
|
|
|
throw new ArgumentException("The output file path cannot be null or empty!");
|
2022-04-02 14:33:48 +00:00
|
|
|
|
}
|
2022-04-02 14:22:42 +00:00
|
|
|
|
|
|
|
|
|
var fileName = Path.GetFileName(initialFilePath).Split('.')[0];
|
|
|
|
|
var path = Path.Combine(_outputDirectory, $"{fileName}.jpg");
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-23 17:30:05 +00:00
|
|
|
|
/// <summary>
|
2022-04-03 15:16:17 +00:00
|
|
|
|
/// Creates an instance of DirectoryOutputSink.
|
2022-01-23 17:30:05 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="outputDirectory">The output directory.</param>
|
2022-04-03 15:16:17 +00:00
|
|
|
|
public static DirectoryOutputSink Create(string outputDirectory)
|
2022-01-23 17:30:05 +00:00
|
|
|
|
{
|
2022-04-03 15:16:17 +00:00
|
|
|
|
return new DirectoryOutputSink(outputDirectory);
|
2022-01-23 17:30:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|