Skip to content

Commit

Permalink
feat : 파일 경로가 중복되는 경우를 처리한다. (#12)
Browse files Browse the repository at this point in the history
* feat : 아이템 도메인 객관식 처리 기능

* feat : 아이템 도메인 주관식과 객관식 타입 분리

* feat : ItemChoicesConverter로 비정규화

* feat : 도메인 예외 처리 정교화, 프론트에 예외 메시지 전달

* feat : 카드 생성 페이지에서 객관식 유형에 맞게 질문 목록을 출력한다.

* refactor : create의 첫번째, 두번째 페이지 파일 분리

* feat : 파일 중복시 경로에 랜덤 문자열 추가
  • Loading branch information
ecsimsw authored Feb 21, 2022
1 parent fa39995 commit 6c68fcf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
28 changes: 21 additions & 7 deletions server/src/main/java/com/giggle/samehere/file/FileService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Objects;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -25,18 +26,31 @@ public class FileService {
private String PHOTO_UPLOAD_FOLDER;

public String saveImageFile(MultipartFile multipartFile) {
if (Objects.isNull(multipartFile) || multipartFile.isEmpty()) {
return ROOT_PATH + DEFAULT_FILE_NAME;
}
try (InputStream inputStream = multipartFile.getInputStream()) {
// TODO :: file duplicated check
final FilePath filePath = FilePath.of(multipartFile);
Files.copy(inputStream, filePath.asPathIn(directoryPath()), StandardCopyOption.REPLACE_EXISTING);
return ROOT_PATH + filePath.asString();
} catch (FileUploadException | IOException | NullPointerException e) {
// TODO :: handle case multipartFile is null
// TODO :: throw new FileUploadException(e.getMessage());
final MultipartFileName fileName = MultipartFileName.of(multipartFile);
saveFile(inputStream, fileName.asPathIn(directoryPath()));
return ROOT_PATH + fileName.asString();
} catch (FileUploadException | IOException e) {
e.printStackTrace();
return ROOT_PATH + DEFAULT_FILE_NAME;
}
}

private void saveFile(InputStream inputStream, Path path) throws IOException {
final Path uniquePath = findUniquePath(path);
Files.copy(inputStream, uniquePath, StandardCopyOption.REPLACE_EXISTING);
}

private Path findUniquePath(Path path) {
if (Files.exists(path)) {
return findUniquePath(Path.of(path + UUID.randomUUID().toString().substring(0, 5)));
}
return path;
}

private Path directoryPath() throws IOException {
final Path directoryPath = Paths.get(PHOTO_UPLOAD_FOLDER);
if (!Files.exists(directoryPath)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

public class FilePath {
public class MultipartFileName {

private final String fileName;

public FilePath(String fileName) {
public MultipartFileName(String fileName) {
this.fileName = fileName;
}

public static FilePath of(MultipartFile multipartFile) {
public static MultipartFileName of(MultipartFile multipartFile) {
if (Objects.isNull(multipartFile) || multipartFile.isEmpty()) {
throw new FileUploadException();
}
final String uniqueFileName = LocalDateTime.now() + StringUtils.cleanPath(multipartFile.getOriginalFilename());
return new FilePath(uniqueFileName);
return new MultipartFileName(uniqueFileName);
}

public Path asPathIn(Path path) {
Expand Down

0 comments on commit 6c68fcf

Please sign in to comment.