ImgMetadataRemover/ImageCore/Output/KeepFilenameFormatter.cs

53 lines
2 KiB
C#
Raw Normal View History

2022-01-23 17:30:05 +00:00
using System.IO;
using GuardNet;
2022-01-23 17:30:05 +00:00
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
2022-01-25 20:57:22 +00:00
namespace Image.Output
2022-01-23 17:30:05 +00:00
{
/// <summary>
2022-01-25 20:57:22 +00:00
/// KeepFilenameFormatter keeps the original file name of the image when formatting the new output
2022-01-23 17:30:05 +00:00
/// path.
/// </summary>
2022-01-25 20:57:22 +00:00
public class KeepFilenameFormatter : IFileOutputFormatter
2022-01-23 17:30:05 +00:00
{
public static ILogger Logger = NullLogger.Instance;
private readonly string _outputDirectory;
/// <summary>
/// Creates an instance of OriginalFilenameFileOutputPathFormatter.
/// </summary>
/// <param name="outputDirectory">The output directory.</param>
2022-01-25 20:57:22 +00:00
public KeepFilenameFormatter(string outputDirectory)
2022-01-23 17:30:05 +00:00
{
if (outputDirectory.Equals(""))
{
outputDirectory = ".";
}
2022-01-23 17:30:05 +00:00
_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)
{
2022-01-26 18:50:02 +00:00
Logger.LogDebug($"KeepFilenameFormatter - {_outputDirectory} - {initialFilePath}");
Guard.NotNullOrEmpty(initialFilePath, nameof(initialFilePath));
2022-01-23 17:30:05 +00:00
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>
2022-01-25 20:57:22 +00:00
public static KeepFilenameFormatter Create(string outputDirectory)
2022-01-23 17:30:05 +00:00
{
2022-01-25 20:57:22 +00:00
return new KeepFilenameFormatter(outputDirectory);
2022-01-23 17:30:05 +00:00
}
}
}