mirror of
https://github.com/Tichau/FileConverter.git
synced 2026-11-28 01:10:56 +01:00
Add input post conversion actions implementation.
This commit is contained in:
1 parent
12cfaaefc6
commit
96a2d4f52f
3 files changed
+80
-13
No files matched your search
@@ -12,6 +12,7 @@ namespace FileConverter.ConversionJobs
|
||||
private float progress = 0f;
|
||||
private ConversionState state = ConversionState.Unknown;
|
||||
private string errorMessage = string.Empty;
|
||||
private string initialInputPath = string.Empty;
|
||||
|
||||
public ConversionJob()
|
||||
{
|
||||
@@ -114,8 +115,10 @@ namespace FileConverter.ConversionJobs
|
||||
throw new Exception("The conversion preset must be valid.");
|
||||
}
|
||||
|
||||
this.initialInputPath = inputFilePath;
|
||||
this.InputFilePath = inputFilePath;
|
||||
this.OutputFilePath = this.ConversionPreset.GenerateOutputFilePath(inputFilePath);
|
||||
|
||||
if (!PathHelpers.IsPathValid(this.OutputFilePath))
|
||||
{
|
||||
this.ConvertionFailed("Invalid output path generated by output file path template.");
|
||||
@@ -123,7 +126,20 @@ namespace FileConverter.ConversionJobs
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if all output folders exists.
|
||||
if (this.OutputFilePath == this.InputFilePath)
|
||||
{
|
||||
// If the input post conversion action is to move or delete the input file, change its name in order to keep the output name intact.
|
||||
if (this.ConversionPreset.InputPostConversionAction == InputPostConversionAction.MoveInArchiveFolder ||
|
||||
this.ConversionPreset.InputPostConversionAction == InputPostConversionAction.Delete)
|
||||
{
|
||||
string inputExtension = System.IO.Path.GetExtension(this.InputFilePath);
|
||||
string pathWithoutExtension = this.InputFilePath.Substring(0, this.InputFilePath.Length - inputExtension.Length);
|
||||
this.InputFilePath = PathHelpers.GenerateUniquePath(pathWithoutExtension + "_TEMP" + inputExtension);
|
||||
System.IO.File.Move(inputFilePath, this.InputFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
// Create output folder that doesn't exist.
|
||||
{
|
||||
StringBuilder path = new StringBuilder(this.OutputFilePath.Length);
|
||||
foreach (string directory in PathHelpers.GetDirectories(this.OutputFilePath))
|
||||
@@ -147,16 +163,10 @@ namespace FileConverter.ConversionJobs
|
||||
}
|
||||
}
|
||||
|
||||
// Make the output path valid.
|
||||
try
|
||||
{
|
||||
string basePath = System.IO.Path.GetDirectoryName(this.OutputFilePath) + "\\" + System.IO.Path.GetFileNameWithoutExtension(this.OutputFilePath);
|
||||
string baseExtension = System.IO.Path.GetExtension(this.OutputFilePath);
|
||||
int index = 2;
|
||||
while (System.IO.File.Exists(this.OutputFilePath))
|
||||
{
|
||||
this.OutputFilePath = string.Format("{0} ({1}){2}", basePath, index, baseExtension);
|
||||
index++;
|
||||
}
|
||||
this.OutputFilePath = PathHelpers.GenerateUniquePath(this.OutputFilePath);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
@@ -193,10 +203,7 @@ namespace FileConverter.ConversionJobs
|
||||
|
||||
if (this.State != ConversionState.Failed)
|
||||
{
|
||||
// Convertion succeed !
|
||||
this.Progress = 1f;
|
||||
this.State = ConversionState.Done;
|
||||
Diagnostics.Log("\nDone!");
|
||||
this.OnConversionSucceed();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -208,6 +215,43 @@ namespace FileConverter.ConversionJobs
|
||||
{
|
||||
}
|
||||
|
||||
protected virtual void OnConversionSucceed()
|
||||
{
|
||||
Diagnostics.Log("Conversion Succeed!");
|
||||
|
||||
// Apply the input post conversion action.
|
||||
switch (this.ConversionPreset.InputPostConversionAction)
|
||||
{
|
||||
case InputPostConversionAction.None:
|
||||
break;
|
||||
|
||||
case InputPostConversionAction.MoveInArchiveFolder:
|
||||
string basePath = System.IO.Path.GetDirectoryName(this.initialInputPath);
|
||||
string inputFilename = System.IO.Path.GetFileName(this.initialInputPath);
|
||||
string archivePath = basePath + "\\" + this.ConversionPreset.ConversionArchiveFolderName;
|
||||
if (!System.IO.Directory.Exists(archivePath))
|
||||
{
|
||||
System.IO.Directory.CreateDirectory(archivePath);
|
||||
}
|
||||
|
||||
string newPath = PathHelpers.GenerateUniquePath(archivePath + "\\" + inputFilename);
|
||||
System.IO.File.Move(this.InputFilePath, newPath);
|
||||
Diagnostics.Log("Input file moved in archive folder: '{0}'", newPath);
|
||||
break;
|
||||
|
||||
case InputPostConversionAction.Delete:
|
||||
System.IO.File.Delete(this.InputFilePath);
|
||||
Diagnostics.Log("Input file deleted: '{0}'", this.initialInputPath);
|
||||
break;
|
||||
}
|
||||
|
||||
Diagnostics.Log(string.Empty);
|
||||
|
||||
this.Progress = 1f;
|
||||
this.State = ConversionState.Done;
|
||||
Diagnostics.Log("Conversion Done!");
|
||||
}
|
||||
|
||||
protected void ConvertionFailed(string exitingMessage)
|
||||
{
|
||||
this.State = ConversionState.Failed;
|
||||
|
||||
@@ -147,6 +147,15 @@ namespace FileConverter
|
||||
}
|
||||
}
|
||||
|
||||
[XmlIgnore]
|
||||
public string ConversionArchiveFolderName
|
||||
{
|
||||
get
|
||||
{
|
||||
return "Conversion Archives";
|
||||
}
|
||||
}
|
||||
|
||||
[XmlIgnore]
|
||||
public IConversionSettings Settings
|
||||
{
|
||||
|
||||
@@ -44,5 +44,19 @@ namespace FileConverter
|
||||
yield return matchGroup.Captures[index].Value;
|
||||
}
|
||||
}
|
||||
|
||||
public static string GenerateUniquePath(string path)
|
||||
{
|
||||
string baseExtension = System.IO.Path.GetExtension(path);
|
||||
string basePath = path.Substring(0, path.Length - baseExtension.Length);
|
||||
int index = 2;
|
||||
while (System.IO.File.Exists(path))
|
||||
{
|
||||
path = string.Format("{0} ({1}){2}", basePath, index, baseExtension);
|
||||
index++;
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user