Add file saving support to NotificationServer
- Implement file saving to disk in `NotificationServer` with optional UUID-based organization. - Use `Config` to configure `DOWNLOAD_DIR` and `PORT` for better flexibility. - Update HTTP responses for improved clarity on file download and save operations.
This commit is contained in:
parent
48a01e0a98
commit
6bb50a57b6
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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/<file>.json endpoint.
|
||||
|
|
@ -101,8 +105,25 @@ public class NotificationServer {
|
|||
private void downloadFile(Context ctx, CompletableFuture<Optional<byte[]>> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user