From 672c015696d1f506aa133024f8908867cac84c9a Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Tue, 30 Apr 2019 19:50:58 +0700 Subject: [PATCH] + Add {history} binding to access the original historic path of {f} (e.g. useful for -exec post-processing calls) --- source/net/filebot/History.java | 13 ++++++++++++- source/net/filebot/format/MediaBindingBean.java | 13 +++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/source/net/filebot/History.java b/source/net/filebot/History.java index 535cbb64..476ca722 100644 --- a/source/net/filebot/History.java +++ b/source/net/filebot/History.java @@ -14,6 +14,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.logging.Level; +import java.util.stream.Stream; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; @@ -181,7 +182,7 @@ public class History { public Map getRenameMap() { Map map = new LinkedHashMap(); - for (History.Sequence seq : this.sequences()) { + for (History.Sequence seq : sequences) { for (History.Element elem : seq.elements()) { File to = new File(elem.to()); if (!to.isAbsolute()) { @@ -194,6 +195,16 @@ public class History { return map; } + public Stream getOriginalPath(File destination) { + return sequences.stream().flatMap(s -> s.elements().stream()).filter(e -> { + File to = new File(e.to()); + if (!to.isAbsolute()) { + to = new File(e.dir(), e.to()); + } + return destination.equals(to); + }).map(e -> new File(e.dir(), e.from())); + } + public static void exportHistory(History history, OutputStream output) { try { Marshaller marshaller = JAXBContext.newInstance(History.class).createMarshaller(); diff --git a/source/net/filebot/format/MediaBindingBean.java b/source/net/filebot/format/MediaBindingBean.java index 63d2e349..07015ed1 100644 --- a/source/net/filebot/format/MediaBindingBean.java +++ b/source/net/filebot/format/MediaBindingBean.java @@ -46,6 +46,7 @@ import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Caffeine; import net.filebot.ApplicationFolder; +import net.filebot.HistorySpooler; import net.filebot.Language; import net.filebot.MediaTypes; import net.filebot.Resource; @@ -488,6 +489,18 @@ public class MediaBindingBean { return name != null ? getNameWithoutExtension(name) : null; } + @Define("history") + public File getOriginalFilePath() throws Exception { + // check in-memory history first + Optional path = HistorySpooler.getInstance().getSessionHistory().getOriginalPath(getMediaFile()).findFirst(); + if (path.isPresent()) { + return path.get(); + } + + // check persistent history file as well + return HistorySpooler.getInstance().getCompleteHistory().getOriginalPath(getMediaFile()).findFirst().orElse(null); + } + @Define("xattr") public Object getMetaAttributesObject() throws Exception { return xattr.getMetaInfo(getMediaFile());