From 625e972853f60160f4b9e8b07f4fa02a6aae65df Mon Sep 17 00:00:00 2001 From: 0xb00bface <0xboobface@gmail.com> Date: Tue, 3 May 2022 17:57:51 +0200 Subject: [PATCH] Save the config in a sub-directory for each version --- CHANGELOG.md | 1 + client/pom.xml | 2 +- .../java/ctbrec/ui/CamrecApplication.java | 21 ++--- .../src/main/java/ctbrec/ui/news/NewsTab.java | 21 +++-- common/pom.xml | 2 +- common/src/main/java/ctbrec/Config.java | 82 ++++++++++++++----- common/src/main/java/ctbrec/Version.java | 28 +++++-- master/pom.xml | 2 +- server/pom.xml | 2 +- 9 files changed, 104 insertions(+), 57 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c9b14926..282a797a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ 4.7.6 ======================== * Fix minimize to tray +* Save coonfig in a sub-directory for each version. 4.7.5 ======================== diff --git a/client/pom.xml b/client/pom.xml index d5a27031..040e738c 100644 --- a/client/pom.xml +++ b/client/pom.xml @@ -8,7 +8,7 @@ ctbrec master - 4.7.5 + 4.7.6 ../master diff --git a/client/src/main/java/ctbrec/ui/CamrecApplication.java b/client/src/main/java/ctbrec/ui/CamrecApplication.java index 95a57972..bd7b1ce4 100644 --- a/client/src/main/java/ctbrec/ui/CamrecApplication.java +++ b/client/src/main/java/ctbrec/ui/CamrecApplication.java @@ -63,7 +63,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.awt.*; -import java.io.*; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; import java.lang.reflect.Type; import java.nio.charset.StandardCharsets; import java.security.InvalidKeyException; @@ -206,7 +209,7 @@ public class CamrecApplication extends Application { LOG.debug("Creating GUI"); DesktopIntegration.setRecorder(recorder); DesktopIntegration.setPrimaryStage(primaryStage); - CamrecApplication.title = "CTB Recorder " + getVersion(); + CamrecApplication.title = "CTB Recorder " + Version.getVersion(); primaryStage.setTitle(title); InputStream icon = getClass().getResourceAsStream("/icon.png"); primaryStage.getIcons().add(new Image(icon)); @@ -564,7 +567,7 @@ public class CamrecApplication extends Application { List releases = adapter.fromJson(body); var latest = releases.get(0); var latestVersion = latest.getVersion(); - var ctbrecVersion = getVersion(); + var ctbrecVersion = Version.getVersion(); if (latestVersion.compareTo(ctbrecVersion) > 0) { LOG.debug("Update available {} < {}", ctbrecVersion, latestVersion); Platform.runLater(() -> tabPane.getTabs().add(new UpdateTab(latest))); @@ -583,18 +586,6 @@ public class CamrecApplication extends Application { updateCheck.start(); } - public static Version getVersion() throws IOException { - if (Objects.equals(System.getenv("CTBREC_DEV"), "1")) { - return Version.of("0.0.0-DEV"); - } else { - try (InputStream is = CamrecApplication.class.getClassLoader().getResourceAsStream("version")) { - var reader = new BufferedReader(new InputStreamReader(is)); - var versionString = reader.readLine(); - return Version.of(versionString); - } - } - } - public static class Release { private String name; private String tag_name; // NOSONAR - name pattern is needed by moshi diff --git a/client/src/main/java/ctbrec/ui/news/NewsTab.java b/client/src/main/java/ctbrec/ui/news/NewsTab.java index ad97b830..dbaf38f4 100644 --- a/client/src/main/java/ctbrec/ui/news/NewsTab.java +++ b/client/src/main/java/ctbrec/ui/news/NewsTab.java @@ -1,18 +1,10 @@ package ctbrec.ui.news; -import static ctbrec.ErrorMessages.HTTP_RESPONSE_BODY_IS_NULL; -import static ctbrec.io.HttpConstants.*; - -import java.io.IOException; -import java.util.Objects; - -import ctbrec.Config; -import org.json.JSONObject; - import com.squareup.moshi.JsonAdapter; import com.squareup.moshi.Moshi; - +import ctbrec.Config; import ctbrec.GlobalThreadPool; +import ctbrec.Version; import ctbrec.io.HttpException; import ctbrec.ui.CamrecApplication; import ctbrec.ui.controls.Dialogs; @@ -24,6 +16,13 @@ import javafx.scene.control.ScrollPane; import javafx.scene.control.Tab; import javafx.scene.layout.VBox; import okhttp3.Request; +import org.json.JSONObject; + +import java.io.IOException; +import java.util.Objects; + +import static ctbrec.ErrorMessages.HTTP_RESPONSE_BODY_IS_NULL; +import static ctbrec.io.HttpConstants.USER_AGENT; public class NewsTab extends Tab implements TabSelectionListener { private static final String ACCESS_TOKEN = "a2804d73a89951a22e0f8483a6fcec8943afd88b7ba17c459c095aa9e6f94fd0"; @@ -49,7 +48,7 @@ public class NewsTab extends Tab implements TabSelectionListener { var request = new Request.Builder() .url(URL) .header("Authorization", "Bearer " + ACCESS_TOKEN) - .header(USER_AGENT, "ctbrec " + CamrecApplication.getVersion()) + .header(USER_AGENT, "ctbrec " + Version.getVersion()) .build(); try (var response = CamrecApplication.httpClient.execute(request)) { if (response.isSuccessful()) { diff --git a/common/pom.xml b/common/pom.xml index 28d77aa6..167182ed 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -8,7 +8,7 @@ ctbrec master - 4.7.5 + 4.7.6 ../master diff --git a/common/src/main/java/ctbrec/Config.java b/common/src/main/java/ctbrec/Config.java index 66a12ec2..3b54e0f8 100644 --- a/common/src/main/java/ctbrec/Config.java +++ b/common/src/main/java/ctbrec/Config.java @@ -36,6 +36,7 @@ import static java.nio.file.StandardOpenOption.*; public class Config { private static final Logger LOG = LoggerFactory.getLogger(Config.class); + private static final String SYSPROP_CONFIG_DIR = "ctbrec.config.dir"; private static Config instance; private Settings settings; @@ -49,21 +50,61 @@ public class Config { private boolean savingDisabled = false; public static final String RECORDING_DATE_FORMAT = "yyyy-MM-dd_HH-mm-ss_SSS"; - public Config(List sites) { + public Config(List sites) throws IOException { this.sites = sites; - if(System.getProperty("ctbrec.config.dir") != null) { - configDir = new File(System.getProperty("ctbrec.config.dir")); + + copyConfigIfNewVersion(); + + if (System.getProperty(SYSPROP_CONFIG_DIR) != null) { + configDir = new File(System.getProperty(SYSPROP_CONFIG_DIR), Version.getVersion().toString()); } else { - configDir = OS.getConfigDir(); + configDir = new File(OS.getConfigDir(), Version.getVersion().toString()); } - if(System.getProperty("ctbrec.config") != null) { + if (System.getProperty("ctbrec.config") != null) { filename = System.getProperty("ctbrec.config"); } else { filename = "settings.json"; } } + private void copyConfigIfNewVersion() throws IOException { + File configDirectory; + if (System.getProperty(SYSPROP_CONFIG_DIR) != null) { + configDirectory = new File(System.getProperty(SYSPROP_CONFIG_DIR)); + } else { + configDirectory = OS.getConfigDir(); + } + + Version currentVersion = Version.getVersion(); + Version previousVersion = getPreviousVersion(configDirectory); + + File src; + File target = new File(configDirectory, currentVersion.toString()); + if (target.exists()) { + return; + } + + if (previousVersion.compareTo(Version.of("4.7.5")) <= 0) { + src = configDirectory; + } else { + src = new File(configDirectory, previousVersion.toString()); + } + if (!Objects.equals(previousVersion, currentVersion)) { + LOG.debug("Version update {} -> {}", previousVersion, currentVersion); + LOG.debug("Copying config from {} to {}", src, target); + FileUtils.copyDirectory(src, target, pathname -> !(pathname.toString().contains("minimal-browser") && pathname.toString().contains("Cache")), true); + } + } + + private Version getPreviousVersion(File configDirectory) { + Optional previousVersion = Arrays.stream(configDirectory.listFiles((dir, name) -> name.matches("\\d+\\.\\d+\\.\\d+"))) + .map(File::getName) + .map(Version::of) + .max(Comparator.naturalOrder()); + return previousVersion.orElse(Version.of("4.7.5")); + } + private void load() throws IOException { Moshi moshi = new Moshi.Builder() .add(Model.class, new ModelJsonAdapter(sites)) @@ -113,7 +154,7 @@ public class Config { @SuppressWarnings("deprecation") private void migrateOldSettings() { // 3.8.0 from maxResolution only to resolution range - if(settings.minimumResolution == settings.maximumResolution && settings.minimumResolution == 0) { + if (settings.minimumResolution == settings.maximumResolution && settings.minimumResolution == 0) { settings.minimumResolution = 0; settings.maximumResolution = 8640; } @@ -168,7 +209,7 @@ public class Config { } } // 3.10.10 model notes due to Cam4 URL change - for (Iterator> iterator = settings.modelNotes.entrySet().iterator(); iterator.hasNext();) { + for (Iterator> iterator = settings.modelNotes.entrySet().iterator(); iterator.hasNext(); ) { Entry note = iterator.next(); if (note.getKey().contains("cam4") && note.getKey().endsWith("/")) { Cam4Model model = new Cam4Model(); @@ -179,11 +220,11 @@ public class Config { } // 3.11.0 make Cam4 model names lower case settings.models.stream() - .filter(Cam4Model.class::isInstance) - .forEach(m -> m.setName(m.getName().toLowerCase())); + .filter(Cam4Model.class::isInstance) + .forEach(m -> m.setName(m.getName().toLowerCase())); settings.modelsIgnored.stream() - .filter(Cam4Model.class::isInstance) - .forEach(m -> m.setName(m.getName().toLowerCase())); + .filter(Cam4Model.class::isInstance) + .forEach(m -> m.setName(m.getName().toLowerCase())); // 4.1.2 reduce models ignore to store only the URL if (settings.modelsIgnored != null && !settings.modelsIgnored.isEmpty()) { settings.ignoredModels = settings.modelsIgnored.stream() @@ -239,7 +280,7 @@ public class Config { File configFile = new File(configDir, filename); LOG.debug("Saving config to {}", configFile.getAbsolutePath()); Files.createDirectories(configDir.toPath()); - Files.write(configFile.toPath(), json.getBytes(UTF_8), CREATE, WRITE, TRUNCATE_EXISTING); + Files.writeString(configFile.toPath(), json, CREATE, WRITE, TRUNCATE_EXISTING); } public static boolean isServerMode() { @@ -265,20 +306,19 @@ public class Config { private File getDirForRecording(Model model, String formattedDate) { switch (getSettings().recordingsDirStructure) { - case ONE_PER_MODEL: - return new File(getSettings().recordingsDir, model.getSanitizedNamed()); - case ONE_PER_RECORDING: - File modelDir = new File(getSettings().recordingsDir, model.getSanitizedNamed()); - return new File(modelDir, formattedDate); - case FLAT: - default: - return new File(getSettings().recordingsDir); + case ONE_PER_MODEL: + return new File(getSettings().recordingsDir, model.getSanitizedNamed()); + case ONE_PER_RECORDING: + File modelDir = new File(getSettings().recordingsDir, model.getSanitizedNamed()); + return new File(modelDir, formattedDate); + case FLAT: + default: + return new File(getSettings().recordingsDir); } } public String getServerUrl() { String scheme = getSettings().transportLayerSecurity ? "https" : "http"; - // int port = getSettings().transportLayerSecurity ? getSettings().httpSecurePort : getSettings().httpPort; int port = getSettings().httpPort; String baseUrl = scheme + "://" + getSettings().httpServer + ":" + port + getContextPath(); return baseUrl; diff --git a/common/src/main/java/ctbrec/Version.java b/common/src/main/java/ctbrec/Version.java index 203a1523..c9ef90af 100644 --- a/common/src/main/java/ctbrec/Version.java +++ b/common/src/main/java/ctbrec/Version.java @@ -1,5 +1,9 @@ package ctbrec; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -47,7 +51,7 @@ public class Version implements Comparable { @Override public String toString() { String version = major + "." + minor + "." + revision; - if(!designator.isEmpty()) { + if (!designator.isEmpty()) { version += "-" + designator; } return version; @@ -90,12 +94,12 @@ public class Version implements Comparable { @Override public int compareTo(Version o) { int result = 0; - if(major == o.major) { - if(minor == o.minor) { - if(revision == o.revision) { - if(!designator.isEmpty() && o.designator.isEmpty()) { + if (major == o.major) { + if (minor == o.minor) { + if (revision == o.revision) { + if (!designator.isEmpty() && o.designator.isEmpty()) { result = -1; - } else if(designator.isEmpty() && !o.designator.isEmpty()) { + } else if (designator.isEmpty() && !o.designator.isEmpty()) { result = 1; } else { result = 0; @@ -111,4 +115,16 @@ public class Version implements Comparable { } return result; } + + public static Version getVersion() throws IOException { + if (Objects.equals(System.getenv("CTBREC_DEV"), "1")) { + return Version.of("0.0.0-DEV"); + } else { + try (InputStream is = Version.class.getClassLoader().getResourceAsStream("version")) { + var reader = new BufferedReader(new InputStreamReader(is)); + var versionString = reader.readLine(); + return Version.of(versionString); + } + } + } } diff --git a/master/pom.xml b/master/pom.xml index c513c7f4..653bc316 100644 --- a/master/pom.xml +++ b/master/pom.xml @@ -6,7 +6,7 @@ ctbrec master pom - 4.7.5 + 4.7.6 ../common diff --git a/server/pom.xml b/server/pom.xml index 5c70c2b0..f9e7f459 100644 --- a/server/pom.xml +++ b/server/pom.xml @@ -8,7 +8,7 @@ ctbrec master - 4.7.5 + 4.7.6 ../master