From 164f44530f18ac84c4d08987084761b823ccbac4 Mon Sep 17 00:00:00 2001 From: Gaojianli Date: Wed, 19 Jun 2024 16:38:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E7=A6=BB=E5=88=9B=E5=BB=BA=EF=BC=8C?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/webapi/PBHDownloaderController.java | 39 +++++++++++++++---- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/ghostchu/peerbanhelper/module/impl/webapi/PBHDownloaderController.java b/src/main/java/com/ghostchu/peerbanhelper/module/impl/webapi/PBHDownloaderController.java index a2bb7a628..22202590e 100644 --- a/src/main/java/com/ghostchu/peerbanhelper/module/impl/webapi/PBHDownloaderController.java +++ b/src/main/java/com/ghostchu/peerbanhelper/module/impl/webapi/PBHDownloaderController.java @@ -43,8 +43,8 @@ public boolean isConfigurable() { public void onEnable() { getServer().getWebContainer().javalin() .get("/api/downloaders", this::handleDownloaderList, Role.USER_READ) - .put("/api/downloaders/{downloaderName}", ctx -> handleDownloaderPut(ctx, ctx.pathParam("downloaderName")), Role.USER_WRITE) - .patch("/api/downloaders/{downloaderName}", ctx -> handleDownloaderPut(ctx, ctx.pathParam("downloaderName")), Role.USER_WRITE) + .put("/api/downloaders", this::handleDownloaderPut, Role.USER_WRITE) + .patch("/api/downloaders/{downloaderName}", ctx -> handleDownloaderPatch(ctx, ctx.pathParam("downloaderName")), Role.USER_WRITE) .post("/api/downloaders/test", this::handleDownloaderTest, Role.USER_WRITE) .delete("/api/downloaders/{downloaderName}", ctx -> handleDownloaderDelete(ctx, ctx.pathParam("downloaderName")), Role.USER_WRITE) .get("/api/downloaders/{downloaderName}/status", ctx -> handleDownloaderStatus(ctx, ctx.pathParam("downloaderName")), Role.USER_READ) @@ -52,14 +52,39 @@ public void onEnable() { .get("/api/downloaders/{downloaderName}/torrent/{torrentId}/peers", ctx -> handlePeersInTorrentOnDownloader(ctx, ctx.pathParam("downloaderName"), ctx.pathParam("torrentId")), Role.USER_READ); } - private void handleDownloaderPut(Context ctx, String downloaderName) { + private void handleDownloaderPut(Context ctx) { JsonObject draftDownloader = JsonParser.parseString(ctx.body()).getAsJsonObject(); String name = draftDownloader.get("name").getAsString(); JsonObject config = draftDownloader.get("config").getAsJsonObject(); Downloader downloader = getServer().createDownloader(name, config); if (downloader == null) { ctx.status(HttpStatus.BAD_REQUEST); - ctx.json(Map.of("message", "Unable to create/update downloader, unsupported downloader type?")); + ctx.json(Map.of("message", "Unable to create downloader, unsupported downloader type?")); + return; + } + if (getServer().registerDownloader(downloader)) { + ctx.status(HttpStatus.OK); + ctx.json(Map.of("message", "Download created!")); + } else { + ctx.status(HttpStatus.BAD_REQUEST); + ctx.json(Map.of("message", "Unable to create downloader, same name downloader already registered!")); + } + try { + getServer().saveDownloaders(); + } catch (IOException e) { + ctx.status(HttpStatus.INTERNAL_SERVER_ERROR); + ctx.json(Map.of("message", "Unable to create downloader, I/O error: " + e.getMessage())); + } + } + + private void handleDownloaderPatch(Context ctx, String downloaderName) { + JsonObject draftDownloader = JsonParser.parseString(ctx.body()).getAsJsonObject(); + String name = draftDownloader.get("name").getAsString(); + JsonObject config = draftDownloader.get("config").getAsJsonObject(); + Downloader downloader = getServer().createDownloader(name, config); + if (downloader == null) { + ctx.status(HttpStatus.BAD_REQUEST); + ctx.json(Map.of("message", "Unable to update downloader, unsupported downloader type?")); return; } // 可能重命名了? @@ -68,16 +93,16 @@ private void handleDownloaderPut(Context ctx, String downloaderName) { .forEach(d -> getServer().unregisterDownloader(d)); if (getServer().registerDownloader(downloader)) { ctx.status(HttpStatus.OK); - ctx.json(Map.of("message", "Download created/updated!")); + ctx.json(Map.of("message", "Downloader updated!")); } else { ctx.status(HttpStatus.BAD_REQUEST); - ctx.json(Map.of("message", "Unable to create/update downloader, same name downloader already registered (and failed to remove)!")); + ctx.json(Map.of("message", "Unable to update downloader, same name downloader already registered (and failed to remove)!")); } try { getServer().saveDownloaders(); } catch (IOException e) { ctx.status(HttpStatus.INTERNAL_SERVER_ERROR); - ctx.json(Map.of("message", "Unable to create/update downloader, I/O error: " + e.getMessage())); + ctx.json(Map.of("message", "Unable to update downloader, I/O error: " + e.getMessage())); } }