2024-04-10 07:43:38 +01:00
|
|
|
package cc.fascinated.controller;
|
|
|
|
|
|
|
|
import cc.fascinated.common.PlayerUtils;
|
|
|
|
import cc.fascinated.model.player.Player;
|
|
|
|
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.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();
|
|
|
|
private final PlayerService playerManagerService;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
public PlayerController(PlayerService playerManagerService) {
|
|
|
|
this.playerManagerService = playerManagerService;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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)
|
|
|
|
.body(playerManagerService.getPlayer(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
@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 07:43:38 +01:00
|
|
|
Player player = playerManagerService.getPlayer(id);
|
|
|
|
Skin.Parts skinPart = Skin.Parts.fromName(part);
|
2024-04-10 10:45:17 +01:00
|
|
|
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)
|
2024-04-10 10:45:17 +01:00
|
|
|
.header(HttpHeaders.CONTENT_DISPOSITION, dispositionHeader.formatted(player.getUsername()))
|
2024-04-10 07:43:38 +01:00
|
|
|
.body(PlayerUtils.getSkinPartBytes(player.getSkin(), skinPart, size));
|
|
|
|
}
|
|
|
|
}
|