Files
Backend/src/main/java/cc.fascinated/controller/PlayerController.java

66 lines
3.0 KiB
Java
Raw Normal View History

2024-04-10 07:43:38 +01:00
package cc.fascinated.controller;
2024-04-10 12:41:35 +01:00
import cc.fascinated.model.cache.CachedPlayer;
2024-04-11 00:49:16 +01:00
import cc.fascinated.model.cache.CachedPlayerName;
2024-04-10 07:43:38 +01:00
import cc.fascinated.model.player.Skin;
import cc.fascinated.service.PlayerService;
2024-04-10 10:26:24 +01:00
import io.swagger.v3.oas.annotations.Parameter;
2024-04-10 09:51:31 +01:00
import io.swagger.v3.oas.annotations.tags.Tag;
2024-04-10 07:43:38 +01:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.CacheControl;
2024-04-10 09:19:02 +01:00
import org.springframework.http.HttpHeaders;
2024-04-10 07:43:38 +01:00
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
2024-04-10 07:43:38 +01:00
import java.util.concurrent.TimeUnit;
@RestController
2024-04-10 09:51:31 +01:00
@Tag(name = "Player Controller", description = "The Player Controller is used to get information about a player.")
2024-04-10 07:43:38 +01:00
@RequestMapping(value = "/player/")
public class PlayerController {
private final CacheControl cacheControl = CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic();
2024-04-10 12:41:35 +01:00
private final PlayerService playerService;
2024-04-10 07:43:38 +01:00
@Autowired
public PlayerController(PlayerService playerManagerService) {
2024-04-10 12:41:35 +01:00
this.playerService = playerManagerService;
2024-04-10 07:43:38 +01:00
}
@ResponseBody
@GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
2024-04-10 10:26:24 +01:00
public ResponseEntity<?> getPlayer(
2024-04-10 11:09:09 +01:00
@Parameter(description = "The UUID or Username of the player", example = "ImFascinated") @PathVariable String id) {
2024-04-10 07:43:38 +01:00
return ResponseEntity.ok()
.cacheControl(cacheControl)
2024-04-10 12:41:35 +01:00
.body(playerService.getPlayer(id));
2024-04-10 07:43:38 +01:00
}
@ResponseBody
@GetMapping(value = "/uuid/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
2024-04-11 00:50:57 +01:00
public CachedPlayerName getPlayerUuid(
@Parameter(description = "The UUID or Username of the player", example = "ImFascinated") @PathVariable String id) {
2024-04-11 00:50:57 +01:00
return playerService.usernameToUuid(id);
}
2024-04-10 07:43:38 +01:00
@GetMapping(value = "/{part}/{id}")
2024-04-10 10:26:24 +01:00
public ResponseEntity<?> getPlayerHead(
2024-04-10 11:09:09 +01:00
@Parameter(description = "The part of the skin", example = "head") @PathVariable String part,
@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,
@Parameter(description = "Whether to download the image") @RequestParam(required = false, defaultValue = "false") boolean download) {
2024-04-10 12:41:35 +01:00
CachedPlayer player = playerService.getPlayer(id);
2024-04-10 07:43:38 +01:00
Skin.Parts skinPart = Skin.Parts.fromName(part);
String dispositionHeader = download ? "attachment; filename=%s.png" : "inline; filename=%s.png";
2024-04-10 07:43:38 +01:00
// Return the part image
return ResponseEntity.ok()
.cacheControl(cacheControl)
.contentType(MediaType.IMAGE_PNG)
.header(HttpHeaders.CONTENT_DISPOSITION, dispositionHeader.formatted(player.getUsername()))
2024-04-10 12:41:35 +01:00
.body(playerService.getSkinPart(player, skinPart, size).getBytes());
2024-04-10 07:43:38 +01:00
}
}