12
12
import org .springframework .web .bind .annotation .GetMapping ;
13
13
import org .springframework .web .bind .annotation .PathVariable ;
14
14
import org .springframework .web .bind .annotation .RequestMapping ;
15
+ import org .springframework .web .bind .annotation .RequestParam ;
15
16
import org .springframework .web .bind .annotation .RestController ;
16
17
18
+ import javax .imageio .ImageIO ;
19
+ import java .awt .image .BufferedImage ;
20
+ import java .io .ByteArrayOutputStream ;
17
21
import java .io .IOException ;
18
22
import java .io .InputStream ;
19
23
24
+
20
25
@ RestController
21
26
@ RequestMapping ("/api/v1/textures" )
22
27
public class ImageController {
@@ -31,25 +36,48 @@ public ImageController(ResourceLoader resourceLoader) {
31
36
}
32
37
33
38
@ 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 ) {
35
44
String filePath = "file:" + assetsBasePath + "/png_files/" + category + "/" + size + "x" + size + "/" + filename + ".png" ;
36
45
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 );
46
66
} catch (IOException e ) {
47
67
e .printStackTrace ();
48
- return ResponseEntity .status (HttpStatus .INTERNAL_SERVER_ERROR ).build ();
68
+ return ResponseEntity .status (HttpStatus .FAILED_DEPENDENCY ).build ();
49
69
}
50
- } else {
51
- return ResponseEntity .status (HttpStatus .NOT_FOUND ).build ();
70
+ imageBytes = imageByteArrayOutputStream .toByteArray ();
71
+ downloadName = filename + "_" + (size * scale ) + "x" + (size * scale );
72
+
52
73
}
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
+
53
81
}
54
82
55
83
}
0 commit comments