From 172c4954dfd30063271fd9ccb8b2aa5ffd2b2ca4 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Wed, 5 Dec 2018 19:24:18 +0700 Subject: [PATCH] Refactor license key import handling --- source/net/filebot/License.java | 20 ++++++++-------- source/net/filebot/Main.java | 22 ++++++++++-------- source/net/filebot/Settings.java | 18 ++++++++++++++- source/net/filebot/cli/ArgumentBean.java | 9 ++++++-- .../net/filebot/ui/rename/RenameAction.java | 23 ++++++++----------- source/net/filebot/util/PGP.java | 4 ---- source/net/filebot/util/ui/SwingEventBus.java | 4 ++++ 7 files changed, 60 insertions(+), 40 deletions(-) diff --git a/source/net/filebot/License.java b/source/net/filebot/License.java index ca7ce1c7..34bf0a8d 100644 --- a/source/net/filebot/License.java +++ b/source/net/filebot/License.java @@ -11,7 +11,6 @@ import java.io.File; import java.io.FileNotFoundException; import java.net.URL; import java.nio.file.Files; -import java.nio.file.StandardCopyOption; import java.time.Instant; import java.time.LocalDate; import java.time.ZoneOffset; @@ -87,8 +86,14 @@ public class License { } // read and verify license file - String psm = findClearSignMessage(file); + return parseLicenseText(new String(Files.readAllBytes(file.toPath()), UTF_8)); + } catch (Exception error) { + return new License(error); + } + } + public static License parseLicenseText(String psm) { + try { // verify and get clear signed content Map properties = getProperties(psm); @@ -153,19 +158,14 @@ public class License { public static final SystemProperty FILE = SystemProperty.of("net.filebot.license", File::new, ApplicationFolder.AppData.resolve("license.txt")); public static final MemoizedResource INSTANCE = Resource.lazy(() -> parseLicenseFile(FILE.get())); - public static License importLicenseFile(File file) throws Exception { - // require non-empty license file - if (file.length() <= 0) { - throw new FileNotFoundException("License file not found: " + file); - } - + public static License importLicense(String psm) throws Exception { // lock memoized resource while validating and setting a new license synchronized (License.INSTANCE) { // check if license file is valid and not expired - License license = parseLicenseFile(file).check(); + License license = parseLicenseText(psm).check(); // write to default license file path - Files.copy(file.toPath(), License.FILE.get().toPath(), StandardCopyOption.REPLACE_EXISTING); + Files.write(License.FILE.get().toPath(), psm.getBytes(UTF_8)); // clear memoized instance and reload on next access License.INSTANCE.clear(); diff --git a/source/net/filebot/Main.java b/source/net/filebot/Main.java index 10eaa200..ebd70478 100644 --- a/source/net/filebot/Main.java +++ b/source/net/filebot/Main.java @@ -118,10 +118,11 @@ public class Main { if (args.runCLI()) { // just import and print license when running with --license option if (LICENSE.isFile()) { - args.getLicenseFile().ifPresent(f -> { - configureLicense(f); + String psm = args.getLicenseKey(); + if (psm != null) { + configureLicense(psm); System.exit(0); - }); + } } int status = new ArgumentProcessor().run(args); @@ -174,12 +175,16 @@ public class Main { SwingEventBus.getInstance().post(new FileTransferable(files)); } + // import license if launched with license file if (LICENSE.isFile()) { - // import license if launched with license file - args.getLicenseFile().ifPresent(f -> configureLicense(f)); - - // make sure license is validated and cached - SwingEventBus.getInstance().post(LICENSE); + try { + String psm = args.getLicenseKey(); + if (psm != null) { + configureLicense(psm); + } + } catch (Throwable e) { + debug.log(Level.WARNING, e, e::getMessage); + } } // JavaFX is used for ProgressMonitor and GettingStartedDialog @@ -250,7 +255,6 @@ public class Main { MacAppUtilities.initializeApplication(FileBotMenuBar.createHelp(), files -> { if (LICENSE.isFile() && files.size() == 1 && containsOnly(files, LICENSE_FILES)) { configureLicense(files.get(0)); - SwingEventBus.getInstance().post(LICENSE); } else { SwingEventBus.getInstance().post(new FileTransferable(files)); } diff --git a/source/net/filebot/Settings.java b/source/net/filebot/Settings.java index 64bc1b1f..6c2539b9 100644 --- a/source/net/filebot/Settings.java +++ b/source/net/filebot/Settings.java @@ -2,6 +2,8 @@ package net.filebot; import static net.filebot.License.*; import static net.filebot.Logging.*; +import static net.filebot.util.FileUtilities.*; +import static net.filebot.util.PGP.*; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; @@ -23,6 +25,7 @@ import net.filebot.util.PreferencesMap; import net.filebot.util.PreferencesMap.JsonAdapter; import net.filebot.util.PreferencesMap.PreferencesEntry; import net.filebot.util.PreferencesMap.StringAdapter; +import net.filebot.util.ui.SwingEventBus; public final class Settings { @@ -149,10 +152,23 @@ public final class Settings { public static void configureLicense(File file) { try { - log.info(importLicenseFile(file) + " has been activated successfully."); + configureLicense(findClearSignMessage(readTextFile(file))); + } catch (Exception e) { + log.severe("Invalid License File: " + e.getMessage()); + } + } + + public static void configureLicense(String text) { + try { + log.info(importLicense(findClearSignMessage(text)) + " has been activated successfully."); } catch (Throwable e) { log.severe("License Error: " + e.getMessage()); } + + // make sure license is validated and cached + if (SwingEventBus.isActive()) { + SwingEventBus.getInstance().post(LICENSE); + } } public static String getAppStoreName() { diff --git a/source/net/filebot/cli/ArgumentBean.java b/source/net/filebot/cli/ArgumentBean.java index 0038bbe4..e7d4ff7c 100644 --- a/source/net/filebot/cli/ArgumentBean.java +++ b/source/net/filebot/cli/ArgumentBean.java @@ -8,6 +8,7 @@ import static net.filebot.hash.VerificationUtilities.*; import static net.filebot.media.XattrMetaInfo.*; import static net.filebot.subtitle.SubtitleUtilities.*; import static net.filebot.util.FileUtilities.*; +import static net.filebot.util.PGP.*; import java.io.File; import java.io.FileFilter; @@ -352,8 +353,12 @@ public class ArgumentBean { }).orElseThrow(error("Illegal mode", mode)); } - public Optional getLicenseFile() { - return optional(license).map(File::new); + public String getLicenseKey() { + try { + return license == null || license.isEmpty() ? null : findClearSignMessage(readTextFile(new File(license))); + } catch (Exception e) { + throw new CmdlineException("Invalid License File: " + e.getMessage(), e); + } } private final String[] args; diff --git a/source/net/filebot/ui/rename/RenameAction.java b/source/net/filebot/ui/rename/RenameAction.java index 8b580ed7..3b6cc63b 100644 --- a/source/net/filebot/ui/rename/RenameAction.java +++ b/source/net/filebot/ui/rename/RenameAction.java @@ -1,6 +1,5 @@ package net.filebot.ui.rename; -import static java.nio.charset.StandardCharsets.*; import static java.util.Arrays.*; import static java.util.Collections.*; import static java.util.stream.Collectors.*; @@ -10,11 +9,11 @@ import static net.filebot.media.MediaDetection.*; import static net.filebot.media.XattrMetaInfo.*; import static net.filebot.util.ExceptionUtilities.*; import static net.filebot.util.FileUtilities.*; -import static net.filebot.util.PGP.*; import static net.filebot.util.ui.SwingUI.*; import java.awt.Toolkit; import java.awt.Window; +import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.event.ActionEvent; import java.io.File; @@ -252,7 +251,7 @@ class RenameAction extends AbstractAction { List files = UserFiles.FileChooser.AWT.showLoadDialogSelectFiles(false, false, null, MediaTypes.LICENSE_FILES, "Select License", evt); if (files.size() > 0) { configureLicense(files.get(0)); - SwingEventBus.getInstance().post(LICENSE); + return; } }); })); @@ -260,21 +259,17 @@ class RenameAction extends AbstractAction { actionPopup.add(newAction("Paste License Key", ResourceManager.getIcon("license.import"), c -> { withWaitCursor(evt.getSource(), () -> { try { - String clip = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor); - String psm = findClearSignMessage(clip); - - File tmp = File.createTempFile("clip", ".txt"); - writeFile(psm.getBytes(UTF_8), tmp); - - configureLicense(tmp); - SwingEventBus.getInstance().post(LICENSE); - - tmp.delete(); + Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); + if (clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)) { + String clip = (String) clipboard.getData(DataFlavor.stringFlavor); + configureLicense(clip); + return; + } } catch (Exception e) { - log.info("The clipboard does not contain a license key. Please select and copy your license key first."); debug.log(Level.WARNING, e, e::getMessage); } + log.info("The clipboard does not contain a license key. Please select and copy your license key first."); }); })); diff --git a/source/net/filebot/util/PGP.java b/source/net/filebot/util/PGP.java index 072b65f0..15420cdb 100644 --- a/source/net/filebot/util/PGP.java +++ b/source/net/filebot/util/PGP.java @@ -68,8 +68,4 @@ public class PGP { throw new IllegalArgumentException("PGP SIGNED MESSAGE not found"); } - public static String findClearSignMessage(File file) throws IOException { - return findClearSignMessage(readTextFile(file)); - } - } diff --git a/source/net/filebot/util/ui/SwingEventBus.java b/source/net/filebot/util/ui/SwingEventBus.java index 816891bb..96c75770 100644 --- a/source/net/filebot/util/ui/SwingEventBus.java +++ b/source/net/filebot/util/ui/SwingEventBus.java @@ -20,6 +20,10 @@ public class SwingEventBus extends AsyncEventBus { return instance; } + public static synchronized boolean isActive() { + return instance != null; + } + public SwingEventBus() { super(SwingUtilities::invokeLater, SwingEventBus::handleException); }