mirror of
https://github.com/revenz/FileFlowsPlugins.git
synced 2025-12-31 06:19:31 -06:00
144 lines
5.0 KiB
C#
144 lines
5.0 KiB
C#
namespace FileFlows.BasicNodes.File
|
|
{
|
|
using System.ComponentModel.DataAnnotations;
|
|
using FileFlows.Plugin;
|
|
using FileFlows.Plugin.Attributes;
|
|
using FileFlows.Plugin.Helpers;
|
|
|
|
/// <summary>
|
|
/// Node that copies a file
|
|
/// </summary>
|
|
public class CopyFile : Node
|
|
{
|
|
/// <summary>
|
|
/// Gets the number of inputs
|
|
/// </summary>
|
|
public override int Inputs => 1;
|
|
|
|
/// <summary>
|
|
/// Gets the number of outputs
|
|
/// </summary>
|
|
public override int Outputs => 1;
|
|
|
|
/// <summary>
|
|
/// Gets the type of node
|
|
/// </summary>
|
|
public override FlowElementType Type => FlowElementType.Process;
|
|
|
|
/// <summary>
|
|
/// Gets the icon for this node
|
|
/// </summary>
|
|
public override string Icon => "far fa-copy";
|
|
|
|
/// <summary>
|
|
/// Gets the help URL
|
|
/// </summary>
|
|
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; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets if the original files creation and last write time dates should be preserved
|
|
/// </summary>
|
|
[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(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)
|
|
{
|
|
Helpers.FileHelper.SetCreationTime(dest, dtCreateTimeUtc);
|
|
Helpers.FileHelper.SetLastWriteTime(dest, dtLastWriteUtc);
|
|
}
|
|
|
|
var srcDir = AdditionalFilesFromOriginal ? new FileInfo(args.MapPath(args.FileName)).DirectoryName : new FileInfo(args.MapPath(args.WorkingFile)).DirectoryName;
|
|
|
|
if (AdditionalFiles?.Any() == true)
|
|
{
|
|
try
|
|
{
|
|
var diSrc = new DirectoryInfo(srcDir);
|
|
foreach (var additional in AdditionalFiles)
|
|
{
|
|
foreach (var addFile in diSrc.GetFiles(additional))
|
|
{
|
|
try
|
|
{
|
|
string addFileDest = Path.Combine(destDir, addFile.Name);
|
|
args.CopyFile(addFile.FullName, addFileDest, updateWorkingFile: false);
|
|
|
|
FileHelper.ChangeOwner(args.Logger, addFileDest, file: true);
|
|
|
|
args.Logger?.ILog("Copied file: \"" + addFile.FullName + "\" to \"" + addFileDest + "\"");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
args.Logger?.ILog("Failed copying file: \"" + addFile.FullName + "\": " + 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;
|
|
}
|
|
}
|
|
} |