From cb7c2e162f193381a9287081519264abfab8e763 Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 10 Apr 2024 10:45:17 +0100 Subject: [PATCH] add an option to download the image instead of viewing it --- .../java/cc.fascinated/controller/PlayerController.java | 7 +++++-- .../java/cc.fascinated/controller/ServerController.java | 8 ++++++-- target/classes/templates/index.html | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/java/cc.fascinated/controller/PlayerController.java b/src/main/java/cc.fascinated/controller/PlayerController.java index 7228556..d3cebca 100644 --- a/src/main/java/cc.fascinated/controller/PlayerController.java +++ b/src/main/java/cc.fascinated/controller/PlayerController.java @@ -45,15 +45,18 @@ public class PlayerController { @Parameter(description = "The UUID or Username of the player", example = "ImFascinated") @PathVariable String id, @Parameter(description = "The size of the image", example = "256") - @RequestParam(required = false, defaultValue = "256") int size) { + @RequestParam(required = false, defaultValue = "256") int size, + @Parameter(description = "Whether to download the image", example = "false") + @RequestParam(required = false, defaultValue = "false") boolean download) { Player player = playerManagerService.getPlayer(id); Skin.Parts skinPart = Skin.Parts.fromName(part); + String dispositionHeader = download ? "attachment; filename=%s.png" : "inline; filename=%s.png"; // Return the part image return ResponseEntity.ok() .cacheControl(cacheControl) .contentType(MediaType.IMAGE_PNG) - .header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=%s.png".formatted(player.getUsername())) + .header(HttpHeaders.CONTENT_DISPOSITION, dispositionHeader.formatted(player.getUsername())) .body(PlayerUtils.getSkinPartBytes(player.getSkin(), skinPart, size)); } } diff --git a/src/main/java/cc.fascinated/controller/ServerController.java b/src/main/java/cc.fascinated/controller/ServerController.java index 5ba1ea9..35c6eba 100644 --- a/src/main/java/cc.fascinated/controller/ServerController.java +++ b/src/main/java/cc.fascinated/controller/ServerController.java @@ -35,13 +35,17 @@ public class ServerController { @GetMapping(value = "/icon/{hostnameAndPort}", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity getServerIcon( @Parameter(description = "The hostname and port of the server", example = "play.hypixel.net") - @PathVariable String hostnameAndPort) { + @PathVariable String hostnameAndPort, + @Parameter(description = "Whether to download the image", example = "false") + @RequestParam(required = false, defaultValue = "false") boolean download) { Tuple host = ServerUtils.getHostnameAndPort(hostnameAndPort); String hostname = host.getLeft(); int port = host.getRight(); + String dispositionHeader = download ? "attachment; filename=%s.png" : "inline; filename=%s.png"; + return ResponseEntity.ok() .contentType(MediaType.IMAGE_PNG) - .header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=%s.png".formatted(ServerUtils.getAddress(hostname, port))) + .header(HttpHeaders.CONTENT_DISPOSITION, dispositionHeader.formatted(ServerUtils.getAddress(hostname, port))) .body(serverService.getServerFavicon(hostname, port)); } } diff --git a/target/classes/templates/index.html b/target/classes/templates/index.html index 390183e..d20880a 100644 --- a/target/classes/templates/index.html +++ b/target/classes/templates/index.html @@ -21,7 +21,7 @@

Player Data: ???

-

Player Data: ???

+

Server Data: ???

Swagger Docs: ???