Skip to content

Commit

Permalink
[feat] 이미지 업로드 API 및 로그인 early 변수 위치 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryeolee committed Oct 30, 2023
1 parent 32546c0 commit 9066ea3
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 156 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package modernfarmer.server.farmususer.global.config.s3;

import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.PutObjectRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Optional;
import java.util.UUID;

@Slf4j
@Component
public class S3Uploader {
private final AmazonS3Client amazonS3Client;

@Value("${cloud.aws.s3.bucket}")
private String bucket;

public S3Uploader(AmazonS3Client amazonS3Client) {
this.amazonS3Client = amazonS3Client;
}

public String uploadFiles(
MultipartFile multipartFile, String dirName) throws IOException {
File uploadFile = convert(multipartFile).orElseThrow(() ->
new IllegalArgumentException("error: MultipartFile -> File convert fail"));
return upload(uploadFile, dirName);
}

public String upload(File uploadFile, String filePath) {
String fileName = filePath + "/" + UUID.randomUUID() + uploadFile.getName();
String uploadImageUrl = putS3(uploadFile, fileName);
removeNewFile(uploadFile);
return uploadImageUrl;
}

private String putS3(File uploadFile, String fileName) {
amazonS3Client.putObject(
new PutObjectRequest(bucket, fileName, uploadFile)
.withCannedAcl(CannedAccessControlList.PublicRead));
return amazonS3Client.getUrl(bucket, fileName).toString();
}

private void removeNewFile(File targetFile) {
if (targetFile.delete()) {
System.out.println("File delete success");
return;
}
System.out.println("File delete fail");
}

private Optional<File> convert(MultipartFile file) throws IOException {
File convertFile = new File(System.getProperty("user.dir") + "/" + file.getOriginalFilename());
if (convertFile.createNewFile()) {
try (FileOutputStream fileOutputStream = new FileOutputStream(convertFile)) {
fileOutputStream.write(file.getBytes());
}
return Optional.of(convertFile);
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Slf4j
@RestController
Expand All @@ -24,12 +25,12 @@ public class UserController {
private final UserService userService;
private final JwtTokenProvider jwtTokenProvider;

@PostMapping(value = "/profileImage", consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE})
public ResponseDto produceProfileImage(HttpServletRequest request, @RequestPart("file") MultipartFile multipartFile){
@PostMapping(value = "/profileImage", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseDto produceProfileImage(HttpServletRequest request, @RequestPart("file") MultipartFile multipartFile) throws IOException {

String userId = jwtTokenProvider.getUserId(request);

ResponseDto responseDto = userService.produceProfileImage(Long.valueOf(userId), multipartFile);
log.info(userId);
ResponseDto responseDto = userService.emitProfileImage(Long.valueOf(userId), multipartFile);

return responseDto;

Expand All @@ -40,6 +41,7 @@ public ProfileImageResponseDto selectProfileImage(HttpServletRequest request){

String userId = jwtTokenProvider.getUserId(request);


ProfileImageResponseDto profileImageResponseDto = userService.selectProfileImage(Long.valueOf(userId));

return profileImageResponseDto;
Expand All @@ -59,21 +61,11 @@ public ResponseDto deleteUser(HttpServletRequest request){


@PostMapping(value = "/nickname")
public ResponseDto produceNickname(HttpServletRequest request, @RequestBody ProduceNicknameRequest produceNicknameRequest){

String userId = jwtTokenProvider.getUserId(request);

ResponseDto responseDto = userService.produceNickname(Long.valueOf(userId), produceNicknameRequest.getNickName());

return responseDto;
}

@PatchMapping(value = "/nickname")
public ResponseDto updateNickname(HttpServletRequest request, @RequestBody ProduceNicknameRequest produceNicknameRequest){
public ResponseDto emitNickname(HttpServletRequest request, @RequestBody ProduceNicknameRequest produceNicknameRequest){

String userId = jwtTokenProvider.getUserId(request);

ResponseDto responseDto = userService.produceNickname(Long.valueOf(userId), produceNicknameRequest.getNickName());
ResponseDto responseDto = userService.emitNickname(Long.valueOf(userId), produceNicknameRequest.getNickName());

return responseDto;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@Getter
public class ResponseDto {
private int code;
private String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.lettuce.core.dynamic.annotation.Param;
import modernfarmer.server.farmususer.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

Expand All @@ -11,20 +12,22 @@
@Repository
public interface UserRepository extends JpaRepository<User, Long> {



Optional<User> findByUsernumber(String usernumber);
@Query("SELECT a.role FROM User AS a WHERE a.id = :userId")
String findUserRole(@Param("userId") Long userId);

@Query("update User as u set u = :nickName where u.id = :userId")
@Modifying
@Query("update User as u set u.nickname = :nickName where u.id = :userId")
void updateUserNickname(@Param("nickName") String nickName, @Param("userId") Long userId);

@Query("delete from User as u where u.id = : userId")
@Modifying
@Query("delete from User as u where u.id = :userId")
void deleteUser(@Param("userId") Long userId);

@Query("select u.profileImage from User as u where u.id = :userId")
String selectUserProfileImage(@Param("userId") Long userId);
@Modifying
@Query("update User as u set u.profileImage = :profileImage where u.id = :userId")
void emitUserProfileImage(@Param("userId") Long userId, @Param("profileImage") String profileImage);



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class AuthService{
public UserRepository userRepository;

private final WebClient webClient;
private boolean early = false;


@Autowired
public AuthService(WebClient webClient, UserRepository userRepository, JwtTokenProvider jwtTokenProvider, RedisTemplate<String, String> redisTemplate) {
Expand All @@ -41,6 +41,7 @@ public AuthService(WebClient webClient, UserRepository userRepository, JwtTokenP
public TokenResponseDto googleLogin(String accessToken) {

User user;
boolean early = false;

Mono<GoogleUserResponseDto> userInfoMono = getUserGoogleInfo(accessToken);
GoogleUserResponseDto userInfo = userInfoMono.block();
Expand Down Expand Up @@ -88,6 +89,7 @@ public TokenResponseDto googleLogin(String accessToken) {
public TokenResponseDto kakaoLogin(String accessToken) {

User user;
boolean early = false;
Mono<KakaoUserResponseDto> userInfoMono = getUserKakaoInfo(accessToken);
KakaoUserResponseDto userInfo = userInfoMono.block();

Expand Down
Loading

0 comments on commit 9066ea3

Please sign in to comment.