Skip to content

Commit

Permalink
refactor: Remove Redundant Validation in File Encryption Process
Browse files Browse the repository at this point in the history
  • Loading branch information
psxjoy committed Dec 20, 2024
1 parent 4aeddf3 commit 52d4d58
Showing 1 changed file with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import cn.idev.excel.exception.ExcelAnalysisException;
import cn.idev.excel.exception.ExcelCommonException;
import cn.idev.excel.read.metadata.ReadWorkbook;
import cn.idev.excel.util.StringUtils;

import lombok.Getter;
import org.apache.poi.util.IOUtils;

Expand Down Expand Up @@ -57,6 +59,12 @@ public static ExcelTypeEnum valueOf(ReadWorkbook readWorkbook) {
if (!file.exists()) {
throw new ExcelAnalysisException("File " + file.getAbsolutePath() + " not exists.");
}
// If there is a password, use the FileMagic first
if (!StringUtils.isEmpty(readWorkbook.getPassword())) {
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file))) {
return recognitionExcelType(bufferedInputStream);
}
}
// Use the name to determine the type
String fileName = file.getName();
if (fileName.endsWith(XLSX.getValue())) {
Expand All @@ -66,8 +74,10 @@ public static ExcelTypeEnum valueOf(ReadWorkbook readWorkbook) {
} else if (fileName.endsWith(CSV.getValue())) {
return CSV;
}
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file))) {
return recognitionExcelType(bufferedInputStream);
if (StringUtils.isEmpty(readWorkbook.getPassword())) {
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file))) {
return recognitionExcelType(bufferedInputStream);
}
}
}
if (!inputStream.markSupported()) {
Expand Down

0 comments on commit 52d4d58

Please sign in to comment.