add an option to download the image instead of viewing it
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Successful in 1m33s

This commit is contained in:
Lee 2024-04-10 10:45:17 +01:00
parent ac8b2e4b74
commit cb7c2e162f
3 changed files with 12 additions and 5 deletions

@ -45,15 +45,18 @@ public class PlayerController {
@Parameter(description = "The UUID or Username of the player", example = "ImFascinated") @Parameter(description = "The UUID or Username of the player", example = "ImFascinated")
@PathVariable String id, @PathVariable String id,
@Parameter(description = "The size of the image", example = "256") @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); Player player = playerManagerService.getPlayer(id);
Skin.Parts skinPart = Skin.Parts.fromName(part); Skin.Parts skinPart = Skin.Parts.fromName(part);
String dispositionHeader = download ? "attachment; filename=%s.png" : "inline; filename=%s.png";
// Return the part image // Return the part image
return ResponseEntity.ok() return ResponseEntity.ok()
.cacheControl(cacheControl) .cacheControl(cacheControl)
.contentType(MediaType.IMAGE_PNG) .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)); .body(PlayerUtils.getSkinPartBytes(player.getSkin(), skinPart, size));
} }
} }

@ -35,13 +35,17 @@ public class ServerController {
@GetMapping(value = "/icon/{hostnameAndPort}", produces = MediaType.APPLICATION_JSON_VALUE) @GetMapping(value = "/icon/{hostnameAndPort}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> getServerIcon( public ResponseEntity<?> getServerIcon(
@Parameter(description = "The hostname and port of the server", example = "play.hypixel.net") @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<String, Integer> host = ServerUtils.getHostnameAndPort(hostnameAndPort); Tuple<String, Integer> host = ServerUtils.getHostnameAndPort(hostnameAndPort);
String hostname = host.getLeft(); String hostname = host.getLeft();
int port = host.getRight(); int port = host.getRight();
String dispositionHeader = download ? "attachment; filename=%s.png" : "inline; filename=%s.png";
return ResponseEntity.ok() return ResponseEntity.ok()
.contentType(MediaType.IMAGE_PNG) .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)); .body(serverService.getServerFavicon(hostname, port));
} }
} }

@ -21,7 +21,7 @@
<div class="flex flex-col mt-3"> <div class="flex flex-col mt-3">
<p>Player Data: <a class="text-blue-600" target=”_blank” th:href="${player_example_url}" th:text="${player_example_url}">???</a></p> <p>Player Data: <a class="text-blue-600" target=”_blank” th:href="${player_example_url}" th:text="${player_example_url}">???</a></p>
<p>Player Data: <a class="text-blue-600" target=”_blank” th:href="${java_server_example_url}" th:text="${java_server_example_url}">???</a></p> <p>Server Data: <a class="text-blue-600" target=”_blank” th:href="${java_server_example_url}" th:text="${java_server_example_url}">???</a></p>
<p>Swagger Docs: <a class="text-blue-600" target=”_blank” th:href="${swagger_url}" th:text="${swagger_url}">???</a></p> <p>Swagger Docs: <a class="text-blue-600" target=”_blank” th:href="${swagger_url}" th:text="${swagger_url}">???</a></p>
</div> </div>
</body> </body>