-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
이미지 업로드 과정에서 에러 발생!!!
- Loading branch information
Showing
11 changed files
with
273 additions
and
6 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
src/main/java/com/mpnp/baechelin/store/controller/UserRegisterStoreController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.mpnp.baechelin.store.controller; | ||
|
||
import com.mpnp.baechelin.store.dto.userRegisterStore.UserRegisterStoreRequestDto; | ||
import com.mpnp.baechelin.store.service.UserRegisterStoreService; | ||
import com.mpnp.baechelin.user.entity.user.User; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.ModelAttribute; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/store") | ||
@RequiredArgsConstructor | ||
public class UserRegisterStoreController { | ||
|
||
private final UserRegisterStoreService userRegisterStoreService; | ||
|
||
/** | ||
* 유저 업장 등록 | ||
* @param userRegisterStoreRequestDto | ||
* @return | ||
*/ | ||
@PostMapping("/register") | ||
public ResponseEntity<String> registerStore( | ||
@ModelAttribute UserRegisterStoreRequestDto userRegisterStoreRequestDto) { | ||
userRegisterStoreService.registerStore(userRegisterStoreRequestDto); | ||
return new ResponseEntity<>("업장 등록 성공", HttpStatus.OK); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/com/mpnp/baechelin/store/domain/UserRegisterStoreImg.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.mpnp.baechelin.store.domain; | ||
|
||
import com.mpnp.baechelin.util.TimeStamped; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import javax.persistence.*; | ||
|
||
@Entity | ||
@Getter | ||
@NoArgsConstructor | ||
public class UserRegisterStoreImg extends TimeStamped { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
private int id; | ||
|
||
@Column(nullable = false) | ||
private String userRegisterStoreImageUrl; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "USERREGISTERSTORE_ID", nullable = false) | ||
private UserRegisterStore userRegisterStore; | ||
|
||
@Builder | ||
public UserRegisterStoreImg(String userRegisterStoreImageUrl, UserRegisterStore userRegisterStore) { | ||
this.userRegisterStoreImageUrl = userRegisterStoreImageUrl; | ||
this.userRegisterStore = userRegisterStore; | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/mpnp/baechelin/store/dto/userRegisterStore/UserRegisterStoreImgDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.mpnp.baechelin.store.dto.userRegisterStore; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
public class UserRegisterStoreImgDto { | ||
private String userRegisterStoreImageUrl; | ||
|
||
public UserRegisterStoreImgDto(String userRegisterStoreImageUrl) { | ||
this.userRegisterStoreImageUrl = userRegisterStoreImageUrl; | ||
} | ||
} |
17 changes: 16 additions & 1 deletion
17
...main/java/com/mpnp/baechelin/store/dto/userRegisterStore/userRegisterStoreRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,19 @@ | ||
package com.mpnp.baechelin.store.dto.userRegisterStore; | ||
|
||
public class userRegisterStoreRequestDto { | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@Setter | ||
public class UserRegisterStoreRequestDto { | ||
private String name; | ||
private String address; | ||
private String elevator; | ||
private String toilet; | ||
private String heightDifferent; | ||
private String approach; | ||
private List<MultipartFile> userRegisterStoreImageList; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/java/com/mpnp/baechelin/store/repository/UserRegisterStoreImgRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.mpnp.baechelin.store.repository; | ||
|
||
import com.mpnp.baechelin.store.domain.UserRegisterStoreImg; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface UserRegisterStoreImgRepository extends JpaRepository<UserRegisterStoreImg, Integer> { | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/mpnp/baechelin/store/repository/UserRegisterStoreRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.mpnp.baechelin.store.repository; | ||
|
||
import com.mpnp.baechelin.store.domain.UserRegisterStore; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface UserRegisterStoreRepository extends JpaRepository<UserRegisterStore, Integer> { | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/com/mpnp/baechelin/store/service/UserRegisterStoreService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.mpnp.baechelin.store.service; | ||
|
||
import com.mpnp.baechelin.store.domain.UserRegisterStore; | ||
import com.mpnp.baechelin.store.domain.UserRegisterStoreImg; | ||
import com.mpnp.baechelin.store.dto.userRegisterStore.UserRegisterStoreRequestDto; | ||
import com.mpnp.baechelin.store.repository.UserRegisterStoreImgRepository; | ||
import com.mpnp.baechelin.store.repository.UserRegisterStoreRepository; | ||
import com.mpnp.baechelin.util.AwsS3Manager; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.transaction.Transactional; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class UserRegisterStoreService { | ||
|
||
private final AwsS3Manager awsS3Manager; | ||
private final UserRegisterStoreRepository userRegisterStoreRepository; | ||
private final UserRegisterStoreImgRepository userRegisterStoreImgRepository; | ||
|
||
|
||
/** | ||
* 유저 업장 등록 | ||
* @param userRegisterStoreRequestDto | ||
*/ | ||
public void registerStore(UserRegisterStoreRequestDto userRegisterStoreRequestDto) { | ||
// 업장 등록 | ||
UserRegisterStore userRegisterStore = UserRegisterStore.builder() | ||
.name(userRegisterStoreRequestDto.getName()) | ||
.address(userRegisterStoreRequestDto.getAddress()) | ||
.elevator(userRegisterStoreRequestDto.getElevator()) | ||
.toilet(userRegisterStoreRequestDto.getToilet()) | ||
.heightDifferent(userRegisterStoreRequestDto.getHeightDifferent()) | ||
.approach(userRegisterStoreRequestDto.getApproach()) | ||
.build(); | ||
|
||
// 업장의 이미지 여러개 등록 | ||
// s3에 이미지 업로드 후 url 반환 | ||
List<String> uploadedImage = awsS3Manager.uploadFile(userRegisterStoreRequestDto.getUserRegisterStoreImageList()); | ||
|
||
// saveAll을 위해 userRegisterStoreImg List에 저장 | ||
List<UserRegisterStoreImg> userRegisterStoreImgList = new ArrayList<>(); | ||
|
||
for (String image : uploadedImage) { | ||
UserRegisterStoreImg userRegisterStoreImg = UserRegisterStoreImg.builder() | ||
.userRegisterStoreImageUrl(image) | ||
.userRegisterStore(userRegisterStore) | ||
.build(); | ||
|
||
userRegisterStoreImgList.add(userRegisterStoreImg); | ||
} | ||
|
||
userRegisterStoreRepository.save(userRegisterStore); | ||
userRegisterStoreImgRepository.saveAll(userRegisterStoreImgList); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.mpnp.baechelin.util; | ||
|
||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.model.CannedAccessControlList; | ||
import com.amazonaws.services.s3.model.DeleteObjectRequest; | ||
import com.amazonaws.services.s3.model.ObjectMetadata; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class AwsS3Manager { | ||
|
||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucket; | ||
|
||
private final AmazonS3 amazonS3; | ||
|
||
public List<String> uploadFile(List<MultipartFile> multipartFile) { | ||
List<String> fileNameList = new ArrayList<>(); | ||
|
||
// 파일이 넘어오지 않으면 빈 리스트 반환 | ||
if (multipartFile == null) { | ||
return fileNameList; | ||
} | ||
|
||
// forEach 구문을 통해 multipartFile로 넘어온 파일들 하나씩 fileNameList에 추가 | ||
multipartFile.forEach(file -> { | ||
String fileName = createFileName(file.getOriginalFilename()); | ||
ObjectMetadata objectMetadata = new ObjectMetadata(); | ||
objectMetadata.setContentLength(file.getSize()); | ||
objectMetadata.setContentType(file.getContentType()); | ||
|
||
try(InputStream inputStream = file.getInputStream()) { | ||
amazonS3.putObject(new PutObjectRequest(bucket, fileName, inputStream, objectMetadata) | ||
.withCannedAcl(CannedAccessControlList.PublicRead)); | ||
} catch(IOException e) { | ||
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "파일 업로드에 실패했습니다."); | ||
} | ||
|
||
fileNameList.add(fileName); | ||
}); | ||
|
||
return fileNameList; | ||
} | ||
|
||
public void deleteFile(String fileName) { | ||
amazonS3.deleteObject(new DeleteObjectRequest(bucket, fileName)); | ||
} | ||
|
||
private String createFileName(String fileName) { // 먼저 파일 업로드 시, 파일명을 난수화하기 위해 random으로 돌립니다. | ||
return UUID.randomUUID().toString().concat(getFileExtension(fileName)); | ||
} | ||
|
||
private String getFileExtension(String fileName) { // file 형식이 잘못된 경우를 확인하기 위해 만들어진 로직이며, 파일 타입과 상관없이 업로드할 수 있게 하기 위해 .의 존재 유무만 판단하였습니다. | ||
try { | ||
return fileName.substring(fileName.lastIndexOf(".")); | ||
} catch (StringIndexOutOfBoundsException e) { | ||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "잘못된 형식의 파일(" + fileName + ") 입니다."); | ||
} | ||
} | ||
} |