Skip to content

Conversation

@linglong67
Copy link
Collaborator

@linglong67 linglong67 commented Mar 21, 2024

💡 Motivation and Context

파일 확장자 검증 로직 추가


🔨 Modified

파일 확장자 검증

  • apache tika 라이브러리 사용
  • FileUtils 파일에 파일 확장자 검증 로직 보완
  • 제품 리뷰 및 세차장 리뷰 등록/수정 로직에 해당 검증 내용 추가

파일 확장자 검증 로직 설명

  • 기본 방식
    • tika 라이브러리를 통해 검증한 실제 파일의 MIME Type (ex> image/png) 기준으로 extention list를 가져와 업로드 파일의 확장자가 포함되는지 체크
 isValidFileExtension(List<MultipartFile> files)
  • 업로드 허용 파일타입에 제한을 두는 경우 (프론트에서도 제한 두겠지만 필요한 경우 사용하면 됩니다)
    • 아래 메서드 호출 시 기본 방식에 대한 검증을 통과한 후 추가적으로 검증합니다
    • 서비스에서 허용한 MIME Type 기준의 extention list를 만들어 해당 파일의 확장자가 업로드 가능한지 체크
 isValidFileExtension(List<MultipartFile> files, List<String> allowedFileType)

🌟 More

  • 리팩토링 작업
  • ...


📋 커밋 전 체크리스트

  • 추가/변경에 대한 단위 테스트를 완료하였습니다.
  • 컨벤션에 맞게 작성하였습니다.

🤟🏻 PR로 완료된 이슈

closes #315

@linglong67 linglong67 added 🖥️ BackEnd 서버 관련 💡 Feature 새로운 기능 추가, 혹은 구현 🔨 Refactor 이 코드는 아주 약간 더 클린코드에 가까워졌습니다... labels Mar 21, 2024
@linglong67 linglong67 self-assigned this Mar 21, 2024
@linglong67 linglong67 closed this Mar 22, 2024
@linglong67 linglong67 reopened this Mar 22, 2024
Copy link
Collaborator

@HyunJunSon HyunJunSon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

티카적용 확인했습니다~

Copy link
Collaborator

@chan99k chan99k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
고생하셨습니다

Comment on lines +91 to +106
public void isValidFileExtension(List<MultipartFile> files) {
boolean isNotValid = files.stream()
.anyMatch(file -> {
try {
List<String> extensions = mimeTypes.forName(tika.detect(file.getInputStream())).getExtensions();
return !extensions.contains("." + FilenameUtils.getExtension(file.getOriginalFilename()));
} catch (MimeTypeException | IOException e) {
log.error("isValidFileExtension(List)", e.getMessage());
throw new BusinessException(CommonErrorCode.FAIL_FILE_EXTENSION_VALIDATE);
}
});

if (isNotValid) {
throw new BusinessException(CommonErrorCode.INVALID_FILE_EXTENSION);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

파일 확장자와 파일 형식이 맞는지 체크하는 부분이 이 부분인가요?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🖥️ BackEnd 서버 관련 💡 Feature 새로운 기능 추가, 혹은 구현 🔨 Refactor 이 코드는 아주 약간 더 클린코드에 가까워졌습니다...

Projects

None yet

Development

Successfully merging this pull request may close these issues.

파일 확장자 검증 로직 추가

3 participants