Skip to content

Commit 52d4d58

Browse files
committed
refactor: Remove Redundant Validation in File Encryption Process
1 parent 4aeddf3 commit 52d4d58

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

fastexcel-core/src/main/java/cn/idev/excel/support/ExcelTypeEnum.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import cn.idev.excel.exception.ExcelAnalysisException;
99
import cn.idev.excel.exception.ExcelCommonException;
1010
import cn.idev.excel.read.metadata.ReadWorkbook;
11+
import cn.idev.excel.util.StringUtils;
12+
1113
import lombok.Getter;
1214
import org.apache.poi.util.IOUtils;
1315

@@ -57,6 +59,12 @@ public static ExcelTypeEnum valueOf(ReadWorkbook readWorkbook) {
5759
if (!file.exists()) {
5860
throw new ExcelAnalysisException("File " + file.getAbsolutePath() + " not exists.");
5961
}
62+
// If there is a password, use the FileMagic first
63+
if (!StringUtils.isEmpty(readWorkbook.getPassword())) {
64+
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file))) {
65+
return recognitionExcelType(bufferedInputStream);
66+
}
67+
}
6068
// Use the name to determine the type
6169
String fileName = file.getName();
6270
if (fileName.endsWith(XLSX.getValue())) {
@@ -66,8 +74,10 @@ public static ExcelTypeEnum valueOf(ReadWorkbook readWorkbook) {
6674
} else if (fileName.endsWith(CSV.getValue())) {
6775
return CSV;
6876
}
69-
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file))) {
70-
return recognitionExcelType(bufferedInputStream);
77+
if (StringUtils.isEmpty(readWorkbook.getPassword())) {
78+
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file))) {
79+
return recognitionExcelType(bufferedInputStream);
80+
}
7181
}
7282
}
7383
if (!inputStream.markSupported()) {

0 commit comments

Comments
 (0)