Skip to content

Commit 33f034f

Browse files
committed
feat(product): enhance image retrieval to include content type detection
1 parent 2df61e4 commit 33f034f

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

src/main/java/com/example/store/product/infrastructure/ProductControllerImplV2.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@
99
import org.springframework.data.domain.Pageable;
1010
import org.springframework.data.web.PageableDefault;
1111
import org.springframework.http.HttpStatus;
12+
import org.springframework.http.MediaType;
1213
import org.springframework.http.ResponseEntity;
1314
import 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
}

0 commit comments

Comments
 (0)