Code cleanup

This commit is contained in:
0xboobface 2019-12-28 15:38:33 +01:00
parent d742756413
commit a59313df49
1 changed files with 13 additions and 13 deletions

View File

@ -3,8 +3,8 @@ package ctbrec;
import static java.nio.file.StandardOpenOption.*; import static java.nio.file.StandardOpenOption.*;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
@ -28,7 +28,7 @@ import ctbrec.sites.Site;
public class Config { public class Config {
private static final transient Logger LOG = LoggerFactory.getLogger(Config.class); private static final Logger LOG = LoggerFactory.getLogger(Config.class);
private static Config instance; private static Config instance;
private Settings settings; private Settings settings;
@ -37,7 +37,7 @@ public class Config {
private File configDir; private File configDir;
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";
private Config(List<Site> sites) throws FileNotFoundException, IOException { private Config(List<Site> sites) {
this.sites = sites; this.sites = sites;
if(System.getProperty("ctbrec.config.dir") != null) { if(System.getProperty("ctbrec.config.dir") != null) {
configDir = new File(System.getProperty("ctbrec.config.dir")); configDir = new File(System.getProperty("ctbrec.config.dir"));
@ -52,14 +52,14 @@ public class Config {
} }
} }
private void load() throws FileNotFoundException, 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))
.build(); .build();
JsonAdapter<Settings> adapter = moshi.adapter(Settings.class).lenient(); JsonAdapter<Settings> adapter = moshi.adapter(Settings.class).lenient();
File configFile = new File(configDir, filename); File configFile = new File(configDir, filename);
LOG.debug("Loading config from {}", configFile.getAbsolutePath()); LOG.debug("Loading config from {}", configFile.getAbsolutePath());
if(configFile.exists()) { if (configFile.exists()) {
try { try {
byte[] fileContent = Files.readAllBytes(configFile.toPath()); byte[] fileContent = Files.readAllBytes(configFile.toPath());
if (fileContent[0] == -17 && fileContent[1] == -69 && fileContent[2] == -65) { if (fileContent[0] == -17 && fileContent[1] == -69 && fileContent[2] == -65) {
@ -69,13 +69,13 @@ public class Config {
fileContent[1] = ' '; fileContent[1] = ' ';
fileContent[2] = ' '; fileContent[2] = ' ';
} }
String json = new String(fileContent, "UTF-8").trim(); String json = new String(fileContent, StandardCharsets.UTF_8.name()).trim();
settings = adapter.fromJson(json); settings = adapter.fromJson(json);
settings.httpTimeout = Math.max(settings.httpTimeout, 10_000); settings.httpTimeout = Math.max(settings.httpTimeout, 10_000);
if (settings.recordingsDir.endsWith("/")) { if (settings.recordingsDir.endsWith("/")) {
settings.recordingsDir = settings.recordingsDir.substring(0, settings.recordingsDir.length() - 1); settings.recordingsDir = settings.recordingsDir.substring(0, settings.recordingsDir.length() - 1);
} }
} catch(Throwable e) { } catch (Exception e) {
settings = OS.getDefaultSettings(); settings = OS.getDefaultSettings();
for (Site site : sites) { for (Site site : sites) {
site.setEnabled(!settings.disabledSites.contains(site.getName())); site.setEnabled(!settings.disabledSites.contains(site.getName()));
@ -99,20 +99,20 @@ public class Config {
String backup = source.getName() + '.' + timestamp; String backup = source.getName() + '.' + timestamp;
File target = new File(source.getParentFile(), backup); File target = new File(source.getParentFile(), backup);
Files.copy(source.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING); Files.copy(source.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch(Throwable e) { } catch (Exception e) {
LOG.error("Couldn't create backup of settings file", e); LOG.error("Couldn't create backup of settings file", e);
} }
} }
public static synchronized void init(List<Site> sites) throws FileNotFoundException, IOException { public static synchronized void init(List<Site> sites) throws IOException {
if(instance == null) { if (instance == null) {
instance = new Config(sites); instance = new Config(sites);
instance.load(); instance.load();
} }
} }
public static synchronized Config getInstance() { public static synchronized Config getInstance() {
if(instance == null) { if (instance == null) {
throw new IllegalStateException("Config not initialized, call init() first"); throw new IllegalStateException("Config not initialized, call init() first");
} }
return instance; return instance;
@ -156,7 +156,7 @@ public class Config {
} }
private File getDirForRecording(Model model, String formattedDate) { private File getDirForRecording(Model model, String formattedDate) {
switch(getSettings().recordingsDirStructure) { switch (getSettings().recordingsDirStructure) {
case ONE_PER_MODEL: case ONE_PER_MODEL:
return new File(getSettings().recordingsDir, model.getSanitizedNamed()); return new File(getSettings().recordingsDir, model.getSanitizedNamed());
case ONE_PER_RECORDING: case ONE_PER_RECORDING:
@ -170,7 +170,7 @@ 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().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;