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
========================
* Fix minimize to tray
* Save coonfig in a sub-directory for each version.
4.7.5
========================

View File

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

View File

@ -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<Release> 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

View File

@ -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()) {

View File

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

View File

@ -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<Site> sites) {
public Config(List<Site> 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<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 {
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<Entry<String, String>> iterator = settings.modelNotes.entrySet().iterator(); iterator.hasNext();) {
for (Iterator<Entry<String, String>> iterator = settings.modelNotes.entrySet().iterator(); iterator.hasNext(); ) {
Entry<String, String> 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;

View File

@ -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<Version> {
@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<Version> {
@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<Version> {
}
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>
<artifactId>master</artifactId>
<packaging>pom</packaging>
<version>4.7.5</version>
<version>4.7.6</version>
<modules>
<module>../common</module>

View File

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