Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-chu committed Jun 20, 2024
2 parents 509b885 + 164f445 commit e9b46fb
Showing 1 changed file with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,48 @@ 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)
.get("/api/downloaders/{downloaderName}/torrents", ctx -> handleDownloaderTorrents(ctx, ctx.pathParam("downloaderName")), Role.USER_READ)
.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;
}
// 可能重命名了?
Expand All @@ -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()));
}
}

Expand Down

0 comments on commit e9b46fb

Please sign in to comment.