Skip to content

Commit

Permalink
feat: Presigned url 생성 요청 구현 (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
OziinG committed Dec 4, 2024
1 parent 3368e23 commit 56f1289
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.ioteatime.meonghanyangserver.cctv.domain.CctvEntity;

public interface CctvRepository {
boolean existsById(Long cctvId);

boolean existsByKvsChannelName(String kvsChannelName);

boolean existsByThingId(String thingId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public class CctvRepositoryImpl implements CctvRepository {
private final JPAQueryFactory jpaQueryFactory;
private final JpaCctvRepository jpaCctvRepository;

public boolean existsById(Long cctvId) {
return jpaCctvRepository.existsById(cctvId);
}

public boolean existsByKvsChannelName(String kvsChannelName) {
return jpaCctvRepository.existsByKvsChannelName(kvsChannelName);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.ioteatime.meonghanyangserver.common.type;

public enum ImageSuccessType implements SuccessTypeCode {
GET_IMAGE_URL(200, "OK", "Presigned Url 조회에 성공하였습니다.");
CREATE_PRESIGNED_URL(200, "OK", "Presigned Url 생성에 성공하였습니다.");

private final Integer code;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.ioteatime.meonghanyangserver.image.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.ioteatime.meonghanyangserver.common.api.Api;
import org.ioteatime.meonghanyangserver.image.dto.response.ImageSaveUrlResponse;
import org.springframework.web.bind.annotation.PathVariable;

@Tag(name = "Image Api", description = "Image 관련 API 목록입니다.")
public interface ImageDeviceApi {
@Operation(summary = "이미지 저장을 위한 presigned url을 발급 받습니다.", description = "담당자: 임지인")
public Api<ImageSaveUrlResponse> getImageSaveUrl(
@PathVariable Long cctvId, @PathVariable String fileName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.ioteatime.meonghanyangserver.image.controller;

import lombok.RequiredArgsConstructor;
import org.ioteatime.meonghanyangserver.common.api.Api;
import org.ioteatime.meonghanyangserver.common.type.ImageSuccessType;
import org.ioteatime.meonghanyangserver.image.dto.response.ImageSaveUrlResponse;
import org.ioteatime.meonghanyangserver.image.service.ImageService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/open-api/image")
public class ImageDeviceController implements ImageDeviceApi {
private final ImageService imageService;

@GetMapping("/{cctvId}/{fileName}")
public Api<ImageSaveUrlResponse> getImageSaveUrl(
@PathVariable Long cctvId, @PathVariable String fileName) {
ImageSaveUrlResponse imageSaveUrlResponse = imageService.getImageSaveUrl(cctvId, fileName);
return Api.success(ImageSuccessType.CREATE_PRESIGNED_URL, imageSaveUrlResponse);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.ioteatime.meonghanyangserver.image.dto.response;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;

@Schema(description = "이미지 저장 Url 요청 응답")
public record ImageSaveUrlResponse(
@NotNull
@Schema(
description = "이미지 저장용 presigned url",
example = "https://bucket.s3.ap-northeast-2.amazonaws.com/test?")
String presignedUrl) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.ioteatime.meonghanyangserver.image.mapper;

import org.ioteatime.meonghanyangserver.image.dto.response.ImageSaveUrlResponse;

public class ImageResponseMapper {
public static ImageSaveUrlResponse form(String presignedUrl) {
return new ImageSaveUrlResponse(presignedUrl);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.ioteatime.meonghanyangserver.image.service;

import com.amazonaws.HttpMethod;
import lombok.RequiredArgsConstructor;
import org.ioteatime.meonghanyangserver.cctv.repository.CctvRepository;
import org.ioteatime.meonghanyangserver.clients.s3.S3Client;
import org.ioteatime.meonghanyangserver.common.exception.NotFoundException;
import org.ioteatime.meonghanyangserver.common.type.CctvErrorType;
import org.ioteatime.meonghanyangserver.image.dto.response.ImageSaveUrlResponse;
import org.ioteatime.meonghanyangserver.image.mapper.ImageResponseMapper;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ImageService {
private final S3Client s3Client;
private final CctvRepository cctvRepository;

public ImageSaveUrlResponse getImageSaveUrl(Long cctvId, String fileName) {
if (!cctvRepository.existsById(cctvId)) {
throw new NotFoundException(CctvErrorType.NOT_FOUND);
}

String presignedUrl = s3Client.generatePreSignUrl(fileName, HttpMethod.PUT);
return ImageResponseMapper.form(presignedUrl);
}
}

0 comments on commit 56f1289

Please sign in to comment.