From 97aab5eab4dfa337ca57f01e7d94846899ea5939 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sun, 26 May 2019 22:41:10 +0700 Subject: [PATCH] Allow `@file.groovy` syntax in `Format Editor` and `Preset Editor` (e.g. `@/path/to/MyFormat.groovy`) --- CHANGES.md | 3 ++- .../net/filebot/format/ExpressionFilter.java | 2 +- .../net/filebot/format/ExpressionFormat.java | 21 ++++++++++++++++++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 19a8835e..8e9eb50b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,11 +1,12 @@ Next Release (4.8.6) ==================== -* Improved `SelectDialog` with thumbnails and tooltips +* Enhanced `Selection Dialog` with thumbnails and tooltips * Added `{history}` binding for looking up the original file path of `{f}` (e.g. useful for `-exec` post-processing commands) * Evaluate `{closures}` automatically in `String.plus(Closure)` constructs (e.g. `{"[" + {n} + " " + {s00e00} + "]"}`) * Ensure that `ActionPopup` is always displayed on top of the Windows Task Bar * Improved `-mediainfo -exec` pipeline * Added `-no-history` CLI option +* Allow `@file.groovy` syntax in `Format Editor` and `Preset Editor` (e.g. `@/path/to/MyFormat.groovy`) * Allow `*.groovy` files as argument value for `--format`, `--filter` and `--file-filter` CLI options (e.g. `--format /path/to/MyFormat.groovy`) diff --git a/source/net/filebot/format/ExpressionFilter.java b/source/net/filebot/format/ExpressionFilter.java index b74570dd..bf4bf8c7 100644 --- a/source/net/filebot/format/ExpressionFilter.java +++ b/source/net/filebot/format/ExpressionFilter.java @@ -21,7 +21,7 @@ public class ExpressionFilter { public ExpressionFilter(String expression) throws ScriptException { this.expression = expression; - this.compiledExpression = new SecureCompiledScript(compileScriptlet(expression)); + this.compiledExpression = new SecureCompiledScript(compileScriptlet(asExpression(expression))); } public String getExpression() { diff --git a/source/net/filebot/format/ExpressionFormat.java b/source/net/filebot/format/ExpressionFormat.java index 0f9a3dd6..a4d1723d 100644 --- a/source/net/filebot/format/ExpressionFormat.java +++ b/source/net/filebot/format/ExpressionFormat.java @@ -1,7 +1,9 @@ package net.filebot.format; import static net.filebot.util.ExceptionUtilities.*; +import static net.filebot.util.FileUtilities.*; +import java.io.File; import java.security.AccessController; import java.text.FieldPosition; import java.text.Format; @@ -39,7 +41,7 @@ public class ExpressionFormat extends Format { public ExpressionFormat(String expression) throws ScriptException { this.expression = expression; - this.compilation = secure(compile(expression)); + this.compilation = secure(compile(asExpression(expression))); } public String getExpression() { @@ -253,6 +255,23 @@ public class ExpressionFormat extends Format { } } + protected static String asExpression(String s) { + // try as file path + if (s.startsWith("@") && s.endsWith(".groovy")) { + File f = new File(s.substring(1)); + if (f.isFile()) { + try { + return readTextFile(f); + } catch (Exception e) { + throw new IllegalArgumentException("Failed to read text file: " + f, e); + } + } + } + + // or default to literal value + return s; + } + private static class Variable extends CompiledScript { private String name;