diff --git a/src/main/java/team18/team18_be/slider/controller/SliderController.java b/src/main/java/team18/team18_be/slider/controller/SliderController.java new file mode 100644 index 0000000..ccb5d9a --- /dev/null +++ b/src/main/java/team18/team18_be/slider/controller/SliderController.java @@ -0,0 +1,27 @@ +package team18.team18_be.slider.controller; + +import io.swagger.v3.oas.annotations.tags.Tag; +import java.util.List; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import team18.team18_be.slider.dto.response.SliderImageResponse; +import team18.team18_be.slider.service.SliderService; + +@Tag(name = "슬라이더", description = "슬라이더 관련 API") +@RestController +@RequestMapping("/api/slides") +public class SliderController { + + private final SliderService sliderService; + + public SliderController(SliderService sliderService) { + this.sliderService = sliderService; + } + + @GetMapping + public ResponseEntity> getSliderBanner() { + return ResponseEntity.ok().body(sliderService.getSliderImageUrls()); + } +} diff --git a/src/main/java/team18/team18_be/slider/dto/response/SliderImageResponse.java b/src/main/java/team18/team18_be/slider/dto/response/SliderImageResponse.java new file mode 100644 index 0000000..de88f89 --- /dev/null +++ b/src/main/java/team18/team18_be/slider/dto/response/SliderImageResponse.java @@ -0,0 +1,7 @@ +package team18.team18_be.slider.dto.response; + +public record SliderImageResponse( + String imageUrl +) { + +} diff --git a/src/main/java/team18/team18_be/slider/service/SliderService.java b/src/main/java/team18/team18_be/slider/service/SliderService.java new file mode 100644 index 0000000..e67eb78 --- /dev/null +++ b/src/main/java/team18/team18_be/slider/service/SliderService.java @@ -0,0 +1,39 @@ +package team18.team18_be.slider.service; + +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.Bucket; +import com.google.cloud.storage.Storage; +import java.util.ArrayList; +import java.util.List; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import team18.team18_be.slider.dto.response.SliderImageResponse; + +@Service +public class SliderService { + + public static final String IMAGE = "image/"; + private static final String DIRECTORY_IMAGES = "Slider"; + private final Storage storage; + @Value("${gcp.bucket.name}") + private String BUCKET_NAME; + + public SliderService(Storage storage) { + this.storage = storage; + } + + public List getSliderImageUrls() { + List imageUrls = new ArrayList<>(); + Bucket bucket = storage.get(BUCKET_NAME); + + for (Blob blob : bucket.list(Storage.BlobListOption.prefix(DIRECTORY_IMAGES)).iterateAll()) { + if (!blob.isDirectory() && blob.getContentType() != null && blob.getContentType().startsWith( + IMAGE)) { + String imageUrl = blob.getMediaLink(); + imageUrls.add(new SliderImageResponse(imageUrl)); + } + } + + return imageUrls; + } +}