Skip to content

Commit

Permalink
refactor: UpdateUserImageService 메서드 추출을 통한 코드 정리
Browse files Browse the repository at this point in the history
  • Loading branch information
vectorch9 committed Oct 21, 2023
1 parent 8f9d5e2 commit 3214ab0
Showing 1 changed file with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
import daybyquest.user.domain.UserImages;
import daybyquest.user.domain.UserRepository;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@Service
@Slf4j
public class UpdateUserImageService {

private final UserRepository userRepository;
Expand All @@ -28,14 +31,23 @@ public UpdateUserImageService(final UserRepository userRepository,
public void invoke(final Long loginId, final MultipartFile file) {
final User user = userRepository.getById(loginId);
final String oldIdentifier = user.getImageIdentifier();
final String uuid = UUID.randomUUID().toString();
final String identifier = uuid + file.getOriginalFilename();
final String identifier = createIdentifier(file.getOriginalFilename());
userImages.upload(identifier, getInputStream(file));
user.updateImage(new Image(identifier));
userImages.remove(oldIdentifier);
}

private InputStream getInputStream(final MultipartFile file) {
try {
userImages.upload(identifier, file.getInputStream());
user.updateImage(new Image(identifier));
userImages.remove(oldIdentifier);
return file.getInputStream();
} catch (IOException e) {
log.error(e.getMessage());
throw new InvalidFileException();
}
}

private String createIdentifier(final String originalName) {
final String uuid = UUID.randomUUID().toString();
return uuid + originalName;
}
}

0 comments on commit 3214ab0

Please sign in to comment.