remote SMTP from docker compose
This commit is contained in:
@@ -5,10 +5,15 @@ import io.github.lumijiez.data.entities.PushData;
|
|||||||
import io.github.lumijiez.raft.Raft;
|
import io.github.lumijiez.raft.Raft;
|
||||||
import io.javalin.Javalin;
|
import io.javalin.Javalin;
|
||||||
import io.javalin.http.Context;
|
import io.javalin.http.Context;
|
||||||
|
import io.javalin.http.UploadedFile;
|
||||||
import jakarta.persistence.EntityManager;
|
import jakarta.persistence.EntityManager;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.StandardCopyOption;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static final String HOST = System.getenv().getOrDefault("HOSTNAME", "localhost");
|
public static final String HOST = System.getenv().getOrDefault("HOSTNAME", "localhost");
|
||||||
public static final int PORT = Integer.parseInt(System.getenv().getOrDefault("UDP_PORT", "8084"));
|
public static final int PORT = Integer.parseInt(System.getenv().getOrDefault("UDP_PORT", "8084"));
|
||||||
@@ -32,6 +37,24 @@ public class Main {
|
|||||||
|
|
||||||
app.post("/push", Main::handlePush);
|
app.post("/push", Main::handlePush);
|
||||||
|
|
||||||
|
app.post("/upload", ctx -> {
|
||||||
|
String description = ctx.formParam("description");
|
||||||
|
|
||||||
|
UploadedFile uploadedFile = ctx.uploadedFile("file");
|
||||||
|
|
||||||
|
if (uploadedFile != null) {
|
||||||
|
Path destination = Path.of("uploads", uploadedFile.filename());
|
||||||
|
Files.createDirectories(destination.getParent());
|
||||||
|
Files.copy(uploadedFile.content(), destination, StandardCopyOption.REPLACE_EXISTING);
|
||||||
|
|
||||||
|
ctx.status(200).json("File uploaded successfully: " + uploadedFile.filename() + "\nDescription: " + description);
|
||||||
|
System.out.println("File uploaded successfully: " + uploadedFile.filename() + "\nDescription: " + description);
|
||||||
|
} else {
|
||||||
|
ctx.status(400).json("No file uploaded");
|
||||||
|
logger.error("No file uploaded");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
logger.info("HTTP server started on port {}", HTTP_PORT);
|
logger.info("HTTP server started on port {}", HTTP_PORT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,17 @@
|
|||||||
package io.github.lumijiez.ftp;
|
package io.github.lumijiez.ftp;
|
||||||
|
|
||||||
import io.github.lumijiez.Main;
|
import io.github.lumijiez.Main;
|
||||||
|
import io.github.lumijiez.http.AddressHolder;
|
||||||
import org.apache.commons.net.ftp.FTPClient;
|
import org.apache.commons.net.ftp.FTPClient;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.*;
|
||||||
import java.io.FileOutputStream;
|
import java.net.HttpURLConnection;
|
||||||
import java.io.IOException;
|
import java.net.URL;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
import java.util.Timer;
|
import java.util.Timer;
|
||||||
import java.util.TimerTask;
|
import java.util.TimerTask;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
public class FTPFetcher {
|
public class FTPFetcher {
|
||||||
String ftpServer = "ftp_server";
|
String ftpServer = "ftp_server";
|
||||||
@@ -53,6 +57,12 @@ public class FTPFetcher {
|
|||||||
boolean success = ftpClient.retrieveFile(fileName, fos);
|
boolean success = ftpClient.retrieveFile(fileName, fos);
|
||||||
if (success) {
|
if (success) {
|
||||||
Main.logger.info("File downloaded: {}", localFile.getName());
|
Main.logger.info("File downloaded: {}", localFile.getName());
|
||||||
|
|
||||||
|
sendFileToMultipartEndpoint(localFile,
|
||||||
|
"http://"
|
||||||
|
+ AddressHolder.getInstance().getHost()
|
||||||
|
+ ":" + AddressHolder.getInstance().getPort()
|
||||||
|
+ "/upload", "Description of " + fileName);
|
||||||
} else {
|
} else {
|
||||||
Main.logger.error("Failed to download the file: {}", fileName);
|
Main.logger.error("Failed to download the file: {}", fileName);
|
||||||
}
|
}
|
||||||
@@ -78,4 +88,68 @@ public class FTPFetcher {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void sendFileToMultipartEndpoint(File file, String endpointUrl, String description) {
|
||||||
|
try {
|
||||||
|
String boundary = UUID.randomUUID().toString();
|
||||||
|
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) new URL(endpointUrl).openConnection();
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
connection.setRequestMethod("POST");
|
||||||
|
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
|
||||||
|
|
||||||
|
try (OutputStream outputStream = connection.getOutputStream();
|
||||||
|
PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8), true)) {
|
||||||
|
|
||||||
|
writer.append("--").append(boundary).append("\r\n");
|
||||||
|
writer.append("Content-Disposition: form-data; name=\"description\"\r\n");
|
||||||
|
writer.append("Content-Type: text/plain; charset=UTF-8\r\n\r\n");
|
||||||
|
writer.append(description).append("\r\n");
|
||||||
|
|
||||||
|
writer.append("--").append(boundary).append("\r\n");
|
||||||
|
writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"")
|
||||||
|
.append(file.getName()).append("\"\r\n");
|
||||||
|
writer.append("Content-Type: application/octet-stream\r\n\r\n");
|
||||||
|
writer.flush();
|
||||||
|
|
||||||
|
try (FileInputStream fileInputStream = new FileInputStream(file)) {
|
||||||
|
byte[] buffer = new byte[4096];
|
||||||
|
int bytesRead;
|
||||||
|
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
|
||||||
|
outputStream.write(buffer, 0, bytesRead);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.append("\r\n--").append(boundary).append("--\r\n");
|
||||||
|
writer.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
int responseCode = connection.getResponseCode();
|
||||||
|
if (responseCode == 200) {
|
||||||
|
System.out.println("File successfully uploaded to the endpoint " + AddressHolder.getInstance().getHost() + ":" + AddressHolder.getInstance().getPort() + ": " + file.getName());
|
||||||
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
|
||||||
|
String line;
|
||||||
|
StringBuilder response = new StringBuilder();
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
response.append(line);
|
||||||
|
}
|
||||||
|
System.out.println("Server response: " + response);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try (BufferedReader errorReader = new BufferedReader(new InputStreamReader(connection.getErrorStream()))) {
|
||||||
|
String line;
|
||||||
|
StringBuilder errorResponse = new StringBuilder();
|
||||||
|
while ((line = errorReader.readLine()) != null) {
|
||||||
|
errorResponse.append(line);
|
||||||
|
}
|
||||||
|
System.err.println("Failed to upload file. Response code: " + responseCode);
|
||||||
|
System.err.println("Error details: " + errorResponse);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
System.err.println("Error sending file to endpoint: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user