mirror of
https://github.com/mitb-archive/filebot
synced 2024-12-22 15:58:52 -05:00
Refactor license key import handling
This commit is contained in:
parent
9496ce6f10
commit
172c4954df
@ -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<String, String> properties = getProperties(psm);
|
||||
|
||||
@ -153,19 +158,14 @@ public class License {
|
||||
public static final SystemProperty<File> FILE = SystemProperty.of("net.filebot.license", File::new, ApplicationFolder.AppData.resolve("license.txt"));
|
||||
public static final MemoizedResource<License> 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();
|
||||
|
@ -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));
|
||||
}
|
||||
|
@ -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() {
|
||||
|
@ -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<File> 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;
|
||||
|
@ -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<File> 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.");
|
||||
});
|
||||
}));
|
||||
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user