forked from j62/ctbrec
1
0
Fork 0

Save the config in a sub-directory for each version

This commit is contained in:
0xb00bface 2022-05-03 17:57:51 +02:00
parent 77b9737495
commit 625e972853
9 changed files with 104 additions and 57 deletions

View File

@ -1,6 +1,7 @@
4.7.6 4.7.6
======================== ========================
* Fix minimize to tray * Fix minimize to tray
* Save coonfig in a sub-directory for each version.
4.7.5 4.7.5
======================== ========================

View File

@ -8,7 +8,7 @@
<parent> <parent>
<groupId>ctbrec</groupId> <groupId>ctbrec</groupId>
<artifactId>master</artifactId> <artifactId>master</artifactId>
<version>4.7.5</version> <version>4.7.6</version>
<relativePath>../master</relativePath> <relativePath>../master</relativePath>
</parent> </parent>

View File

@ -63,7 +63,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.awt.*; 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.lang.reflect.Type;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
@ -206,7 +209,7 @@ public class CamrecApplication extends Application {
LOG.debug("Creating GUI"); LOG.debug("Creating GUI");
DesktopIntegration.setRecorder(recorder); DesktopIntegration.setRecorder(recorder);
DesktopIntegration.setPrimaryStage(primaryStage); DesktopIntegration.setPrimaryStage(primaryStage);
CamrecApplication.title = "CTB Recorder " + getVersion(); CamrecApplication.title = "CTB Recorder " + Version.getVersion();
primaryStage.setTitle(title); primaryStage.setTitle(title);
InputStream icon = getClass().getResourceAsStream("/icon.png"); InputStream icon = getClass().getResourceAsStream("/icon.png");
primaryStage.getIcons().add(new Image(icon)); primaryStage.getIcons().add(new Image(icon));
@ -564,7 +567,7 @@ public class CamrecApplication extends Application {
List<Release> releases = adapter.fromJson(body); List<Release> releases = adapter.fromJson(body);
var latest = releases.get(0); var latest = releases.get(0);
var latestVersion = latest.getVersion(); var latestVersion = latest.getVersion();
var ctbrecVersion = getVersion(); var ctbrecVersion = Version.getVersion();
if (latestVersion.compareTo(ctbrecVersion) > 0) { if (latestVersion.compareTo(ctbrecVersion) > 0) {
LOG.debug("Update available {} < {}", ctbrecVersion, latestVersion); LOG.debug("Update available {} < {}", ctbrecVersion, latestVersion);
Platform.runLater(() -> tabPane.getTabs().add(new UpdateTab(latest))); Platform.runLater(() -> tabPane.getTabs().add(new UpdateTab(latest)));
@ -583,18 +586,6 @@ public class CamrecApplication extends Application {
updateCheck.start(); 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 { public static class Release {
private String name; private String name;
private String tag_name; // NOSONAR - name pattern is needed by moshi private String tag_name; // NOSONAR - name pattern is needed by moshi

View File

@ -1,18 +1,10 @@
package ctbrec.ui.news; 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.JsonAdapter;
import com.squareup.moshi.Moshi; import com.squareup.moshi.Moshi;
import ctbrec.Config;
import ctbrec.GlobalThreadPool; import ctbrec.GlobalThreadPool;
import ctbrec.Version;
import ctbrec.io.HttpException; import ctbrec.io.HttpException;
import ctbrec.ui.CamrecApplication; import ctbrec.ui.CamrecApplication;
import ctbrec.ui.controls.Dialogs; import ctbrec.ui.controls.Dialogs;
@ -24,6 +16,13 @@ import javafx.scene.control.ScrollPane;
import javafx.scene.control.Tab; import javafx.scene.control.Tab;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import okhttp3.Request; 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 { public class NewsTab extends Tab implements TabSelectionListener {
private static final String ACCESS_TOKEN = "a2804d73a89951a22e0f8483a6fcec8943afd88b7ba17c459c095aa9e6f94fd0"; private static final String ACCESS_TOKEN = "a2804d73a89951a22e0f8483a6fcec8943afd88b7ba17c459c095aa9e6f94fd0";
@ -49,7 +48,7 @@ public class NewsTab extends Tab implements TabSelectionListener {
var request = new Request.Builder() var request = new Request.Builder()
.url(URL) .url(URL)
.header("Authorization", "Bearer " + ACCESS_TOKEN) .header("Authorization", "Bearer " + ACCESS_TOKEN)
.header(USER_AGENT, "ctbrec " + CamrecApplication.getVersion()) .header(USER_AGENT, "ctbrec " + Version.getVersion())
.build(); .build();
try (var response = CamrecApplication.httpClient.execute(request)) { try (var response = CamrecApplication.httpClient.execute(request)) {
if (response.isSuccessful()) { if (response.isSuccessful()) {

View File

@ -8,7 +8,7 @@
<parent> <parent>
<groupId>ctbrec</groupId> <groupId>ctbrec</groupId>
<artifactId>master</artifactId> <artifactId>master</artifactId>
<version>4.7.5</version> <version>4.7.6</version>
<relativePath>../master</relativePath> <relativePath>../master</relativePath>
</parent> </parent>

View File

@ -36,6 +36,7 @@ import static java.nio.file.StandardOpenOption.*;
public class Config { public class Config {
private static final Logger LOG = LoggerFactory.getLogger(Config.class); private static final Logger LOG = LoggerFactory.getLogger(Config.class);
private static final String SYSPROP_CONFIG_DIR = "ctbrec.config.dir";
private static Config instance; private static Config instance;
private Settings settings; private Settings settings;
@ -49,12 +50,15 @@ public class Config {
private boolean savingDisabled = false; private boolean savingDisabled = false;
public static final String RECORDING_DATE_FORMAT = "yyyy-MM-dd_HH-mm-ss_SSS"; public static final String RECORDING_DATE_FORMAT = "yyyy-MM-dd_HH-mm-ss_SSS";
public Config(List<Site> sites) { public Config(List<Site> sites) throws IOException {
this.sites = sites; 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 { } else {
configDir = OS.getConfigDir(); configDir = new File(OS.getConfigDir(), Version.getVersion().toString());
} }
if (System.getProperty("ctbrec.config") != null) { if (System.getProperty("ctbrec.config") != null) {
@ -64,6 +68,43 @@ public class Config {
} }
} }
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<Version> 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 { private void load() throws IOException {
Moshi moshi = new Moshi.Builder() Moshi moshi = new Moshi.Builder()
.add(Model.class, new ModelJsonAdapter(sites)) .add(Model.class, new ModelJsonAdapter(sites))
@ -239,7 +280,7 @@ public class Config {
File configFile = new File(configDir, filename); File configFile = new File(configDir, filename);
LOG.debug("Saving config to {}", configFile.getAbsolutePath()); LOG.debug("Saving config to {}", configFile.getAbsolutePath());
Files.createDirectories(configDir.toPath()); 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() { public static boolean isServerMode() {
@ -278,7 +319,6 @@ public class Config {
public String getServerUrl() { public String getServerUrl() {
String scheme = getSettings().transportLayerSecurity ? "https" : "http"; String scheme = getSettings().transportLayerSecurity ? "https" : "http";
// int port = getSettings().transportLayerSecurity ? getSettings().httpSecurePort : getSettings().httpPort;
int port = getSettings().httpPort; int port = getSettings().httpPort;
String baseUrl = scheme + "://" + getSettings().httpServer + ":" + port + getContextPath(); String baseUrl = scheme + "://" + getSettings().httpServer + ":" + port + getContextPath();
return baseUrl; return baseUrl;

View File

@ -1,5 +1,9 @@
package ctbrec; 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.Objects;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -111,4 +115,16 @@ public class Version implements Comparable<Version> {
} }
return result; 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);
}
}
}
} }

View File

@ -6,7 +6,7 @@
<groupId>ctbrec</groupId> <groupId>ctbrec</groupId>
<artifactId>master</artifactId> <artifactId>master</artifactId>
<packaging>pom</packaging> <packaging>pom</packaging>
<version>4.7.5</version> <version>4.7.6</version>
<modules> <modules>
<module>../common</module> <module>../common</module>

View File

@ -8,7 +8,7 @@
<parent> <parent>
<groupId>ctbrec</groupId> <groupId>ctbrec</groupId>
<artifactId>master</artifactId> <artifactId>master</artifactId>
<version>4.7.5</version> <version>4.7.6</version>
<relativePath>../master</relativePath> <relativePath>../master</relativePath>
</parent> </parent>