Skip to content

Commit

Permalink
Merge branch 'master' into v2/music-rework
Browse files Browse the repository at this point in the history
  • Loading branch information
DxsSucuk committed Oct 17, 2022
2 parents 8a3a95e + f1d6dd9 commit 77683a5
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 42 deletions.
2 changes: 1 addition & 1 deletion languages/en_EN.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language:
name: "English (UK)"
locale: "en_GB"
version: "1.9.7"
version: "2.0.0"
author: "Presti"
command:
perform:
Expand Down
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
<dependency>
<groupId>se.michaelthelin.spotify</groupId>
<artifactId>spotify-web-api-java</artifactId>
<version>7.2.1</version>
<version>7.2.2</version>
<exclusions>
<exclusion>
<groupId>commons-codec</groupId>
Expand Down Expand Up @@ -180,6 +180,11 @@
</dependency>

<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/de/presti/ree6/language/Language.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,31 @@ public String getResource(@NotNull String key, @Nullable Object... parameter) {
if (!resources.containsKey(key)) return "Missing language resource!";
return String.format(resources.get(key), parameter);
}

/**
* Compare the current Language version with another Language.
* @param language The Language to compare with.
* @return The result of the comparison. True, if it should update | False, if it should not be updated.
*/
public boolean compareVersion(Language language) {
if (language == null) return false;
if (language.getVersion() == null) return false;
if (version == null) return true;
if (language.getVersion().equals(version)) return false;

String[] split = version.split("\\.");

int mayor = Integer.parseInt(split[0]);
int minor = Integer.parseInt(split[1]);
int patch = Integer.parseInt(split[2]);

String[] split2 = language.getVersion().split("\\.");
int otherMayor = Integer.parseInt(split2[0]);
int otherMinor = Integer.parseInt(split2[1]);
int otherPatch = Integer.parseInt(split2[2]);

if (otherMayor > mayor) return true;
if (otherMayor == mayor && otherMinor > minor) return true;
return otherMayor == mayor && otherMinor == minor && otherPatch > patch;
}
}
22 changes: 19 additions & 3 deletions src/main/java/de/presti/ree6/language/LanguageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public static void downloadLanguages() {

Path languageFile = Path.of("languages/", language + ".yml");

if (Files.exists(languageFile)) {
log.info("Ignoring Language download: {}", language);
if (!languageFile.toAbsolutePath().startsWith(Path.of("languages/").toAbsolutePath())) {
log.info("Ignoring Language download, since Path Traversal has been detected!");
return;
}

Expand All @@ -49,7 +49,23 @@ public static void downloadLanguages() {
try (InputStream inputStream = RequestUtility.request(RequestUtility.Request.builder().url(download).build())) {
if (inputStream == null) return;

Files.copy(inputStream, languageFile);
if (Files.exists(languageFile)) {
log.info("Language file {} already exists! Will compare version!", language);
YamlConfiguration newLanguageYaml = YamlConfiguration.loadConfiguration(inputStream);
Language newLanguage = new Language(newLanguageYaml);
Language oldLanguage = new Language(YamlConfiguration.loadConfiguration(languageFile.toFile()));
if (newLanguage.compareVersion(oldLanguage)) {
log.info("Language file {} is outdated!\nWill update!", language);
if (!languageFile.toFile().delete()) {
log.info("Failed to delete old Language file {}!", language);
}
newLanguageYaml.save(languageFile.toFile());
} else {
log.info("Language file {} is up to date!", language);
}
} else {
Files.copy(inputStream, languageFile);
}
} catch (IOException exception) {
log.error("An error occurred while downloading the language file!", exception);
}
Expand Down
17 changes: 1 addition & 16 deletions src/main/java/de/presti/ree6/main/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.presti.ree6.main;

import com.google.gson.JsonObject;
import com.zaxxer.hikari.HikariDataSource;
import de.presti.ree6.addons.AddonLoader;
import de.presti.ree6.addons.AddonManager;
import de.presti.ree6.audio.AudioPlayerSendHandler;
Expand Down Expand Up @@ -82,11 +81,6 @@ public class Main {
*/
Config config;

/**
* A reference to the Bots' generell data source.
*/
HikariDataSource dataSource;

/**
* String used to identify the last day.
*/
Expand Down Expand Up @@ -116,7 +110,7 @@ public static void main(String[] args) {

Sentry.init(options -> {
String dsn = instance.config.getConfiguration().getString("sentry.dsn");
options.setDsn(dsn == null ? "" : dsn);
options.setDsn((dsn == null || dsn.equalsIgnoreCase("yourSentryDSNHere")) ? "" : dsn);
// Set tracesSampleRate to 1.0 to capture 100% of transactions for performance monitoring.
// We recommend adjusting this value in production.
options.setTracesSampleRate(1.0);
Expand Down Expand Up @@ -411,15 +405,6 @@ public MusicWorker getMusicWorker() {
return musicWorker;
}

/**
* Retrieve the Instance of the {@link HikariDataSource}.
*
* @return the {@link HikariDataSource} instance.
*/
public HikariDataSource getDataSource() {
return dataSource;
}

/**
* Retrieve the Instance of the Config.
*
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/de/presti/ree6/main/MigrationMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@ public static void main(String[] args) {
instance.config.init();

// Create a new connection between the Application and the SQL-Server.
instance.sqlConnector = new SQLConnector(instance.config.getConfiguration().getString("mysql.user"),
instance.config.getConfiguration().getString("mysql.db"), instance.config.getConfiguration().getString("mysql.pw"),
instance.config.getConfiguration().getString("mysql.host"), instance.config.getConfiguration().getInt("mysql.port"));
instance.sqlConnector = new SQLConnector(instance.config.getConfiguration().getString("hikari.sql.user"),
instance.config.getConfiguration().getString("hikari.sql.db"), instance.config.getConfiguration().getString("hikari.sql.pw"),
instance.config.getConfiguration().getString("hikari.sql.host"), instance.config.getConfiguration().getInt("hikari.sql.port"));

new MigrationBuilder().name(args.length != 0 ? args[0] : "NOTGIVEN").build(instance.sqlConnector).storeMigration();
}

}
8 changes: 4 additions & 4 deletions src/main/java/de/presti/ree6/sql/SQLConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public void connectToSQLServer() {
HikariConfig hConfig = new HikariConfig();

hConfig.setJdbcUrl(SQLSession.getJdbcURL());
hConfig.setUsername(Main.getInstance().getConfig().getConfiguration().getString("hikari.sql.user"));
hConfig.setPassword(Main.getInstance().getConfig().getConfiguration().getString("hikari.sql.pw"));
hConfig.setMaximumPoolSize(SQLSession.getMaxPoolSize());
dataSource = new HikariDataSource(hConfig);
log.info("Service (SQL) has been started. Connection was successful.");
Expand Down Expand Up @@ -140,12 +142,10 @@ public String buildConnectionURL() {

switch (Main.getInstance().getConfig().getConfiguration().getString("hikari.misc.storage").toLowerCase()) {
case "mariadb" -> {
jdbcUrl = "jdbc:mariadb://%s:%s/%s?user=%s&password=%s";
jdbcUrl = "jdbc:mariadb://%s:%s/%s";
jdbcUrl = jdbcUrl.formatted(databaseServerIP,
databaseServerPort,
databaseName,
databaseUser,
databasePassword);
databaseName);
}

default -> {
Expand Down
19 changes: 14 additions & 5 deletions src/main/java/de/presti/ree6/sql/SQLSession.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.presti.ree6.sql;

import com.google.gson.JsonElement;
import de.presti.ree6.main.Main;
import de.presti.ree6.utils.data.TypUtil;
import jakarta.persistence.AttributeConverter;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -38,7 +39,7 @@ public class SQLSession {
* Build a new SessionFactory or return the current one.
* @return The SessionFactory.
*/
public static SessionFactory buildSessionFactory() {
public static SessionFactory buildSessionFactory(String username, String password) {
if (sessionFactory != null) return getSessionFactory();

try {
Expand All @@ -47,8 +48,13 @@ public static SessionFactory buildSessionFactory() {
properties.put("hibernate.connection.datasource", "com.zaxxer.hikari.HikariDataSource");
properties.put("hibernate.connection.provider_class", "org.hibernate.hikaricp.internal.HikariCPConnectionProvider");
properties.put("hibernate.connection.url", jdbcURL);
properties.put("hibernate.hikari.maximumPoolSize", maxPoolSize);
properties.put("hibernate.connection.username", username);
properties.put("hibernate.connection.password", password);
properties.put("hibernate.hikari.maximumPoolSize", String.valueOf(maxPoolSize));
properties.put("hibernate.dialect","org.hibernate.dialect.MariaDBDialect");
properties.put("hibernate.hbm2ddl.auto", "update");
properties.put("hibernate.show_sql", true);
properties.put("hibernate.format_sql", true);
configuration.addProperties(properties);
configuration.addPackage("de.presti.ree6.sql.entities");
configuration.addAttributeConverter(new AttributeConverter<JsonElement, Blob>() {
Expand Down Expand Up @@ -78,10 +84,9 @@ public String convertToDatabaseColumn(byte[] dbData) {
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();

return sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
catch (Throwable ex) {
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
log.error("Initial SessionFactory creation failed." + ex);
log.error("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}
Expand Down Expand Up @@ -123,6 +128,10 @@ public static int getMaxPoolSize() {
* @return The SessionFactory.
*/
public static SessionFactory getSessionFactory() {
if (sessionFactory == null)
return sessionFactory = buildSessionFactory(
Main.getInstance().getConfig().getConfiguration().getString("hikari.sql.user"),
Main.getInstance().getConfig().getConfiguration().getString("hikari.sql.pw"));
return sessionFactory;
}
}
14 changes: 7 additions & 7 deletions src/main/java/de/presti/ree6/sql/entities/Recording.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package de.presti.ree6.sql.entities;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import de.presti.ree6.utils.others.RandomUtils;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.persistence.*;

/**
* This class is used to represent a Ree6-Voice-Recording, in our Database.
Expand Down Expand Up @@ -42,14 +40,16 @@ public class Recording {
/**
* The WAV-File bytes.
*/
@Convert
@Column(name = "recording")
byte[] recording;

/**
* An JsonArray containing the IDs of the Users who have participated in the Recording.
*/
@Convert
@Column(name = "participants")
JsonArray jsonArray;
JsonElement jsonArray;

/**
* Value used to tell us when this entry was made.
Expand All @@ -71,7 +71,7 @@ public Recording() {
* @param recording the WAV-File bytes.
* @param jsonArray an JsonArray containing the IDs of the Users who have participated in the Recording.
*/
public Recording(String guildId, String voiceId, String creatorId, byte[] recording, JsonArray jsonArray) {
public Recording(String guildId, String voiceId, String creatorId, byte[] recording, JsonElement jsonArray) {
this.identifier = RandomUtils.getRandomBase64String(16);
this.guildId = guildId;
this.voiceId = voiceId;
Expand Down Expand Up @@ -126,7 +126,7 @@ public byte[] getRecording() {
* @return the IDs of the Users who have participated in the Recording.
*/
public JsonArray getJsonArray() {
return jsonArray;
return jsonArray.getAsJsonArray();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ChannelStats {
* The PrimaryKey of the Entity.
*/
@Id
@GeneratedValue
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "id")
private int id;

Expand Down

0 comments on commit 77683a5

Please sign in to comment.