using System.IO;
using GuardNet;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
namespace Image
{
///
/// OriginalFilenameFileOutputPathFormatter keeps the original file name of the image when formatting the new output
/// path.
///
public class OriginalFilenameFileOutputPathFormatter : IFileOutputPathFormatter
{
public static ILogger Logger = NullLogger.Instance;
private readonly string _outputDirectory;
///
/// Creates an instance of OriginalFilenameFileOutputPathFormatter.
///
/// The output directory.
public OriginalFilenameFileOutputPathFormatter(string outputDirectory)
{
if (outputDirectory.Equals(""))
{
outputDirectory = ".";
}
_outputDirectory = outputDirectory;
}
///
/// Returns a path containing the file name in the output directory.
///
/// The initial path of the image.
/// An absolute path of the form output_directory/initialFileName.jpg
public string GetOutputPath(string initialFilePath)
{
Guard.NotNullOrEmpty(initialFilePath, nameof(initialFilePath));
var fileName = Path.GetFileName(initialFilePath)?.Split(".")[0];
var path = Path.Join(_outputDirectory, $"{fileName}.jpg");
return path;
}
///
/// Creates an instance of OriginalFilenameFileOutputPathFormatter.
///
/// The output directory.
public static OriginalFilenameFileOutputPathFormatter Create(string outputDirectory)
{
return new OriginalFilenameFileOutputPathFormatter(outputDirectory);
}
}
}