diff --git a/server/src/main/java/cn/keking/service/OfficeToPdfService.java b/server/src/main/java/cn/keking/service/OfficeToPdfService.java index 6217cc20..aef72983 100644 --- a/server/src/main/java/cn/keking/service/OfficeToPdfService.java +++ b/server/src/main/java/cn/keking/service/OfficeToPdfService.java @@ -1,12 +1,17 @@ package cn.keking.service; +import cn.keking.model.FileAttribute; +import com.sun.star.document.UpdateDocMode; +import org.apache.commons.lang3.StringUtils; import org.jodconverter.core.office.OfficeException; -import org.jodconverter.local.JodConverter; +import org.jodconverter.local.LocalConverter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import java.io.File; +import java.util.HashMap; +import java.util.Map; /** * @author yudian-it @@ -16,22 +21,33 @@ public class OfficeToPdfService { private final static Logger logger = LoggerFactory.getLogger(OfficeToPdfService.class); - public void openOfficeToPDF(String inputFilePath, String outputFilePath) throws OfficeException { - office2pdf(inputFilePath, outputFilePath); + public void openOfficeToPDF(String inputFilePath, String outputFilePath, FileAttribute fileAttribute) throws OfficeException { + office2pdf(inputFilePath, outputFilePath, fileAttribute); } - public static void converterFile(File inputFile, String outputFilePath_end) throws OfficeException { + public static void converterFile(File inputFile, String outputFilePath_end, FileAttribute fileAttribute) throws OfficeException { File outputFile = new File(outputFilePath_end); // 假如目标路径不存在,则新建该路径 if (!outputFile.getParentFile().exists() && !outputFile.getParentFile().mkdirs()) { logger.error("创建目录【{}】失败,请检查目录权限!",outputFilePath_end); } - JodConverter.convert(inputFile).to(outputFile).execute(); + LocalConverter.Builder builder; + if (StringUtils.isNotBlank(fileAttribute.getFilePassword())) { + Map loadProperties = new HashMap<>(); + loadProperties.put("Hidden", true); + loadProperties.put("ReadOnly", true); + loadProperties.put("UpdateDocMode", UpdateDocMode.NO_UPDATE); + loadProperties.put("Password", fileAttribute.getFilePassword()); + builder = LocalConverter.builder().loadProperties(loadProperties); + } else { + builder = LocalConverter.builder(); + } + builder.build().convert(inputFile).to(outputFile).execute(); } - public void office2pdf(String inputFilePath, String outputFilePath) throws OfficeException { + public void office2pdf(String inputFilePath, String outputFilePath, FileAttribute fileAttribute) throws OfficeException { if (null != inputFilePath) { File inputFile = new File(inputFilePath); // 判断目标文件路径是否为空 @@ -40,12 +56,12 @@ public class OfficeToPdfService { String outputFilePath_end = getOutputFilePath(inputFilePath); if (inputFile.exists()) { // 找不到源文件, 则返回 - converterFile(inputFile, outputFilePath_end); + converterFile(inputFile, outputFilePath_end, fileAttribute); } } else { if (inputFile.exists()) { // 找不到源文件, 则返回 - converterFile(inputFile, outputFilePath); + converterFile(inputFile, outputFilePath, fileAttribute); } } } diff --git a/server/src/main/java/cn/keking/service/impl/OfficeFilePreviewImpl.java b/server/src/main/java/cn/keking/service/impl/OfficeFilePreviewImpl.java index 98c9b0a7..158206a1 100644 --- a/server/src/main/java/cn/keking/service/impl/OfficeFilePreviewImpl.java +++ b/server/src/main/java/cn/keking/service/impl/OfficeFilePreviewImpl.java @@ -92,7 +92,7 @@ public class OfficeFilePreviewImpl implements FilePreview { } else { if (StringUtils.hasText(outFilePath)) { try { - officeToPdfService.openOfficeToPDF(filePath, outFilePath); + officeToPdfService.openOfficeToPDF(filePath, outFilePath, fileAttribute); } catch (OfficeException e) { if (isPwdProtectedOffice && !OfficeUtils.isCompatible(filePath, filePassword)) { // 加密文件密码错误,提示重新输入 diff --git a/server/src/main/java/cn/keking/utils/OfficeUtils.java b/server/src/main/java/cn/keking/utils/OfficeUtils.java index c782b6cf..c638c34d 100644 --- a/server/src/main/java/cn/keking/utils/OfficeUtils.java +++ b/server/src/main/java/cn/keking/utils/OfficeUtils.java @@ -1,12 +1,12 @@ package cn.keking.utils; import org.apache.commons.lang3.exception.ExceptionUtils; +import org.apache.poi.EncryptedDocumentException; import org.apache.poi.extractor.ExtractorFactory; import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey; import org.springframework.lang.Nullable; import java.io.FileInputStream; -import java.io.IOException; /** * Office工具类 @@ -16,8 +16,6 @@ import java.io.IOException; */ public class OfficeUtils { - private static final String POI_INVALID_PASSWORD_MSG = "Invalid password specified"; - /** * 判断office(word,excel,ppt)文件是否受密码保护 * @@ -27,14 +25,12 @@ public class OfficeUtils { public static boolean isPwdProtected(String path) { try { ExtractorFactory.createExtractor(new FileInputStream(path)); - } catch (IOException e) { - if (POI_INVALID_PASSWORD_MSG.equals(e.getMessage())) { - return true; - } + } catch (EncryptedDocumentException e) { + return true; } catch (Exception e) { Throwable[] throwableArray = ExceptionUtils.getThrowables(e); for (Throwable throwable : throwableArray) { - if (throwable instanceof IOException && POI_INVALID_PASSWORD_MSG.equals(throwable.getMessage())) { + if (throwable instanceof EncryptedDocumentException) { return true; } } diff --git a/server/src/main/resources/web/html.ftl b/server/src/main/resources/web/html.ftl index 4ee3e627..f4da55b7 100644 --- a/server/src/main/resources/web/html.ftl +++ b/server/src/main/resources/web/html.ftl @@ -6,6 +6,7 @@ 文件预览 <#include "*/commonHeader.ftl"> + <#include "*/needFilePasswordHeader.ftl"> diff --git a/server/src/main/resources/web/needFilePasswordHeader.ftl b/server/src/main/resources/web/needFilePasswordHeader.ftl new file mode 100644 index 00000000..566044ce --- /dev/null +++ b/server/src/main/resources/web/needFilePasswordHeader.ftl @@ -0,0 +1,63 @@ +<#setting classic_compatible=true> + + + + + + + + +