mirror of
https://github.com/Tichau/FileConverter.git
synced 2026-11-21 10:01:58 +01:00
Add possibility to output multiple files in a conversion job.
That allows 1 to N files conversions.
This commit is contained in:
1 parent
d029311217
commit
fdc16468cc
3 files changed
+173
-77
No files matched your search
@@ -467,15 +467,6 @@ namespace FileConverter
|
||||
{
|
||||
Debug.LogError("Failure during conversion: {0}", exception.ToString());
|
||||
}
|
||||
|
||||
if (conversionJob.State == ConversionJob.ConversionState.Done && !System.IO.File.Exists(conversionJob.OutputFilePath))
|
||||
{
|
||||
Debug.LogError("Can't find the output file.");
|
||||
}
|
||||
else if (conversionJob.State == ConversionJob.ConversionState.Failed && System.IO.File.Exists(conversionJob.OutputFilePath))
|
||||
{
|
||||
Debug.Log("The conversion job failed but there is an output file that does exists.");
|
||||
}
|
||||
}
|
||||
|
||||
private void OnGetLatestVersionDescription(UpgradeVersionDescription upgradeVersionDescription)
|
||||
|
||||
@@ -15,10 +15,13 @@ namespace FileConverter.ConversionJobs
|
||||
private float progress = 0f;
|
||||
private ConversionState state = ConversionState.Unknown;
|
||||
private string errorMessage = string.Empty;
|
||||
private string initialInputPath = string.Empty;
|
||||
private string userState = string.Empty;
|
||||
private CancelConversionJobCommand cancelCommand;
|
||||
|
||||
private string initialInputPath = string.Empty;
|
||||
private string[] outputFilePaths;
|
||||
private int currentOuputFilePathIndex;
|
||||
|
||||
public ConversionJob()
|
||||
{
|
||||
this.State = ConversionState.Unknown;
|
||||
@@ -62,10 +65,41 @@ namespace FileConverter.ConversionJobs
|
||||
|
||||
public string OutputFilePath
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
get
|
||||
{
|
||||
if (this.outputFilePaths == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
if (this.CurrentOuputFilePathIndex < 0)
|
||||
{
|
||||
return this.outputFilePaths[0];
|
||||
}
|
||||
|
||||
if (this.CurrentOuputFilePathIndex >= this.outputFilePaths.Length)
|
||||
{
|
||||
return this.outputFilePaths[this.outputFilePaths.Length - 1];
|
||||
}
|
||||
|
||||
return this.outputFilePaths[this.CurrentOuputFilePathIndex];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected int CurrentOuputFilePathIndex
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.currentOuputFilePathIndex;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
this.currentOuputFilePathIndex = value;
|
||||
this.NotifyPropertyChanged(nameof(this.OutputFilePath));
|
||||
}
|
||||
}
|
||||
|
||||
public ConversionState State
|
||||
{
|
||||
get
|
||||
@@ -175,7 +209,7 @@ namespace FileConverter.ConversionJobs
|
||||
{
|
||||
if (string.IsNullOrEmpty(inputFilePath))
|
||||
{
|
||||
throw new ArgumentNullException("inputFilePath");
|
||||
throw new ArgumentNullException(nameof(inputFilePath));
|
||||
}
|
||||
|
||||
if (this.ConversionPreset == null)
|
||||
@@ -194,66 +228,57 @@ namespace FileConverter.ConversionJobs
|
||||
|
||||
this.initialInputPath = inputFilePath;
|
||||
this.InputFilePath = inputFilePath;
|
||||
this.OutputFilePath = outputFilePath ?? this.ConversionPreset.GenerateOutputFilePath(inputFilePath);
|
||||
|
||||
if (!PathHelpers.IsPathValid(this.OutputFilePath))
|
||||
{
|
||||
this.ConversionFailed("Invalid output path generated by output file path template.");
|
||||
Debug.Log(string.Format("Invalid output path generated: {0} from input: {1}.", this.OutputFilePath, this.InputFilePath));
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.OutputFilePath == this.InputFilePath)
|
||||
int outputFilesCount = this.GetOuputFilesCount();
|
||||
this.outputFilePaths = new string[outputFilesCount];
|
||||
|
||||
for (int index = 0; index < outputFilesCount; index++)
|
||||
{
|
||||
// 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 path = outputFilePath ?? this.ConversionPreset.GenerateOutputFilePath(inputFilePath, index + 1, outputFilesCount);
|
||||
|
||||
if (!PathHelpers.IsPathValid(path))
|
||||
{
|
||||
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);
|
||||
this.ConversionFailed("Invalid output path generated by output file path template.");
|
||||
Debug.Log($"Invalid output path generated: {path} from input: {this.InputFilePath}.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Create output folder that doesn't exist.
|
||||
{
|
||||
StringBuilder path = new StringBuilder(this.OutputFilePath.Length);
|
||||
string drive = PathHelpers.GetDrive(this.OutputFilePath);
|
||||
path.Append(drive);
|
||||
|
||||
foreach (string directory in PathHelpers.GetDirectories(this.OutputFilePath))
|
||||
if (path == this.InputFilePath)
|
||||
{
|
||||
path.Append(directory);
|
||||
path.Append('\\');
|
||||
|
||||
if (!System.IO.Directory.Exists(path.ToString()))
|
||||
// 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)
|
||||
{
|
||||
try
|
||||
{
|
||||
System.IO.Directory.CreateDirectory(path.ToString());
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
this.ConversionFailed("Invalid output path generated by output file path template.");
|
||||
Debug.Log(string.Format("Can't create directories for path {0}", this.OutputFilePath));
|
||||
return;
|
||||
}
|
||||
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 folders that doesn't exist.
|
||||
if (!PathHelpers.CreateFolders(path))
|
||||
{
|
||||
this.ConversionFailed("Invalid output path generated by output file path template.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Make the output path valid.
|
||||
try
|
||||
{
|
||||
path = PathHelpers.GenerateUniquePath(path, this.outputFilePaths);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
this.ConversionFailed("Can't generate a valid output filename.");
|
||||
Debug.Log(exception.Message);
|
||||
return;
|
||||
}
|
||||
|
||||
this.outputFilePaths[index] = path;
|
||||
}
|
||||
|
||||
// Make the output path valid.
|
||||
try
|
||||
{
|
||||
this.OutputFilePath = PathHelpers.GenerateUniquePath(this.OutputFilePath);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
this.ConversionFailed("Can't generate a valid output filename.");
|
||||
Debug.Log(exception.Message);
|
||||
return;
|
||||
}
|
||||
this.CurrentOuputFilePathIndex = 0;
|
||||
|
||||
// Check if the input file is located on a cd drive.
|
||||
if (PathHelpers.IsOnCDDrive(this.InputFilePath))
|
||||
@@ -270,7 +295,7 @@ namespace FileConverter.ConversionJobs
|
||||
|
||||
Debug.Log("Job initialized: Preset: '{0}' Input: {1} Output: {2}", this.ConversionPreset.Name, this.InputFilePath, this.OutputFilePath);
|
||||
|
||||
this.UserState = "In Queue";
|
||||
this.UserState = Properties.Resources.ConversionStateInQueue;
|
||||
}
|
||||
|
||||
public void StartConvertion()
|
||||
@@ -308,6 +333,15 @@ namespace FileConverter.ConversionJobs
|
||||
{
|
||||
this.OnConversionSucceed();
|
||||
}
|
||||
|
||||
if (this.State == ConversionJob.ConversionState.Done && !this.AllOuputFilesExists())
|
||||
{
|
||||
Debug.LogError("Can't find the output file(s).");
|
||||
}
|
||||
else if (this.State == ConversionJob.ConversionState.Failed && this.AtLeastOneOuputFilesExists())
|
||||
{
|
||||
Debug.Log("The conversion job failed but there is an output file that does exists.");
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Cancel()
|
||||
@@ -321,6 +355,11 @@ namespace FileConverter.ConversionJobs
|
||||
this.ConversionFailed("Canceled.");
|
||||
}
|
||||
|
||||
protected virtual int GetOuputFilesCount()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
protected virtual void Convert()
|
||||
{
|
||||
}
|
||||
@@ -333,17 +372,21 @@ namespace FileConverter.ConversionJobs
|
||||
{
|
||||
Debug.Log("Conversion Failed.");
|
||||
|
||||
try
|
||||
for (int index = 0; index < this.outputFilePaths.Length; index++)
|
||||
{
|
||||
if (System.IO.File.Exists(this.OutputFilePath))
|
||||
string outputFilePath = this.outputFilePaths[index];
|
||||
try
|
||||
{
|
||||
System.IO.File.Delete(this.OutputFilePath);
|
||||
if (System.IO.File.Exists(outputFilePath))
|
||||
{
|
||||
System.IO.File.Delete(outputFilePath);
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Debug.Log("Can't delete file '{0}' after conversion job failure.", outputFilePath);
|
||||
Debug.Log("An exception as been thrown: {0}.", exception.ToString());
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Debug.Log("Can't delete file '{0}' after conversion job failure.", this.OutputFilePath);
|
||||
Debug.Log("An exception as been thrown: {0}.", exception.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -381,7 +424,7 @@ namespace FileConverter.ConversionJobs
|
||||
|
||||
this.Progress = 1f;
|
||||
this.State = ConversionState.Done;
|
||||
this.UserState = "Done";
|
||||
this.UserState = Properties.Resources.ConversionStateDone;
|
||||
Debug.Log("Conversion Done!");
|
||||
}
|
||||
|
||||
@@ -396,7 +439,7 @@ namespace FileConverter.ConversionJobs
|
||||
}
|
||||
|
||||
this.State = ConversionState.Failed;
|
||||
this.UserState = "Failed";
|
||||
this.UserState = Properties.Resources.ConversionStateFailed;
|
||||
this.ErrorMessage = exitingMessage;
|
||||
}
|
||||
|
||||
@@ -407,5 +450,33 @@ namespace FileConverter.ConversionJobs
|
||||
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
}
|
||||
|
||||
private bool AllOuputFilesExists()
|
||||
{
|
||||
for (int index = 0; index < this.outputFilePaths.Length; index++)
|
||||
{
|
||||
string outputFilePath = this.outputFilePaths[index];
|
||||
if (!System.IO.File.Exists(outputFilePath))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool AtLeastOneOuputFilesExists()
|
||||
{
|
||||
for (int index = 0; index < this.outputFilePaths.Length; index++)
|
||||
{
|
||||
string outputFilePath = this.outputFilePaths[index];
|
||||
if (System.IO.File.Exists(outputFilePath))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,13 @@
|
||||
|
||||
namespace FileConverter
|
||||
{
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
using FileConverter.Diagnostics;
|
||||
|
||||
public static class PathHelpers
|
||||
{
|
||||
private static Regex driveLetterRegex = new Regex(@"[a-zA-Z]:\\");
|
||||
@@ -90,20 +94,50 @@ namespace FileConverter
|
||||
}
|
||||
}
|
||||
|
||||
public static string GenerateUniquePath(string path)
|
||||
public static string GenerateUniquePath(string path, params string[] blacklist)
|
||||
{
|
||||
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))
|
||||
while (System.IO.File.Exists(path) ||
|
||||
(blacklist != null && System.Array.Exists(blacklist, match => match == path)))
|
||||
{
|
||||
path = string.Format("{0} ({1}){2}", basePath, index, baseExtension);
|
||||
path = $"{basePath} ({index}){baseExtension}";
|
||||
index++;
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
public static bool CreateFolders(string filePath)
|
||||
{
|
||||
// Create output folders that doesn't already exist.
|
||||
StringBuilder path = new StringBuilder(filePath.Length);
|
||||
string drive = PathHelpers.GetDrive(filePath);
|
||||
path.Append(drive);
|
||||
|
||||
foreach (string directory in PathHelpers.GetDirectories(filePath))
|
||||
{
|
||||
path.Append(directory);
|
||||
path.Append('\\');
|
||||
|
||||
if (!System.IO.Directory.Exists(path.ToString()))
|
||||
{
|
||||
try
|
||||
{
|
||||
System.IO.Directory.CreateDirectory(path.ToString());
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Debug.Log(string.Format("Can't create directories for path {0}", filePath));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static string GetUserDataFolderPath()
|
||||
{
|
||||
string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData);
|
||||
|
||||
Reference in new issue
Block a user