mirror of
https://github.com/revenz/FileFlowsPlugins.git
synced 2026-02-17 15:18:36 -06:00
removed javascript executor out of basic nodes
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -3,9 +3,6 @@ namespace FileFlows.BasicNodes.Functions
|
||||
using System.ComponentModel;
|
||||
using FileFlows.Plugin;
|
||||
using FileFlows.Plugin.Attributes;
|
||||
using Jint.Runtime;
|
||||
using Jint.Native.Object;
|
||||
using Jint;
|
||||
using System.Text;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Text.RegularExpressions;
|
||||
@@ -37,51 +34,12 @@ namespace FileFlows.BasicNodes.Functions
|
||||
|
||||
try
|
||||
{
|
||||
long fileSize = 0;
|
||||
var fileInfo = new FileInfo(args.WorkingFile);
|
||||
if (fileInfo.Exists)
|
||||
fileSize = fileInfo.Length;
|
||||
|
||||
// replace Variables. with dictionary notation
|
||||
string tcode = Code;
|
||||
foreach (string k in args.Variables.Keys.OrderByDescending(x => x.Length))
|
||||
return args.ScriptExecutor.Execute(new FileFlows.Plugin.Models.ScriptExecutionArgs
|
||||
{
|
||||
// replace Variables.Key or Variables?.Key?.Subkey etc to just the variable
|
||||
// so Variables.file?.Orig.Name, will be replaced to Variables["file.Orig.Name"]
|
||||
// since its just a dictionary key value
|
||||
string keyRegex = @"Variables(\?)?\." + k.Replace(".", @"(\?)?\.");
|
||||
|
||||
|
||||
object? value = args.Variables[k];
|
||||
if (value is JsonElement jElement)
|
||||
{
|
||||
if (jElement.ValueKind == JsonValueKind.String)
|
||||
value = jElement.GetString();
|
||||
if (jElement.ValueKind == JsonValueKind.Number)
|
||||
value = jElement.GetInt64();
|
||||
}
|
||||
|
||||
tcode = Regex.Replace(tcode, keyRegex, "Variables['" + k + "']");
|
||||
}
|
||||
|
||||
var sb = new StringBuilder();
|
||||
var log = new
|
||||
{
|
||||
ILog = new LogDelegate(args.Logger.ILog),
|
||||
DLog = new LogDelegate(args.Logger.DLog),
|
||||
WLog = new LogDelegate(args.Logger.WLog),
|
||||
ELog = new LogDelegate(args.Logger.ELog),
|
||||
};
|
||||
var engine = new Engine(options =>
|
||||
{
|
||||
options.LimitMemory(4_000_000);
|
||||
options.MaxStatements(500);
|
||||
})
|
||||
.SetValue("Logger", args.Logger)
|
||||
.SetValue("Variables", args.Variables)
|
||||
.SetValue("Flow", args);
|
||||
var result = int.Parse(engine.Evaluate(tcode).ToObject().ToString());
|
||||
return result;
|
||||
Args = args,
|
||||
Code = Code
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -1,38 +1,35 @@
|
||||
namespace FileFlows.BasicNodes.Functions
|
||||
namespace FileFlows.BasicNodes.Functions;
|
||||
|
||||
using FileFlows.Plugin;
|
||||
using FileFlows.Plugin.Attributes;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
/// <summary>
|
||||
/// Node that logs a message to the Flow logger
|
||||
/// </summary>
|
||||
public class Log : Node
|
||||
{
|
||||
using System.ComponentModel;
|
||||
using FileFlows.Plugin;
|
||||
using FileFlows.Plugin.Attributes;
|
||||
using Jint.Runtime;
|
||||
using Jint.Native.Object;
|
||||
using Jint;
|
||||
using System.Text;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
public override int Inputs => 1;
|
||||
public override int Outputs => 1;
|
||||
public override FlowElementType Type => FlowElementType.Logic;
|
||||
public override string Icon => "far fa-file-alt";
|
||||
|
||||
public class Log : Node
|
||||
[Enum(1, LogType.Info, LogType.Debug, LogType.Warning, LogType.Error)]
|
||||
public LogType LogType { get; set; }
|
||||
|
||||
[TextArea(2)]
|
||||
[Required]
|
||||
public string Message { get; set; }
|
||||
public override int Execute(NodeParameters args)
|
||||
{
|
||||
public override int Inputs => 1;
|
||||
public override int Outputs => 1;
|
||||
public override FlowElementType Type => FlowElementType.Logic;
|
||||
public override string Icon => "far fa-file-alt";
|
||||
|
||||
[Enum(1, LogType.Info, LogType.Debug, LogType.Warning, LogType.Error)]
|
||||
public LogType LogType { get; set; }
|
||||
|
||||
[TextArea(2)]
|
||||
[Required]
|
||||
public string Message { get; set; }
|
||||
public override int Execute(NodeParameters args)
|
||||
switch (LogType)
|
||||
{
|
||||
switch (LogType)
|
||||
{
|
||||
case LogType.Error: args.Logger.ELog(Message); break;
|
||||
case LogType.Warning: args.Logger.WLog(Message); break;
|
||||
case LogType.Debug: args.Logger.DLog(Message); break;
|
||||
case LogType.Info: args.Logger.ILog(Message); break;
|
||||
}
|
||||
|
||||
return base.Execute(args);
|
||||
case LogType.Error: args.Logger.ELog(Message); break;
|
||||
case LogType.Warning: args.Logger.WLog(Message); break;
|
||||
case LogType.Debug: args.Logger.DLog(Message); break;
|
||||
case LogType.Info: args.Logger.ILog(Message); break;
|
||||
}
|
||||
|
||||
return base.Execute(args);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,125 +0,0 @@
|
||||
using FileFlows.Plugin;
|
||||
using Jint;
|
||||
using Jint.Runtime;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace BasicNodes;
|
||||
|
||||
/// <summary>
|
||||
/// A Javascript code executor
|
||||
/// </summary>
|
||||
internal class JavascriptExecutor
|
||||
{
|
||||
/// <summary>
|
||||
/// Delegate used by the executor so log messages can be passed from the javascript code into the flow runner
|
||||
/// </summary>
|
||||
/// <param name="values">the parameters for the logger</param>
|
||||
delegate void LogDelegate(params object[] values);
|
||||
|
||||
/// <summary>
|
||||
/// Executes javascript
|
||||
/// </summary>
|
||||
/// <param name="execArgs">the execution arguments</param>
|
||||
/// <returns>the output to be called next</returns>
|
||||
public static int Execute(JavascriptExecutionArgs execArgs)
|
||||
{
|
||||
if (string.IsNullOrEmpty(execArgs?.Code))
|
||||
return -1; // no code, flow cannot continue doesnt know what to do
|
||||
var args = execArgs.Args;
|
||||
try
|
||||
{
|
||||
long fileSize = 0;
|
||||
var fileInfo = new FileInfo(args.WorkingFile);
|
||||
if (fileInfo.Exists)
|
||||
fileSize = fileInfo.Length;
|
||||
|
||||
// replace Variables. with dictionary notation
|
||||
string tcode = execArgs.Code;
|
||||
foreach (string k in args.Variables.Keys.OrderByDescending(x => x.Length))
|
||||
{
|
||||
// replace Variables.Key or Variables?.Key?.Subkey etc to just the variable
|
||||
// so Variables.file?.Orig.Name, will be replaced to Variables["file.Orig.Name"]
|
||||
// since its just a dictionary key value
|
||||
string keyRegex = @"Variables(\?)?\." + k.Replace(".", @"(\?)?\.");
|
||||
|
||||
|
||||
object? value = args.Variables[k];
|
||||
if (value is JsonElement jElement)
|
||||
{
|
||||
if (jElement.ValueKind == JsonValueKind.String)
|
||||
value = jElement.GetString();
|
||||
if (jElement.ValueKind == JsonValueKind.Number)
|
||||
value = jElement.GetInt64();
|
||||
}
|
||||
|
||||
tcode = Regex.Replace(tcode, keyRegex, "Variables['" + k + "']");
|
||||
}
|
||||
|
||||
var sb = new StringBuilder();
|
||||
var log = new
|
||||
{
|
||||
ILog = new LogDelegate(args.Logger.ILog),
|
||||
DLog = new LogDelegate(args.Logger.DLog),
|
||||
WLog = new LogDelegate(args.Logger.WLog),
|
||||
ELog = new LogDelegate(args.Logger.ELog),
|
||||
};
|
||||
var engine = new Engine(options =>
|
||||
{
|
||||
options.LimitMemory(4_000_000);
|
||||
options.MaxStatements(500);
|
||||
})
|
||||
.SetValue("Logger", args.Logger)
|
||||
.SetValue("Variables", args.Variables)
|
||||
.SetValue("Flow", args)
|
||||
.SetValue(nameof(FileInfo), new Func<string, FileInfo>((string file) => new FileInfo(file)))
|
||||
.SetValue(nameof(DirectoryInfo), new Func<string, DirectoryInfo>((string path) => new DirectoryInfo(path))); ;
|
||||
foreach (var arg in execArgs.AdditionalArguments)
|
||||
engine.SetValue(arg.Key, arg.Value);
|
||||
|
||||
var result = int.Parse(engine.Evaluate(tcode).ToObject().ToString());
|
||||
return result;
|
||||
}
|
||||
catch(JavaScriptException ex)
|
||||
{
|
||||
// print out the code block for debugging
|
||||
int lineNumber = 0;
|
||||
var lines = execArgs.Code.Split('\n');
|
||||
string pad = "D" + (lines.ToString().Length);
|
||||
args.Logger.DLog("Code: " + Environment.NewLine +
|
||||
string.Join("\n", lines.Select(x => (++lineNumber).ToString("D3") + ": " + x)));
|
||||
|
||||
args.Logger?.ELog($"Failed executing script [{ex.LineNumber}, {ex.Column}]: {ex.Message}");
|
||||
return -1;
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
args.Logger?.ELog("Failed executing function: " + ex.Message + Environment.NewLine + ex.StackTrace);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The arguments to pass to the javascript executor
|
||||
/// </summary>
|
||||
public class JavascriptExecutionArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the code to execute
|
||||
/// </summary>
|
||||
public string Code { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets teh NodeParameters
|
||||
/// </summary>
|
||||
public NodeParameters Args { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collection of additional arguments to be passed to the javascript executor
|
||||
/// </summary>
|
||||
public Dictionary<string, object> AdditionalArguments { get; private set; } = new Dictionary<string, object>();
|
||||
}
|
||||
@@ -42,7 +42,7 @@ public class ScriptNode:Node
|
||||
// all scripts must contain the "Script" method we then add this to call that
|
||||
string entryPoint = $"Script({epParams});";
|
||||
|
||||
var execArgs = new JavascriptExecutionArgs
|
||||
var execArgs = new FileFlows.Plugin.Models.ScriptExecutionArgs
|
||||
{
|
||||
Args = args,
|
||||
//Code = ("try\n{\n\t" + Code.Replace("\n", "\n\t") + "\n\n\t" + entryPoint + "\n} catch (err) { \n\tLogger.ELog(`Error in script [${err.line}]: ${err}`);\n\treturn -1;\n}").Replace("\t", " ")
|
||||
@@ -59,6 +59,6 @@ public class ScriptNode:Node
|
||||
}
|
||||
}
|
||||
|
||||
return JavascriptExecutor.Execute(execArgs);
|
||||
return args.ScriptExecutor.Execute(execArgs);
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Emby/Emby.csproj
BIN
Emby/Emby.csproj
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Plex/Plex.csproj
BIN
Plex/Plex.csproj
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -382,6 +382,25 @@
|
||||
Gets or sets the local equivalent path on the node
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:FileFlows.ServerShared.ScriptExecution.ScriptExecutor">
|
||||
<summary>
|
||||
A Javascript code executor
|
||||
</summary>
|
||||
|
||||
</member>
|
||||
<member name="T:FileFlows.ServerShared.ScriptExecution.ScriptExecutor.LogDelegate">
|
||||
<summary>
|
||||
Delegate used by the executor so log messages can be passed from the javascript code into the flow runner
|
||||
</summary>
|
||||
<param name="values">the parameters for the logger</param>
|
||||
</member>
|
||||
<member name="M:FileFlows.ServerShared.ScriptExecution.ScriptExecutor.Execute(FileFlows.Plugin.Models.ScriptExecutionArgs)">
|
||||
<summary>
|
||||
Executes javascript
|
||||
</summary>
|
||||
<param name="execArgs">the execution arguments</param>
|
||||
<returns>the output to be called next</returns>
|
||||
</member>
|
||||
<member name="T:FileFlows.ServerShared.Services.IFlowRunnerService">
|
||||
<summary>
|
||||
Interface for a Flow Runner, which is responsible for executing a flow and processing files
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -16,6 +16,25 @@
|
||||
"PluginInfoGenerator.dll": {}
|
||||
}
|
||||
},
|
||||
"Esprima/2.1.2": {
|
||||
"runtime": {
|
||||
"lib/netstandard2.1/Esprima.dll": {
|
||||
"assemblyVersion": "2.1.2.0",
|
||||
"fileVersion": "2.1.2.0"
|
||||
}
|
||||
}
|
||||
},
|
||||
"Jint/3.0.0-beta-2038": {
|
||||
"dependencies": {
|
||||
"Esprima": "2.1.2"
|
||||
},
|
||||
"runtime": {
|
||||
"lib/netstandard2.1/Jint.dll": {
|
||||
"assemblyVersion": "3.0.0.0",
|
||||
"fileVersion": "3.0.0.0"
|
||||
}
|
||||
}
|
||||
},
|
||||
"MessageFormat/5.0.1": {
|
||||
"dependencies": {
|
||||
"Microsoft.Extensions.ObjectPool": "5.0.5"
|
||||
@@ -43,7 +62,8 @@
|
||||
"FileFlows.ServerShared/1.0.0": {
|
||||
"dependencies": {
|
||||
"FileFlows.Plugin": "1.0.0",
|
||||
"FileFlows.Shared": "1.0.0"
|
||||
"FileFlows.Shared": "1.0.0",
|
||||
"Jint": "3.0.0-beta-2038"
|
||||
},
|
||||
"runtime": {
|
||||
"FileFlows.ServerShared.dll": {}
|
||||
@@ -66,6 +86,20 @@
|
||||
"serviceable": false,
|
||||
"sha512": ""
|
||||
},
|
||||
"Esprima/2.1.2": {
|
||||
"type": "package",
|
||||
"serviceable": true,
|
||||
"sha512": "sha512-JAiCTRsb90Rt7Yhk39mfS9KF7Eb7CxMMJ6/n9H4LRBfYlAmq9FI3oxmb0iNxtME4EhV6oQx1F/IsvDtGYwOHqg==",
|
||||
"path": "esprima/2.1.2",
|
||||
"hashPath": "esprima.2.1.2.nupkg.sha512"
|
||||
},
|
||||
"Jint/3.0.0-beta-2038": {
|
||||
"type": "package",
|
||||
"serviceable": true,
|
||||
"sha512": "sha512-RNCrRzThi4nTum4EQ/v8deKyZwySv5vt0HEzGjI7Nru2b+gppDx0UsU7p5RN75P2EQjl7lwxmXtW1zVN04aT3A==",
|
||||
"path": "jint/3.0.0-beta-2038",
|
||||
"hashPath": "jint.3.0.0-beta-2038.nupkg.sha512"
|
||||
},
|
||||
"MessageFormat/5.0.1": {
|
||||
"type": "package",
|
||||
"serviceable": true,
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user