Skip to content

Commit

Permalink
Merge pull request #118 from kookmin-sw/backend/feat/#107
Browse files Browse the repository at this point in the history
Backend/feat/#107
  • Loading branch information
J-Yong99 authored Apr 30, 2024
2 parents 25f5ea8 + 32887dc commit 7485a57
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class UserController {
public ResponseEntity<APIResponse> registerUser(
@RequestBody UserRequestDTO.registerUser request
) {
s3Service.createFolder(request.getId().toString());
// s3Service.createFolder(request.getId().toString());
userService.save(request);
return ResponseEntity.ok(APIResponse.of(SuccessCode.INSERT_SUCCESS));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public void deleteRecord(Long cardViewId) {
CardView cardView = cardViewRepository.findById(cardViewId).orElseThrow(() -> new IllegalArgumentException("존재하지 않는 카드뷰입니다."));
// cardView에 엮인 사진들 먼저 삭제
imageFileService.deleteAll(cardView);
s3Service.deleteFile(cardView.getRecordFileName());
boolean isAnalyzed = cardView.getRecordFileStatus().equals("WAIT");
// 만약 tripfile의 Trip이 untitled일 경우
// 만약 tripfile의 크기가 1이라면 tripFile과 cardView 전부 삭제, untitledTrip의 analyzingCount 감소
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

@Service
Expand All @@ -22,6 +23,7 @@ public class ImageFileService {
private final ImageFileRepository imageFileRepository;
private final S3Service s3Service;
private final CardViewRepository cardViewRepository;
private final UserService userService;

@Transactional
public void deleteAll(CardView cardView) {
Expand All @@ -37,7 +39,8 @@ public void uploadAll(List<MultipartFile> imageFiles, Long cardViewId, Long user
for (MultipartFile imageFile : imageFiles) {
CardView cv = cardViewRepository.findById(cardViewId).orElseThrow(() -> new IllegalArgumentException("해당 카드뷰가 없습니다."));
String filename = UUID.randomUUID().toString();
String url = s3Service.uploadFile(imageFile, userId, filename, false);
String extension = Objects.requireNonNull(imageFile.getOriginalFilename()).substring(imageFile.getOriginalFilename().lastIndexOf("."));
String url = s3Service.uploadFile(imageFile, userId, filename + extension, false);
ImageFile image = ImageFile.builder()
.fileUrl(url)
.fileName(filename)
Expand All @@ -48,4 +51,13 @@ public void uploadAll(List<MultipartFile> imageFiles, Long cardViewId, Long user

}

@Transactional
public void deleteImages(List<Long> images, Long userId) {
for (Long imageId : images) {
ImageFile image = imageFileRepository.findById(imageId).orElseThrow(() -> new IllegalArgumentException("해당 이미지가 없습니다."));
userService.validateUserWithCardView(userId, image.getCardView().getId());
s3Service.deleteFile(image.getFileName());
imageFileRepository.delete(image);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ public String uploadFile(MultipartFile image, Long userId, String fileName, bool
}
public String uploadToS3(MultipartFile image, Long userId, String fileName, boolean isRecord) throws IOException {
String s3FileName = s3folderPath + userId.toString() + "/" + fileName; //S3에 저장될 파일 명
String extention = Objects.requireNonNull(image.getOriginalFilename()).substring(image.getOriginalFilename().lastIndexOf("."));
String extension = Objects.requireNonNull(image.getOriginalFilename()).substring(image.getOriginalFilename().lastIndexOf("."));

InputStream is = image.getInputStream();
byte[] bytes = IOUtils.toByteArray(is); //image를 byte[]로 변환

ObjectMetadata metadata = new ObjectMetadata(); //metadata 생성
if (isRecord) {
metadata.setContentType("audio/" + extention);
metadata.setContentType("audio/" + extension);
} else {
metadata.setContentType("image/" + extention);
metadata.setContentType("image/" + extension);
}
metadata.setContentLength(bytes.length);

Expand Down

0 comments on commit 7485a57

Please sign in to comment.