From 8f4ec6511f66c11f8e135948840b8eec9c04dd27 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Tue, 18 Apr 2017 17:15:59 +0800 Subject: [PATCH] Support filebot -mediainfo -exec (similar to find -exec) --- source/net/filebot/cli/ArgumentProcessor.java | 5 ++++ source/net/filebot/cli/CmdlineInterface.java | 2 ++ source/net/filebot/cli/CmdlineOperations.java | 25 +++++++++++++++---- source/net/filebot/cli/ExecCommand.java | 2 +- 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/source/net/filebot/cli/ArgumentProcessor.java b/source/net/filebot/cli/ArgumentProcessor.java index 50d6f470..b2dea30b 100644 --- a/source/net/filebot/cli/ArgumentProcessor.java +++ b/source/net/filebot/cli/ArgumentProcessor.java @@ -63,6 +63,11 @@ public class ArgumentProcessor { return print(cli.fetchEpisodeList(args.getEpisodeListProvider(), args.getSearchQuery(), args.getExpressionFormat(), args.getExpressionFilter(), args.getSortOrder(), args.getLanguage().getLocale(), args.isStrict())); } + // execute command for each file + if (args.mediaInfo && args.getExecCommand() != null) { + return cli.execute(args.getFiles(true), args.getFileFilter(), args.getExecCommand()) ? 0 : 1; + } + // print media info if (args.mediaInfo) { return print(cli.getMediaInfo(args.getFiles(true), args.getFileFilter(), args.getExpressionFormat())); diff --git a/source/net/filebot/cli/CmdlineInterface.java b/source/net/filebot/cli/CmdlineInterface.java index c463bf7b..8a58aca9 100644 --- a/source/net/filebot/cli/CmdlineInterface.java +++ b/source/net/filebot/cli/CmdlineInterface.java @@ -43,6 +43,8 @@ public interface CmdlineInterface { Stream getMediaInfo(Collection files, FileFilter filter, ExpressionFormat format) throws Exception; + boolean execute(Collection files, FileFilter filter, ExecCommand exec) throws Exception; + List extract(Collection files, File output, ConflictAction conflict, FileFilter filter, boolean forceExtractAll) throws Exception; } diff --git a/source/net/filebot/cli/CmdlineOperations.java b/source/net/filebot/cli/CmdlineOperations.java index b8392f4b..1d2f850c 100644 --- a/source/net/filebot/cli/CmdlineOperations.java +++ b/source/net/filebot/cli/CmdlineOperations.java @@ -649,13 +649,9 @@ public class CmdlineOperations implements CmdlineInterface { // execute command if (exec != null) { - Map context = renameLog.values().stream().filter(Objects::nonNull).collect(toMap(f -> f, f -> xattr.getMetaInfo(f), (a, b) -> a, LinkedHashMap::new)); - if (context.size() > 0) { - exec.execute(context.entrySet().stream().map(m -> new MediaBindingBean(m.getValue(), m.getKey(), context)).toArray(MediaBindingBean[]::new)); - } + execute(renameLog.values(), Objects::nonNull, exec); // destination files may include null values } - // destination files may include null values return new ArrayList(renameLog.values()); } @@ -1075,6 +1071,25 @@ public class CmdlineOperations implements CmdlineInterface { }).filter(Objects::nonNull); } + @Override + public boolean execute(Collection files, FileFilter filter, ExecCommand exec) throws Exception { + // collect files + List f = filter(files, filter); + + if (f.isEmpty()) { + return false; + } + + // collect object metadata + List m = f.stream().map(xattr::getMetaInfo).collect(toList()); + + // build and execute commands + MediaBindingBean[] group = IntStream.range(0, f.size()).mapToObj(i -> new MediaBindingBean(m.get(i), f.get(i), new EntryList(f, m))).toArray(MediaBindingBean[]::new); + exec.execute(group); + + return true; + } + @Override public List revert(Collection files, FileFilter filter, RenameAction action) throws Exception { if (files.isEmpty()) { diff --git a/source/net/filebot/cli/ExecCommand.java b/source/net/filebot/cli/ExecCommand.java index a16953d1..53f6016c 100644 --- a/source/net/filebot/cli/ExecCommand.java +++ b/source/net/filebot/cli/ExecCommand.java @@ -72,7 +72,7 @@ public class ExecCommand { process.directory(directory); process.inheritIO(); - debug.finest(message("Execute", command)); + debug.finest(format("Execute %s", command)); int exitCode = process.start().waitFor(); if (exitCode != 0) {