Files
FileFlowsPlugins/BasicNodes/File/MoveFile.cs
reven 6215d927d6 fixed issue with audio normalizing
fixed issue with moving files
2021-11-30 14:26:27 +13:00

74 lines
2.4 KiB
C#

namespace FileFlows.BasicNodes.File
{
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using FileFlows.Plugin;
using FileFlows.Plugin.Attributes;
public class MoveFile : Node
{
public override int Inputs => 1;
public override int Outputs => 1;
public override FlowElementType Type => FlowElementType.Process;
public override string Icon => "fas fa-file-export";
[Required]
[Folder(1)]
public string DestinationPath { get; set; }
[Boolean(2)]
public bool MoveFolder { get; set; }
[Boolean(3)]
public bool DeleteOriginal { get; set; }
public override int Execute(NodeParameters args)
{
string dest = args.ReplaceVariables(DestinationPath, true);
dest = dest.Replace("\\", Path.DirectorySeparatorChar.ToString());
dest = dest.Replace("/", Path.DirectorySeparatorChar.ToString());
if (string.IsNullOrEmpty(dest))
{
args.Logger?.ELog("No destination specified");
args.Result = NodeResult.Failure;
return -1;
}
args.Result = NodeResult.Failure;
if (MoveFolder)
dest = Path.Combine(dest, args.RelativeFile);
else
dest = Path.Combine(dest, new FileInfo(args.FileName).Name);
var fiDest = new FileInfo(dest);
var fiWorkingFile = new FileInfo(args.WorkingFile);
if (fiDest.Extension != fiWorkingFile.Extension)
{
dest = dest.Replace(fiDest.Extension, fiWorkingFile.Extension);
fiDest = new FileInfo(dest);
}
var destDir = fiDest.DirectoryName;
args.CreateDirectoryIfNotExists(destDir ?? String.Empty);
if (args.MoveFile(dest) == false)
return -1;
if (DeleteOriginal && args.FileName != args.WorkingFile)
{
args.Logger?.ILog("Deleting orginal file: " + args.FileName);
try
{
System.IO.File.Delete(args.FileName);
}
catch(Exception ex)
{
args.Logger?.WLog("Failed to delete original file: " + ex.Message);
}
}
return 1;
}
}
}