Skip to content

Commit

Permalink
Merge pull request #75 from connecSWUn/feature/upload-profile
Browse files Browse the repository at this point in the history
[FIX] 사용자 프로필 사진 업로드 API 구현
  • Loading branch information
ehBeak authored Sep 1, 2023
2 parents d8ee2a1 + 808985d commit a0515ea
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.swulab.eatswunee.domain.user.adapter.in.web.controller;

import com.swulab.eatswunee.domain.user.application.port.in.UpdateUserUseCase;
import com.swulab.eatswunee.global.common.adapter.web.in.dto.SuccessResponse;
import com.swulab.eatswunee.global.common.application.port.in.AddImageUseCase;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequiredArgsConstructor
@Slf4j
public class UploadUserProfileController {

private final AddImageUseCase addImageUseCase;
private final UpdateUserUseCase updateUserUseCase;

@PostMapping(value="/user/profile",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity UploadUserProfile(@AuthenticationPrincipal UserDetails userDetails, @RequestParam(value="image") MultipartFile file) {

long userId = Long.parseLong(userDetails.getUsername());

String fileName = addImageUseCase.uploadImage(file);
log.info("{}", fileName);
updateUserUseCase.updateUserProfileUrl(userId, fileName);

return ResponseEntity.ok(SuccessResponse.create201SuccessResponse());

}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.swulab.eatswunee.domain.user.application.port.in;

public interface UpdateUserUseCase {

void updateUserProfileUrl(Long userId, String fileName);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.swulab.eatswunee.domain.user.application.service;

import com.swulab.eatswunee.domain.user.application.port.in.UpdateUserUseCase;
import com.swulab.eatswunee.domain.user.application.port.out.FindUserPort;
import com.swulab.eatswunee.domain.user.application.port.out.SaveUserPort;
import com.swulab.eatswunee.domain.user.domain.model.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class UpdateUserService implements UpdateUserUseCase {

private final FindUserPort findUserPort;
private final SaveUserPort saveUserPort;

@Override
public void updateUserProfileUrl(Long userId, String fileName) {
User user = findUserPort.findUser(userId);
user.changeProfileImage(fileName);
saveUserPort.saveUser(user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public User(Long userId, String name, String profileUrl, String password, String
}




public void changeProfileImage(String profileUrl) {
this.profileUrl = profileUrl;
}
public void mapImageToUrl(String url) {
this.profileUrl = url;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.swulab.eatswunee.global.common.application.port.in;

import org.springframework.web.multipart.MultipartFile;

public interface AddImageUseCase {

String uploadImage(MultipartFile multipartFile);

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
package com.swulab.eatswunee.global.common.application.service;

import static com.google.common.io.Files.getFileExtension;

import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.swulab.eatswunee.global.common.application.port.in.AddImageUseCase;
import com.swulab.eatswunee.global.common.application.port.in.GetImageUrlUseCase;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;
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;

@Service
@RequiredArgsConstructor
public class S3service implements GetImageUrlUseCase {
public class S3service implements GetImageUrlUseCase, AddImageUseCase {

private final AmazonS3Client amazonS3Client;

Expand All @@ -19,4 +32,36 @@ public String getImageUrl(String resource) {
return amazonS3Client.getUrl(bucketName, resource).toString();
}

public String uploadImage(MultipartFile file) {

String fileName = createFileName(file.getOriginalFilename());
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(file.getSize());
objectMetadata.setContentType(file.getContentType());


try(InputStream inputStream = file.getInputStream()) {
amazonS3Client.putObject(
new PutObjectRequest(bucketName, "user_profile/" + fileName, inputStream, objectMetadata)
.withCannedAcl(CannedAccessControlList.PublicRead));
} catch(IOException e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "이미지 업로드에 실패했습니다.");
}


return fileName;
}

private String createFileName(String fileName) {
return UUID.randomUUID().toString().concat(getFileExtension(fileName));
}

private String getFileExtension(String fileName) {
try {
return fileName.substring(fileName.lastIndexOf("."));
} catch (StringIndexOutOfBoundsException e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "잘못된 형식의 파일(" + fileName + ") 입니다.");
}
}

}

0 comments on commit a0515ea

Please sign in to comment.