1212import org .springframework .web .bind .annotation .GetMapping ;
1313import org .springframework .web .bind .annotation .PathVariable ;
1414import org .springframework .web .bind .annotation .RequestMapping ;
15+ import org .springframework .web .bind .annotation .RequestParam ;
1516import org .springframework .web .bind .annotation .RestController ;
1617
18+ import javax .imageio .ImageIO ;
19+ import java .awt .image .BufferedImage ;
20+ import java .io .ByteArrayOutputStream ;
1721import java .io .IOException ;
1822import java .io .InputStream ;
1923
24+
2025@ RestController
2126@ RequestMapping ("/api/v1/textures" )
2227public class ImageController {
@@ -31,25 +36,48 @@ public ImageController(ResourceLoader resourceLoader) {
3136 }
3237
3338 @ GetMapping (value = "/{category}/{filename}/{size}" , produces = MediaType .IMAGE_PNG_VALUE )
34- public ResponseEntity <byte []> getImage (@ PathVariable String category , @ PathVariable String filename , @ PathVariable int size ) {
39+ public ResponseEntity <byte []> getImage (
40+ @ PathVariable String category ,
41+ @ PathVariable String filename ,
42+ @ PathVariable int size ,
43+ @ RequestParam (required = false , defaultValue = "1" ) int scale ) {
3544 String filePath = "file:" + assetsBasePath + "/png_files/" + category + "/" + size + "x" + size + "/" + filename + ".png" ;
3645 Resource resource = resourceLoader .getResource (filePath );
37- if (resource .exists ()) {
38- try (InputStream inputStream = resource .getInputStream ()) {
39- byte [] imageBytes = StreamUtils .copyToByteArray (inputStream );
40- String downloadName = filename + "_" + size + "x" + size ;
41- HttpHeaders headers = new HttpHeaders ();
42- headers .add (HttpHeaders .CONTENT_TYPE , "image/png" );
43- headers .add (HttpHeaders .CONTENT_DISPOSITION , String .format ("inline; filename=%s" , downloadName ));
44-
45- return new ResponseEntity <>(imageBytes , headers , HttpStatus .OK );
46+ if (!resource .exists ()) {
47+ return ResponseEntity .status (HttpStatus .NOT_FOUND ).build ();
48+
49+ }
50+ byte [] imageBytes ;
51+ try (InputStream inputStream = resource .getInputStream ()) {
52+ imageBytes = StreamUtils .copyToByteArray (inputStream );
53+ } catch (IOException e ) {
54+ e .printStackTrace ();
55+ return ResponseEntity .status (HttpStatus .ALREADY_REPORTED ).build ();
56+ }
57+ String downloadName = filename + "_" + size + "x" + size ;
58+ if (scale > 1 ) {
59+ BufferedImage rescaledImage = Rescaler .rescaleImage (filePath , scale , size );
60+ if (rescaledImage == null ) {
61+ return ResponseEntity .status (HttpStatus .CONFLICT ).build ();
62+ }
63+ ByteArrayOutputStream imageByteArrayOutputStream = new ByteArrayOutputStream ();
64+ try {
65+ ImageIO .write (rescaledImage , "png" , imageByteArrayOutputStream );
4666 } catch (IOException e ) {
4767 e .printStackTrace ();
48- return ResponseEntity .status (HttpStatus .INTERNAL_SERVER_ERROR ).build ();
68+ return ResponseEntity .status (HttpStatus .FAILED_DEPENDENCY ).build ();
4969 }
50- } else {
51- return ResponseEntity .status (HttpStatus .NOT_FOUND ).build ();
70+ imageBytes = imageByteArrayOutputStream .toByteArray ();
71+ downloadName = filename + "_" + (size * scale ) + "x" + (size * scale );
72+
5273 }
74+
75+ HttpHeaders headers = new HttpHeaders ();
76+ headers .add (HttpHeaders .CONTENT_TYPE , "image/png" );
77+ headers .add (HttpHeaders .CONTENT_DISPOSITION , String .format ("inline; filename=%s" , downloadName ));
78+ return new ResponseEntity <>(imageBytes , headers , HttpStatus .OK );
79+
80+
5381 }
5482
5583}
0 commit comments