namespace FileFlows.BasicNodes.File
{
using System.ComponentModel.DataAnnotations;
using FileFlows.Plugin;
using FileFlows.Plugin.Attributes;
using FileFlows.Plugin.Helpers;
///
/// Node that copies a file
///
public class CopyFile : Node
{
///
/// Gets the number of inputs
///
public override int Inputs => 1;
///
/// Gets the number of outputs
///
public override int Outputs => 1;
///
/// Gets the type of node
///
public override FlowElementType Type => FlowElementType.Process;
///
/// Gets the icon for this node
///
public override string Icon => "far fa-copy";
///
/// Gets the help URL
///
public override string HelpUrl => "https://fileflows.com/docs/plugins/basic-nodes/copy-file";
private string _DestinationPath = string.Empty;
private string _DestinationFile = string.Empty;
[Required]
[Folder(1)]
public string DestinationPath
{
get => _DestinationPath;
set { _DestinationPath = value ?? ""; }
}
[TextVariable(2)]
public string DestinationFile
{
get => _DestinationFile;
set { _DestinationFile = value ?? ""; }
}
[Boolean(3)]
public bool CopyFolder { get; set; }
[StringArray(4)]
public string[] AdditionalFiles { get; set; }
[Boolean(5)]
public bool AdditionalFilesFromOriginal { get; set; }
///
/// Gets or sets if the original files creation and last write time dates should be preserved
///
[Boolean(6)]
public bool PreserverOriginalDates { get; set; }
private bool Canceled;
public override Task Cancel()
{
Canceled = true;
return base.Cancel();
}
public override int Execute(NodeParameters args)
{
Canceled = false;
var destParts = MoveFile.GetDestinationPathParts(args, DestinationPath, DestinationFile, CopyFolder);
if (destParts.Filename == null)
return -1;
// cant use new FileInfo(dest).Directory.Name here since
// if the folder is a linux folder and this node is running on windows
// /mnt, etc will be converted to c:\mnt and break the destination
var destDir = destParts.Path;
string dest = destParts.Path + destParts.Separator + destParts.Filename;
//bool copied = args.CopyFile(args.WorkingFile, dest, updateWorkingFile: true);
//if (!copied)
// return -1;
if (args.FileService.FileCopy(args.WorkingFile, dest, true).IsFailed)
return -1;
args.SetWorkingFile(dest);
if(PreserverOriginalDates && args.Variables.TryGetValue("ORIGINAL_CREATE_UTC", out object oCreateTimeUtc) &&
args.Variables.TryGetValue("ORIGINAL_LAST_WRITE_UTC", out object oLastWriteUtc) &&
oCreateTimeUtc is DateTime dtCreateTimeUtc && oLastWriteUtc is DateTime dtLastWriteUtc)
{
args.FileService.SetCreationTimeUtc(dest, dtCreateTimeUtc);
args.FileService.SetLastWriteTimeUtc(dest, dtLastWriteUtc);
//Helpers.FileHelper.SetCreationTime(dest, dtCreateTimeUtc);
//Helpers.FileHelper.SetLastWriteTime(dest, dtLastWriteUtc);
}
var srcDir = FileHelper.GetDirectory(AdditionalFilesFromOriginal
? args.MapPath(args.FileName)
: args.MapPath(args.WorkingFile));
if (AdditionalFiles?.Any() == true)
{
try
{
//var diSrc = new DirectoryInfo(srcDir);
foreach (var additional in AdditionalFiles)
{
//foreach (var addFile in diSrc.GetFiles(additional))
foreach(var addFile in args.FileService.GetFiles(additional).ValueOrDefault ?? new string[] {})
{
try
{
string shortName = FileHelper.GetShortFileName(addFile);
string addFileDest = destDir + args.FileService.PathSeparator + shortName;
args.FileService.FileCopy(addFile, addFileDest, true);
//args.CopyFile(addFile, addFileDest, updateWorkingFile: false);
//FileHelper.ChangeOwner(args.Logger, addFileDest, file: true);
args.Logger?.ILog("Copied file: \"" + addFile + "\" to \"" + addFileDest + "\"");
}
catch (Exception ex)
{
args.Logger?.ILog("Failed copying file: \"" + addFile + "\": " + ex.Message);
}
}
}
}
catch (Exception ex)
{
args.Logger.WLog("Error copying additional files: " + ex.Message);
}
}
// not needed as args.CopyFile does this
//args?.SetWorkingFile(dest);
return 1;
}
}
}