diff --git a/src/main/java/com/alttd/webinterface/WebInterface.java b/src/main/java/com/alttd/webinterface/WebInterface.java index 51236c1..1ec497d 100644 --- a/src/main/java/com/alttd/webinterface/WebInterface.java +++ b/src/main/java/com/alttd/webinterface/WebInterface.java @@ -21,7 +21,7 @@ import lombok.extern.slf4j.Slf4j; public class WebInterface { private final ProxyServer server; private NotificationServer notificationServer; - private static final int HTTP_PORT = 8080; // Default port, could be made configurable + private static final int HTTP_PORT = Config.PORT; @Inject public WebInterface(ProxyServer proxyServer) { diff --git a/src/main/java/com/alttd/webinterface/config/Config.java b/src/main/java/com/alttd/webinterface/config/Config.java index 88c21d3..7f6e6ed 100644 --- a/src/main/java/com/alttd/webinterface/config/Config.java +++ b/src/main/java/com/alttd/webinterface/config/Config.java @@ -214,4 +214,14 @@ public final class Config { DOWNLOAD_ENDPOINT = getString("download-endpoint", DOWNLOAD_ENDPOINT); LOGIN_CODE_ENDPOINT = getString("login-code-endpoint", LOGIN_CODE_ENDPOINT); } + + public static int PORT = 8080; + private static void web_server() { + PORT = getInt("web-server.port", PORT); + } + + public static String DOWNLOAD_DIR = "/mnt/configs/AltitudeParticles/particles"; + private static void download_dir() { + DOWNLOAD_DIR = getString("download-dir", DOWNLOAD_DIR); + } } diff --git a/src/main/java/com/alttd/webinterface/http/NotificationServer.java b/src/main/java/com/alttd/webinterface/http/NotificationServer.java index d6de60e..f0d9fc9 100644 --- a/src/main/java/com/alttd/webinterface/http/NotificationServer.java +++ b/src/main/java/com/alttd/webinterface/http/NotificationServer.java @@ -1,15 +1,19 @@ package com.alttd.webinterface.http; +import com.alttd.webinterface.config.Config; import com.alttd.webinterface.web_interact.FileDownloadService; import io.javalin.Javalin; import io.javalin.http.Context; import io.javalin.http.HttpStatus; import lombok.extern.slf4j.Slf4j; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Optional; import java.util.UUID; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; /** * HTTP server that exposes the /notify/.json endpoint. @@ -101,8 +105,25 @@ public class NotificationServer { private void downloadFile(Context ctx, CompletableFuture> optionalCompletableFuture) { optionalCompletableFuture.thenAccept(fileData -> { if (fileData.isPresent()) { - ctx.contentType("application/json"); - ctx.result(fileData.get()); + String fileName = ctx.pathParam("file") + ".json"; + String uuid = null; + try { + uuid = ctx.pathParam("uuid"); + } catch (Exception ignored) { + log.debug("UUID not present"); + } + + boolean saved = saveFileToDisk(fileData.get(), fileName, uuid); + + if (saved) { + ctx.status(HttpStatus.OK); + ctx.contentType("text/plain"); + ctx.result("File downloaded and saved successfully"); + } else { + ctx.status(HttpStatus.INTERNAL_SERVER_ERROR); + ctx.contentType("text/plain"); + ctx.result("Failed to save file to disk"); + } } else { ctx.status(HttpStatus.NOT_FOUND); ctx.contentType("text/plain"); @@ -116,4 +137,34 @@ public class NotificationServer { return null; }); } + + /** + * Saves the downloaded file to disk. + * + * @param fileData The binary content of the file + * @param fileName The name of the file + * @param uuid Optional UUID for organizing files + * @return true if the file was saved successfully, false otherwise + */ + private boolean saveFileToDisk(byte[] fileData, String fileName, String uuid) { + try { + Path basePath = Paths.get(Config.DOWNLOAD_DIR); + + Path filePath; + if (uuid != null) { + filePath = basePath.resolve(uuid).resolve(fileName); + } else { + filePath = basePath.resolve(fileName); + } + + Files.createDirectories(filePath.getParent()); + Files.write(filePath, fileData); + + log.info("File saved to: {}", filePath.toAbsolutePath()); + return true; + } catch (IOException e) { + log.error("Error saving file to disk", e); + return false; + } + } }