using FileFlows.Nextcloud.Helpers;
namespace FileFlows.Nextcloud.FlowElements;
///
/// Uploads a file to Nextcloud
///
public class UploadToNextcloud : Node
{
///
public override int Inputs => 1;
///
public override int Outputs => 1;
///
public override FlowElementType Type => FlowElementType.Process;
///
public override string CustomColor => "#0082c9";
///
public override string Icon => "svg:nextcloud";
///
public override string HelpUrl => "https://fileflows.com/docs/plugins/nextcloud/upload-to-next-cloud";
///
public override string Group => "Nextcloud";
///
public override LicenseLevel LicenseLevel => LicenseLevel.Standard;
///
/// Gets or sets the destination path
///
[Folder(1)]
public string DestinationPath { get; set; } = null!;
///
/// Gets or sets the file to unpack
///
[TextVariable(2)]
public string File { get; set; } = null!;
public override int Execute(NodeParameters args)
{
var settings = args.GetPluginSettings();
if (string.IsNullOrWhiteSpace(settings?.Url))
{
args.FailureReason = "No Nextcloud URL set";
args.Logger?.ELog(args.FailureReason);
return -1;
}
if (string.IsNullOrWhiteSpace(settings?.Username))
{
args.FailureReason = "No Nextcloud User set";
args.Logger?.ELog(args.FailureReason);
return -1;
}
if (string.IsNullOrWhiteSpace(settings?.Password))
{
args.FailureReason = "No Nextcloud Password set";
args.Logger?.ELog(args.FailureReason);
return -1;
}
var file = args.ReplaceVariables(File ?? string.Empty, stripMissing: true)?.EmptyAsNull() ?? args.WorkingFile;
var destination = args.ReplaceVariables(DestinationPath ?? string.Empty, stripMissing: true) ?? string.Empty;
destination = destination.TrimStart('/');
if (string.IsNullOrWhiteSpace(destination))
{
args.FailureReason = "No Destination set";
args.Logger?.ELog(args.FailureReason);
return -1;
}
var local = args.FileService.GetLocalPath(file);
if (local.Failed(out var error))
{
args.FailureReason = error;
args.Logger?.ELog(args.FailureReason);
return -1;
}
args.Logger?.ILog("File: " + local.Value);
args.Logger?.ILog("Destination: " + destination);
var uploader = GetUploader(args.Logger!, settings.Url, settings.Username, settings.Password);
var result = uploader.UploadFile(local.Value, destination);
if(result.Failed(out error))
{
args.FailureReason = error;
args.Logger?.ELog(args.FailureReason);
return -1;
}
args.Logger?.ILog("File successfully uploaded");
return 1;
}
///
/// The function to get the nextcloud uploader
///
private Func? _GetUploader;
///
/// Gets the function to get the next cloud uploader used to send a request
///
internal Func GetUploader
{
get
{
if(_GetUploader == null)
{
_GetUploader = (logger, url, username, password) =>
new NextcloudUploader(logger, url, username, password);
}
return _GetUploader;
}
#if(DEBUG)
set
{
_GetUploader = value;
}
#endif
}
}