mirror of
https://github.com/revenz/FileFlowsPlugins.git
synced 2025-12-31 13:40:16 -06:00
89 lines
3.0 KiB
C#
89 lines
3.0 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; }
|
|
|
|
[TextVariable(2)]
|
|
public string DestinationFile{ get; set; }
|
|
|
|
[Boolean(3)]
|
|
public bool MoveFolder { get; set; }
|
|
|
|
[Boolean(4)]
|
|
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 fiWorking = new FileInfo(args.WorkingFile);
|
|
if (string.IsNullOrEmpty(fiDest.Extension) == false && fiDest.Extension != fiWorking.Extension)
|
|
{
|
|
dest = dest.Substring(0, dest.LastIndexOf(".")) + fiWorking.Extension;
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(DestinationFile) == false)
|
|
{
|
|
string destFile = args.ReplaceVariables(DestinationFile);
|
|
dest = Path.Combine(new FileInfo(dest).DirectoryName!, destFile);
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
} |