File tree Expand file tree Collapse file tree
src/main/java/com/example/store/product/infrastructure Expand file tree Collapse file tree Original file line number Diff line number Diff line change 99import org .springframework .data .domain .Pageable ;
1010import org .springframework .data .web .PageableDefault ;
1111import org .springframework .http .HttpStatus ;
12+ import org .springframework .http .MediaType ;
1213import org .springframework .http .ResponseEntity ;
1314import org .springframework .web .bind .annotation .*;
1415
16+ import java .io .IOException ;
17+ import java .nio .file .Files ;
18+ import java .nio .file .Paths ;
19+
1520@ RestController
1621@ RequestMapping ("/api/v2/products" )
1722@ RequiredArgsConstructor
@@ -33,7 +38,12 @@ public ResponseEntity<Page<Product>> findByNameContaining(@RequestParam String n
3338
3439 @ GetMapping ("/images/{fileName:.+}" )
3540 @ Operation (summary = "Return a product image by file name" )
36- public ResponseEntity <Resource > getImage (@ PathVariable String fileName ) {
37- return ResponseEntity .status (HttpStatus .OK ).body (productService .findImageByName (fileName ));
41+ public ResponseEntity <Resource > getImage (@ PathVariable String fileName ) throws IOException {
42+ Resource image = productService .findImageByName (fileName );
43+ String contentType = Files .probeContentType (Paths .get (image .getFile ().getAbsolutePath ()));
44+ if (contentType == null ) {
45+ contentType = "application/octet-stream" ; // fallback si no se detecta
46+ }
47+ return ResponseEntity .status (HttpStatus .OK ).contentType (MediaType .parseMediaType (contentType )).body (image );
3848 }
3949}
You can’t perform that action at this time.
0 commit comments