using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.IO;
using FileFlows.Plugin;
using FileFlows.Plugin.Attributes;
using FileFlows.Plugin.Helpers;
namespace FileFlows.BasicNodes.File;
///
/// Flow element that moves a folder
///
public class MoveFolder : Node
{
///
public override int Inputs => 1;
///
public override int Outputs => 1;
///
public override FlowElementType Type => FlowElementType.Process;
///
public override string Icon => "fas fa-people-carry";
///
public override string HelpUrl => "https://fileflows.com/docs/plugins/basic-nodes/move-directory";
///
/// Gets or sets the source path to move
///
[Folder(1)]
public string SourcePath { get; set; }
///
/// Gets or sets the destination path
///
[Required]
[Folder(2)]
public string DestinationPath { get; set; }
///
/// Gets or sets a sub folder should be created
///
[Boolean(3)]
[DefaultValue(true)]
public bool CreateSubfolder { get; set; }
///
public override int Execute(NodeParameters args)
{
string source = args.ReplaceVariables(SourcePath ?? string.Empty)?.EmptyAsNull() ?? args.WorkingFile;
bool updateWorkingFolder = args.WorkingFile == source;
var existsResult = args.FileService.DirectoryExists(source);
if (existsResult.Failed(out var error))
{
args.FailureReason = error;
args.Logger?.ELog(error);
return -1;
}
if (existsResult.Value == false)
{
args.FailureReason = "Directory does not exists: " + source;
args.Logger?.ELog(args.FailureReason);
return -1;
}
var dest = args.ReplaceVariables(DestinationPath ?? string.Empty);
if (string.IsNullOrWhiteSpace(dest))
{
args.FailureReason = "No destination path set";
args.Logger?.ELog(args.FailureReason);
return -1;
}
if (CreateSubfolder)
{
var subfolder = new DirectoryInfo(source).Name;
args.Logger?.ILog("Creating sub folder: " + subfolder);
dest = FileHelper.Combine(dest, subfolder);
}
args.Logger?.ILog("Moving Directory: " + source);
args.Logger?.ILog("Destination Directory: " + dest);
var moveResult = args.FileService.DirectoryMove(source, dest);
if (moveResult.Failed(out error))
{
args.FailureReason = error;
args.Logger?.ELog(error);
return -1;
}
if (updateWorkingFolder)
{
args.Logger?.ILog("Updating working folder to: " + dest);
args.SetWorkingFile(dest, dontDelete: true);
}
args.Logger?.ILog("Directory moved");
return 1;
}
}