added support for variables in nodes, and crated Renamer in basic nodes which can use variables to rename a file

This commit is contained in:
reven
2021-11-23 22:26:15 +13:00
parent 761b82dbff
commit 962fe7959c
21 changed files with 313 additions and 38 deletions
Binary file not shown.
+11 -7
View File
@@ -38,15 +38,15 @@
}
},
"FileSize": {
"Description": "Checks if the file size matches the configured parameters.\n\nOutput 1: Matches\nOutput 2: Does not match",
"Description": "Checks if the file size matches the configured parameters. The values are in megabytes.\n\nOutput 1: Matches\nOutput 2: Does not match",
"Fields": {
"Comparison": "Comparison",
"Lower": "Lower",
"Lower-Suffix": "MB",
"Lower-Help": "The value it must bet greater than",
"Lower-Help": "The value it must be greater than this number of megabytes",
"Upper": "Upper",
"Upper-Suffix": "MB",
"Upper-Help": "The value it must be less than. Leave as 0 to not test the upper limit."
"Upper-Help": "The value it must be less than than this number of megabytes. Leave as 0 to not test the upper limit."
}
},
"Function": {
@@ -82,11 +82,15 @@
"Pattern-Help": "A regular expression, using the C# specifciation for regular expressions."
}
},
"RenameFile": {
"Description": "Renames the working file",
"Renamer": {
"Description": "Renames the working file.\nVariables can be used by entering the key '{' inside the Pattern field.",
"Fields": {
"FileName": "File Name",
"FileName-Help": "The new filename"
"Pattern": "Pattern",
"Pattern-Help": "The pattern to use to rename the folder. Variables can be used, for example '{FileName}.{ext}'.",
"DestinationPath": "Destination Path",
"DestinationPath-Help": "If the file should be moved to a different directory.",
"LogOnly": "Log Only",
"LogOnly-Help": "Turn on if you just want to test this node without it actually renaming the file"
}
}
}
+80
View File
@@ -0,0 +1,80 @@
namespace FileFlows.BasicNodes.File
{
using System.Text.RegularExpressions;
using FileFlows.Plugin;
using FileFlows.Plugin.Attributes;
public class Renamer : Node
{
public override int Inputs => 1;
public override int Outputs => 1;
public override string Icon => "fas fa-font";
public string _Pattern = string.Empty;
public override FlowElementType Type => FlowElementType.Process;
[TextVariable(1)]
public string? Pattern
{
get => _Pattern;
set { _Pattern = value ?? ""; }
}
private string _DestinationPath = string.Empty;
[Folder(2)]
public string DestinationPath
{
get => _DestinationPath;
set { _DestinationPath = value ?? ""; }
}
[Boolean(3)]
public bool LogOnly { get; set; }
public override int Execute(NodeParameters args)
{
if(string.IsNullOrEmpty(Pattern))
{
args.Logger?.ELog("No pattern specified");
return -1;
}
string newFile = Pattern;
// incase they set a linux path on windows or vice versa
newFile = newFile.Replace('\\', Path.DirectorySeparatorChar);
newFile = newFile.Replace('/', Path.DirectorySeparatorChar);
if (args.Variables?.Any() == true)
{
{
foreach (string variable in args.Variables.Keys)
{
string strValue = args.Variables[variable]?.ToString() ?? "";
newFile = ReplaceVariable(newFile, variable, strValue);
}
}
}
string destFolder = DestinationPath;
if (string.IsNullOrEmpty(destFolder))
destFolder = new FileInfo(args.WorkingFile).Directory?.FullName ?? "";
var dest = new FileInfo(Path.Combine(destFolder, newFile));
args.Logger?.ILog("Renaming file to: " + (string.IsNullOrEmpty(DestinationPath) ? "" : DestinationPath + Path.DirectorySeparatorChar) + newFile);
if (LogOnly)
return 1;
return args.MoveFile(dest.FullName) ? 1 : -1;
}
private string ReplaceVariable(string input, string variable, string value)
{
return Regex.Replace(input, @"{" + Regex.Escape(variable) + @"}", value, RegexOptions.IgnoreCase);
}
}
}
+40
View File
@@ -0,0 +1,40 @@
#if(DEBUG)
namespace BasicNodes.Tests
{
using FileFlows.BasicNodes.File;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class RenamerTests
{
[TestMethod]
public void Renamer_Extension()
{
var args = new FileFlows.Plugin.NodeParameters(@"c:\test\testfile.mkv");
var logger = new TestLogger();
args.Logger = logger;
args.Variables = new Dictionary<string, object>
{
{ "mi.Title", "Ghostbusters" },
{ "mi.Year", 1984 },
{ "vi.Resolution", "1080P" }
};
args.SetWorkingFile($@"c:\temp\{Guid.NewGuid()}.mkv", dontDelete: true);
Renamer node = new Renamer();
node.Pattern = @"{mi.Title} ({mi.Year})\{mi.Title} [{vi.Resolution}].{ext}";
node.LogOnly = true;
var result = node.Execute(args);
Assert.AreEqual(1, result);
string expectedShort = $"Ghostbusters (1984){Path.DirectorySeparatorChar}Ghostbusters [1080P].mkv";
Assert.IsTrue(logger.Contains($"Renaming file to: " + expectedShort));
}
}
}
#endif
+29 -11
View File
@@ -1,4 +1,6 @@
namespace BasicNodes.Tests
#if (DEBUG)
namespace BasicNodes.Tests
{
using FileFlows.Plugin;
using System;
@@ -9,21 +11,37 @@
internal class TestLogger : ILogger
{
public void DLog(params object[] args)
private List<string> Messages = new List<string>();
public void DLog(params object[] args) => Log("DBUG", args);
public void ELog(params object[] args) => Log("ERRR", args);
public void ILog(params object[] args) => Log("INFO", args);
public void WLog(params object[] args) => Log("WARN", args);
private void Log(string type, object[] args)
{
if (args == null || args.Length == 0)
return;
string message = type + " -> " +
string.Join(", ", args.Select(x =>
x == null ? "null" :
x.GetType().IsPrimitive || x is string ? x.ToString() :
System.Text.Json.JsonSerializer.Serialize(x)));
Messages.Add(message);
}
public void ELog(params object[] args)
{
}
public void ILog(params object[] args)
{
}
public void WLog(params object[] args)
public bool Contains(string message)
{
if (string.IsNullOrWhiteSpace(message))
return false;
string log = string.Join(Environment.NewLine, Messages);
return log.Contains(message);
}
}
}
#endif