From 6c68fcf4f404a48c7cf9374282746708a1b56ab7 Mon Sep 17 00:00:00 2001 From: JinHwanKim Date: Tue, 22 Feb 2022 08:21:37 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20=ED=8C=8C=EC=9D=BC=20=EA=B2=BD?= =?UTF-8?q?=EB=A1=9C=EA=B0=80=20=EC=A4=91=EB=B3=B5=EB=90=98=EB=8A=94=20?= =?UTF-8?q?=EA=B2=BD=EC=9A=B0=EB=A5=BC=20=EC=B2=98=EB=A6=AC=ED=95=9C?= =?UTF-8?q?=EB=8B=A4.=20(#12)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat : 아이템 도메인 객관식 처리 기능 * feat : 아이템 도메인 주관식과 객관식 타입 분리 * feat : ItemChoicesConverter로 비정규화 * feat : 도메인 예외 처리 정교화, 프론트에 예외 메시지 전달 * feat : 카드 생성 페이지에서 객관식 유형에 맞게 질문 목록을 출력한다. * refactor : create의 첫번째, 두번째 페이지 파일 분리 * feat : 파일 중복시 경로에 랜덤 문자열 추가 --- .../com/giggle/samehere/file/FileService.java | 28 ++++++++++++++----- .../{FilePath.java => MultipartFileName.java} | 8 +++--- 2 files changed, 25 insertions(+), 11 deletions(-) rename server/src/main/java/com/giggle/samehere/file/{FilePath.java => MultipartFileName.java} (78%) diff --git a/server/src/main/java/com/giggle/samehere/file/FileService.java b/server/src/main/java/com/giggle/samehere/file/FileService.java index b6ad825..424b4b0 100644 --- a/server/src/main/java/com/giggle/samehere/file/FileService.java +++ b/server/src/main/java/com/giggle/samehere/file/FileService.java @@ -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; @@ -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)) { diff --git a/server/src/main/java/com/giggle/samehere/file/FilePath.java b/server/src/main/java/com/giggle/samehere/file/MultipartFileName.java similarity index 78% rename from server/src/main/java/com/giggle/samehere/file/FilePath.java rename to server/src/main/java/com/giggle/samehere/file/MultipartFileName.java index 7bad6af..5cb16d7 100644 --- a/server/src/main/java/com/giggle/samehere/file/FilePath.java +++ b/server/src/main/java/com/giggle/samehere/file/MultipartFileName.java @@ -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) {