Skip to content

Commit

Permalink
Merge pull request #58 from mju-likelion/feature/file-validation-log-#57
Browse files Browse the repository at this point in the history
Feature/#57 파일 제출 Validation에 로그 추가
  • Loading branch information
Dh3356 authored Feb 18, 2024
2 parents ab44292 + aa1a24d commit 4cc8ca3
Showing 1 changed file with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,46 @@
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
public class IntroduceFileConstraintValidator implements ConstraintValidator<IntroduceFileConstraint, MultipartFile> {
@Override
public void initialize(IntroduceFileConstraint constraintAnnotation) {
}

@Override
public boolean isValid(MultipartFile file, ConstraintValidatorContext context) {
log.info("Start validate file");
return !isEmptyFile(file) && (isValidFileName(file.getOriginalFilename()) && isValidFileExtension(file)
&& isValidFileSize(file));
}

private boolean isEmptyFile(MultipartFile file) {
log.info("Is file empty? {}",
file == null || file.isEmpty() || file.getSize() == 0 || file.getOriginalFilename() == null
|| file.getOriginalFilename().isEmpty());
return file == null || file.isEmpty() || file.getSize() == 0 || file.getOriginalFilename() == null
|| file.getOriginalFilename().isEmpty();
}

private boolean isValidFileName(String fileName) {
String fileNameWithoutExtension = fileName.substring(0, fileName.lastIndexOf("."));
log.info("Is file name valid? {}", fileNameWithoutExtension.matches(APPLICATION_STUDENT_ID_PATTERN));
return fileNameWithoutExtension.matches(APPLICATION_STUDENT_ID_PATTERN);
}

private boolean isValidFileExtension(MultipartFile file) {
log.info("Is file extension valid? {}",
Objects.equals(file.getContentType(), ZIP_CONTENT_TYPE) && Objects.requireNonNull(
file.getOriginalFilename()).toLowerCase().endsWith(ZIP_EXTENSION));
return Objects.equals(file.getContentType(), ZIP_CONTENT_TYPE) && Objects.requireNonNull(
file.getOriginalFilename()).toLowerCase().endsWith(ZIP_EXTENSION);
}

private boolean isValidFileSize(MultipartFile file) {
log.info("Is file size valid? {}", file.getSize() <= MAX_FILE_SIZE);
return file.getSize() <= MAX_FILE_SIZE;
}
}

0 comments on commit 4cc8ca3

Please sign in to comment.