Skip to content

Commit

Permalink
Merge pull request #68 from mju-likelion/feature/s3-config-#65
Browse files Browse the repository at this point in the history
Feature/#65 S3 설정 추가
  • Loading branch information
rnqhstmd authored Jul 30, 2024
2 parents 7c3106f + 294317c commit b6e73d8
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 14 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ dependencies {
annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jakarta"
annotationProcessor "jakarta.annotation:jakarta.annotation-api"
annotationProcessor "jakarta.persistence:jakarta.persistence-api"

implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE' //AWS
}

tasks.named('test') {
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/example/mutsideout_mju/config/S3Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.mutsideout_mju.config;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class S3Config {
@Value("${cloud.aws.credentials.accessKey}")
private String accessKey;
@Value("${cloud.aws.credentials.secretKey}")
private String secretKey;
@Value("${cloud.aws.region.static}")
private String region;

@Bean
public AmazonS3Client amazonS3Client() {
BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);

return (AmazonS3Client) AmazonS3ClientBuilder.standard()
.withRegion(region)
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.example.mutsideout_mju.dto.response.PaginationData;
import com.example.mutsideout_mju.entity.Room;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import org.springframework.data.domain.Page;
Expand All @@ -10,17 +11,8 @@
import java.util.stream.Collectors;

@Getter
@Builder
@AllArgsConstructor
public class RoomListResponseData {
private List<RoomResponseDto> roomList;
private PaginationData pagination;

public static RoomListResponseData from(Page<Room> page){
return RoomListResponseData.builder()
.roomList(page.stream()
.map(room -> RoomResponseDto.fromRoom(room))
.collect(Collectors.toList()))
.pagination(PaginationData.paginationData(page))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ public class RoomResponseDto {
private UUID id;
private String title;
private String createdAt;
private String imageLink;

public static RoomResponseDto fromRoom(Room room){
public static RoomResponseDto fromRoom(Room room, String imageLink){
return RoomResponseDto.builder()
.id(room.getId())
.title(room.getTitle())
.createdAt(room.getCreatedAt().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")))
.imageLink(imageLink)
.build();
}
}
15 changes: 12 additions & 3 deletions src/main/java/com/example/mutsideout_mju/service/RoomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import com.example.mutsideout_mju.dto.request.PaginationDto;
import com.example.mutsideout_mju.dto.request.room.CreateRoomDto;
import com.example.mutsideout_mju.dto.request.room.UpdateRoomDto;
import com.example.mutsideout_mju.dto.response.PaginationData;
import com.example.mutsideout_mju.dto.response.room.RoomListResponseData;
import com.example.mutsideout_mju.dto.response.room.RoomResponseData;
import com.example.mutsideout_mju.dto.response.room.RoomResponseDto;
import com.example.mutsideout_mju.entity.Room;
import com.example.mutsideout_mju.entity.User;
import com.example.mutsideout_mju.exception.ForbiddenException;
import com.example.mutsideout_mju.exception.NotFoundException;
import com.example.mutsideout_mju.exception.errorCode.ErrorCode;
import com.example.mutsideout_mju.repository.RoomRepository;
import com.example.mutsideout_mju.util.S3Service;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand All @@ -28,6 +31,7 @@
@RequiredArgsConstructor
public class RoomService {
private final RoomRepository roomRepository;
private final S3Service s3Service;

// 24시간 지난 방 삭제 (한 시간마다 실행)
@Scheduled(fixedRate = 3600000)
Expand All @@ -38,16 +42,21 @@ public void deleteOldRooms() {

//집중 세션 방 전체 목록 조회
public RoomListResponseData getRoomList(PaginationDto paginationDto) {
//요청받은 페이지 번호, 페이지 크기, 작성순으로 정렬
// 요청받은 페이지 번호, 페이지 크기, 작성순으로 정렬
Pageable pageable = PageRequest.of(paginationDto.getPage(), paginationDto.getPAGE_SIZE(), Sort.by(Sort.Order.desc("createdAt")));

// 비활성화 된 방 삭제 후 해당 페이지 데이터를 모두 가져옴
// 해당 페이지 데이터를 모두 가져옴
Page<Room> roomPage = roomRepository.findAll(pageable);
if (roomPage.getTotalPages() <= paginationDto.getPage() && paginationDto.getPage() != 0) {
throw new NotFoundException(ErrorCode.NOT_FOUND_PAGE);
}

return RoomListResponseData.from(roomPage);
List<RoomResponseDto> roomResponseList = roomPage.getContent().stream()
.map(room -> RoomResponseDto.fromRoom(room, s3Service.getRoomImageLink(room.getLink())))
.collect(Collectors.toList());

PaginationData pagination = PaginationData.paginationData(roomPage);
return new RoomListResponseData(roomResponseList, pagination);
}

//집중 세션 방 상세 조회
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/com/example/mutsideout_mju/util/S3Service.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.example.mutsideout_mju.util;

import com.amazonaws.services.s3.AmazonS3;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
* AWS S3 관련된 기능 구현
* 업로드, 조회
*/
@Service
@RequiredArgsConstructor
public class S3Service {
private final AmazonS3 s3client;
@Value("${cloud.aws.s3.bucket-name}")
private String bucketName;

public String getRoomImageLink(String link) {
String imagePath = getImagePath(link);
return imagePath != null ? generateFileUrl(imagePath) : null;
}

private String generateFileUrl(String key) {
return s3client.getUrl(bucketName.trim(), key).toString();
}

private String getImagePath(String link) {
String basePath = "rooms/";
if (link.contains("google")) {
return basePath + "googlemeet.svg";
} else if (link.contains("zoom")) {
return basePath + "zoom.svg";
} else if (link.contains("discord")) {
return basePath + "discord.svg";
} else {
return null;
}
}
}

0 comments on commit b6e73d8

Please sign in to comment.