File tree Expand file tree Collapse file tree
src/main/java/com/example/store/product Expand file tree Collapse file tree Original file line number Diff line number Diff line change 33import com .example .store .product .domain .Product ;
44import com .example .store .product .infrastructure .repository .implementation .ProductRepositoryImplV2 ;
55import lombok .RequiredArgsConstructor ;
6+ import org .springframework .core .io .Resource ;
7+ import org .springframework .core .io .UrlResource ;
68import org .springframework .data .domain .Page ;
79import org .springframework .data .domain .Pageable ;
810import org .springframework .stereotype .Service ;
911
12+ import java .net .MalformedURLException ;
13+ import java .nio .file .Path ;
14+ import java .nio .file .Paths ;
15+
1016@ Service
1117@ RequiredArgsConstructor
1218public class ProductServiceImplV2 {
@@ -20,4 +26,16 @@ public Page<Product> findAll(Pageable pageable) {
2026 public Page <Product > findByNameContaining (String name , Pageable pageable ) {
2127 return productRepositoryImplV2 .findAllByProductNameContaining (name , pageable );
2228 }
29+
30+ public Resource findImageByName (String name ) {
31+ Path imagePath = Paths .get ("public/images" , name );
32+ if (!imagePath .toFile ().exists ()) {
33+ throw new IllegalArgumentException ("Image not found: " + name );
34+ }
35+ try {
36+ return new UrlResource (imagePath .toUri ());
37+ } catch (MalformedURLException e ) {
38+ throw new IllegalArgumentException ("Error retrieving image: " + name , e );
39+ }
40+ }
2341}
Original file line number Diff line number Diff line change 44import com .example .store .product .domain .Product ;
55import io .swagger .v3 .oas .annotations .Operation ;
66import lombok .RequiredArgsConstructor ;
7+ import org .springframework .core .io .Resource ;
78import org .springframework .data .domain .Page ;
89import org .springframework .data .domain .Pageable ;
910import org .springframework .data .web .PageableDefault ;
1011import org .springframework .http .HttpStatus ;
1112import org .springframework .http .ResponseEntity ;
12- import org .springframework .web .bind .annotation .GetMapping ;
13- import org .springframework .web .bind .annotation .RequestMapping ;
14- import org .springframework .web .bind .annotation .RequestParam ;
15- import org .springframework .web .bind .annotation .RestController ;
13+ import org .springframework .web .bind .annotation .*;
1614
1715@ RestController
1816@ RequestMapping ("/api/v2/products" )
@@ -32,4 +30,10 @@ public ResponseEntity<Page<Product>> findAll(@PageableDefault Pageable pageable)
3230 public ResponseEntity <Page <Product >> findByNameContaining (@ RequestParam String name , @ PageableDefault Pageable pageable ) {
3331 return ResponseEntity .status (HttpStatus .OK ).body (productService .findByNameContaining (name , pageable ));
3432 }
33+
34+ @ GetMapping ("/images/{fileName:.+}" )
35+ @ 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 ));
38+ }
3539}
You can’t perform that action at this time.
0 commit comments