From 15d6ff69886d6a33f460532016053c4cef71972b Mon Sep 17 00:00:00 2001 From: Braydon Date: Fri, 2 Jun 2023 19:19:21 -0400 Subject: [PATCH] Initial Commit --- .gitignore | 26 ++++++++ pom.xml | 68 +++++++++++++++++++++ src/main/java/me/braydon/migrator/Main.java | 65 ++++++++++++++++++++ 3 files changed, 159 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/me/braydon/migrator/Main.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d7300f0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,26 @@ +*.class +*.log +*.ctxt +.mtj.tmp/ +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar +hs_err_pid* +replay_pid* +.idea +cmake-build-*/ +*.iws +out/ +build/ +work/ +.idea_modules/ +atlassian-ide-plugin.xml +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties +git.properties \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..8252417 --- /dev/null +++ b/pom.xml @@ -0,0 +1,68 @@ + + + 4.0.0 + + me.braydon + hastebin-migrator + 1.0-SNAPSHOT + + + 8 + ${java.version} + ${java.version} + UTF-8 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + ${java.version} + ${java.version} + + + + org.apache.maven.plugins + maven-shade-plugin + 3.1.0 + + + package + + shade + + + + + + + + src/main/resources + true + + + + + + + + org.projectlombok + lombok + 1.18.28 + provided + + + + + io.lettuce + lettuce-core + 6.2.4.RELEASE + compile + + + \ No newline at end of file diff --git a/src/main/java/me/braydon/migrator/Main.java b/src/main/java/me/braydon/migrator/Main.java new file mode 100644 index 0000000..872370c --- /dev/null +++ b/src/main/java/me/braydon/migrator/Main.java @@ -0,0 +1,65 @@ +package me.braydon.migrator; + +import io.lettuce.core.RedisClient; +import io.lettuce.core.api.StatefulRedisConnection; +import io.lettuce.core.api.async.RedisAsyncCommands; +import lombok.NonNull; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.List; + +/** + * @author Braydon + */ +public class Main { + public static void main(@NonNull String[] args) { + String redisUri = System.getenv("REDIS_URI"); // Get the redis uri env var + assert redisUri != null; // We need this + + File dataDir = new File("data"); // The directory where the files are located + if (!dataDir.exists()) { + dataDir.mkdirs(); + System.out.println("Didn't find the './data' directory, created it for you (place files here)"); + return; + } + File[] files = dataDir.listFiles(); + if (files == null || (files.length == 0)) { // No files to iterate + System.out.println("No files to copy"); + return; + } + try (RedisClient redisClient = RedisClient.create(redisUri); + StatefulRedisConnection connection = redisClient.connect() + ) { + RedisAsyncCommands asyncCommands = connection.async(); + asyncCommands.multi(); // Start the transaction + int copied = 0; // The amount of files copied + for (File file : files) { + if (file.isDirectory()) { // Only care about files + continue; + } + String md5Hash = file.getName(); // The name is the md5 hash of the paste + try { + List fileLines = Files.readAllLines(file.toPath()); // The lines of the line + if (fileLines.isEmpty()) { // Skip empty files + continue; + } + StringBuilder stringBuilder = new StringBuilder(); + for (String fileLine : fileLines) { + stringBuilder.append(fileLine).append("\n"); // Append the line to the string builder + } + String fileContent = stringBuilder.toString(); + System.out.println("Copying '" + md5Hash + "' to Redis"); // Log the copy + asyncCommands.set(md5Hash, fileContent); // Copy the file to Redis + copied++; // Increment the amount of files copied + System.out.println("Done!"); + } catch (IOException ex) { + ex.printStackTrace(); + } + } + asyncCommands.exec(); // Execute the transaction + System.out.println("Copied " + copied + "/" + files.length + " files to Redis"); // Log the copied files + } + } +} \ No newline at end of file