2018-01-17 17:51:53 +08:00
|
|
|
|
package cn.keking.service.impl;
|
|
|
|
|
|
|
2019-04-16 21:09:32 +08:00
|
|
|
|
import cn.keking.config.ConfigConstants;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
import cn.keking.model.FileAttribute;
|
2018-01-19 14:51:18 +08:00
|
|
|
|
import cn.keking.model.ReturnResponse;
|
2020-12-26 19:13:50 +08:00
|
|
|
|
import cn.keking.service.FileHandlerService;
|
2022-07-21 03:19:46 +00:00
|
|
|
|
import cn.keking.service.FilePreview;
|
2020-12-26 02:31:34 +08:00
|
|
|
|
import cn.keking.service.OfficeToPdfService;
|
2022-07-21 03:19:46 +00:00
|
|
|
|
import cn.keking.utils.DownloadUtils;
|
2023-04-21 09:56:46 +08:00
|
|
|
|
import cn.keking.utils.KkFileUtils;
|
2022-07-21 03:19:46 +00:00
|
|
|
|
import cn.keking.utils.OfficeUtils;
|
2020-05-13 19:40:31 +08:00
|
|
|
|
import cn.keking.web.filter.BaseUrlFilter;
|
2023-04-28 09:48:51 +08:00
|
|
|
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
|
|
|
|
|
import org.apache.poi.EncryptedDocumentException;
|
2022-12-15 18:19:30 +08:00
|
|
|
|
import org.jodconverter.core.office.OfficeException;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import org.springframework.ui.Model;
|
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
2023-04-28 09:48:51 +08:00
|
|
|
|
import java.io.IOException;
|
2023-01-11 13:26:18 +08:00
|
|
|
|
import java.net.URLEncoder;
|
2019-04-25 18:39:58 +08:00
|
|
|
|
import java.util.List;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Created by kl on 2018/1/17.
|
|
|
|
|
|
* Content :处理office文件
|
|
|
|
|
|
*/
|
|
|
|
|
|
@Service
|
|
|
|
|
|
public class OfficeFilePreviewImpl implements FilePreview {
|
|
|
|
|
|
|
2020-12-27 14:06:06 +08:00
|
|
|
|
public static final String OFFICE_PREVIEW_TYPE_IMAGE = "image";
|
|
|
|
|
|
public static final String OFFICE_PREVIEW_TYPE_ALL_IMAGES = "allImages";
|
|
|
|
|
|
private static final String FILE_DIR = ConfigConstants.getFileDir();
|
2023-04-28 09:48:51 +08:00
|
|
|
|
private static final String OFFICE_PASSWORD_MSG = "password";
|
2020-12-27 14:06:06 +08:00
|
|
|
|
|
2020-12-26 19:13:50 +08:00
|
|
|
|
private final FileHandlerService fileHandlerService;
|
2020-12-26 02:31:34 +08:00
|
|
|
|
private final OfficeToPdfService officeToPdfService;
|
2020-12-27 14:47:28 +08:00
|
|
|
|
private final OtherFilePreviewImpl otherFilePreview;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
|
2020-12-27 14:47:28 +08:00
|
|
|
|
public OfficeFilePreviewImpl(FileHandlerService fileHandlerService, OfficeToPdfService officeToPdfService, OtherFilePreviewImpl otherFilePreview) {
|
2020-12-26 19:13:50 +08:00
|
|
|
|
this.fileHandlerService = fileHandlerService;
|
2020-12-26 02:31:34 +08:00
|
|
|
|
this.officeToPdfService = officeToPdfService;
|
2020-12-27 14:47:28 +08:00
|
|
|
|
this.otherFilePreview = otherFilePreview;
|
2020-05-18 09:46:52 +08:00
|
|
|
|
}
|
2019-04-25 18:39:58 +08:00
|
|
|
|
|
2018-01-17 17:51:53 +08:00
|
|
|
|
@Override
|
2019-06-17 14:21:16 +08:00
|
|
|
|
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
2019-04-25 18:39:58 +08:00
|
|
|
|
// 预览Type,参数传了就取参数的,没传取系统默认
|
2020-12-26 00:56:48 +08:00
|
|
|
|
String officePreviewType = fileAttribute.getOfficePreviewType();
|
2020-05-13 19:40:31 +08:00
|
|
|
|
String baseUrl = BaseUrlFilter.getBaseUrl();
|
2020-12-27 14:47:28 +08:00
|
|
|
|
String suffix = fileAttribute.getSuffix();
|
|
|
|
|
|
String fileName = fileAttribute.getName();
|
2022-07-21 03:19:46 +00:00
|
|
|
|
String filePassword = fileAttribute.getFilePassword();
|
2023-04-23 11:22:29 +08:00
|
|
|
|
boolean forceUpdatedCache=fileAttribute.forceUpdatedCache();
|
2022-07-21 03:19:46 +00:00
|
|
|
|
String userToken = fileAttribute.getUserToken();
|
2023-04-23 11:25:39 +08:00
|
|
|
|
boolean isHtml = suffix.equalsIgnoreCase("xls") || suffix.equalsIgnoreCase("xlsx") || suffix.equalsIgnoreCase("csv") || suffix.equalsIgnoreCase("xlsm") || suffix.equalsIgnoreCase("xlt") || suffix.equalsIgnoreCase("xltm") || suffix.equalsIgnoreCase("et") || suffix.equalsIgnoreCase("ett") || suffix.equalsIgnoreCase("xlam");
|
|
|
|
|
|
String pdfName = fileName.substring(0, fileName.lastIndexOf(".") ) + suffix +"." +(isHtml ? "html" : "pdf"); //生成文件添加类型后缀 防止同名文件
|
2022-07-21 03:19:46 +00:00
|
|
|
|
String cacheFileName = userToken == null ? pdfName : userToken + "_" + pdfName;
|
|
|
|
|
|
String outFilePath = FILE_DIR + cacheFileName;
|
2023-04-23 11:22:29 +08:00
|
|
|
|
if (forceUpdatedCache|| !fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
|
2022-07-21 03:19:46 +00:00
|
|
|
|
// 下载远程文件到本地,如果文件在本地已存在不会重复下载
|
|
|
|
|
|
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
|
|
|
|
|
|
if (response.isFailure()) {
|
|
|
|
|
|
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
|
|
|
|
|
}
|
|
|
|
|
|
String filePath = response.getContent();
|
|
|
|
|
|
/*
|
|
|
|
|
|
* 1. 缓存判断-如果文件已经进行转换过,就直接返回,否则执行转换
|
|
|
|
|
|
* 2. 缓存判断-加密文件基于userToken进行缓存,如果没有就不缓存
|
|
|
|
|
|
*/
|
|
|
|
|
|
boolean isCached = false;
|
|
|
|
|
|
boolean isUseCached = false;
|
|
|
|
|
|
boolean isPwdProtectedOffice = false;
|
|
|
|
|
|
if (ConfigConstants.isCacheEnabled()) {
|
|
|
|
|
|
// 全局开启缓存
|
|
|
|
|
|
isUseCached = true;
|
2023-04-23 11:22:29 +08:00
|
|
|
|
if (!forceUpdatedCache && fileHandlerService.listConvertedFiles().containsKey(cacheFileName)) {
|
2022-07-21 03:19:46 +00:00
|
|
|
|
// 存在缓存
|
|
|
|
|
|
isCached = true;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
}
|
2022-07-21 03:19:46 +00:00
|
|
|
|
if (OfficeUtils.isPwdProtected(filePath)) {
|
|
|
|
|
|
isPwdProtectedOffice = true;
|
|
|
|
|
|
if (!StringUtils.hasLength(userToken)) {
|
|
|
|
|
|
// 不缓存没有userToken的加密文件
|
|
|
|
|
|
isUseCached = false;
|
2019-08-24 14:38:42 +08:00
|
|
|
|
}
|
2022-07-21 03:19:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
isPwdProtectedOffice = OfficeUtils.isPwdProtected(filePath);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-15 18:19:30 +08:00
|
|
|
|
if (!isCached) {
|
2022-07-21 03:19:46 +00:00
|
|
|
|
// 没有缓存执行转换逻辑
|
|
|
|
|
|
if (isPwdProtectedOffice && !StringUtils.hasLength(filePassword)) {
|
|
|
|
|
|
// 加密文件需要密码
|
|
|
|
|
|
model.addAttribute("needFilePassword", true);
|
|
|
|
|
|
return EXEL_FILE_PREVIEW_PAGE;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if (StringUtils.hasText(outFilePath)) {
|
|
|
|
|
|
try {
|
2022-12-19 16:38:50 +08:00
|
|
|
|
officeToPdfService.openOfficeToPDF(filePath, outFilePath, fileAttribute);
|
2022-07-21 03:19:46 +00:00
|
|
|
|
} catch (OfficeException e) {
|
2022-12-15 18:19:30 +08:00
|
|
|
|
if (isPwdProtectedOffice && !OfficeUtils.isCompatible(filePath, filePassword)) {
|
2022-07-21 03:19:46 +00:00
|
|
|
|
// 加密文件密码错误,提示重新输入
|
|
|
|
|
|
model.addAttribute("needFilePassword", true);
|
|
|
|
|
|
model.addAttribute("filePasswordError", true);
|
|
|
|
|
|
return EXEL_FILE_PREVIEW_PAGE;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return otherFilePreview.notSupportedFile(model, fileAttribute, "抱歉,该文件版本不兼容,文件版本错误。");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (isHtml) {
|
|
|
|
|
|
// 对转换后的文件进行操作(改变编码方式)
|
|
|
|
|
|
fileHandlerService.doActionConvertedFile(outFilePath);
|
|
|
|
|
|
}
|
2023-04-21 09:56:46 +08:00
|
|
|
|
if(ConfigConstants.getdeletesourcefile()){ //是否保留OFFICE源文件
|
|
|
|
|
|
KkFileUtils.deleteFileByPath(filePath);
|
|
|
|
|
|
}
|
2022-07-21 03:19:46 +00:00
|
|
|
|
if (isUseCached) {
|
|
|
|
|
|
// 加入缓存
|
|
|
|
|
|
fileHandlerService.addConvertedFile(cacheFileName, fileHandlerService.getRelativePath(outFilePath));
|
|
|
|
|
|
}
|
2018-01-17 17:51:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-01-11 13:26:18 +08:00
|
|
|
|
}
|
2020-05-18 09:46:52 +08:00
|
|
|
|
if (!isHtml && baseUrl != null && (OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OFFICE_PREVIEW_TYPE_ALL_IMAGES.equals(officePreviewType))) {
|
2022-07-21 03:19:46 +00:00
|
|
|
|
return getPreviewType(model, fileAttribute, officePreviewType, baseUrl, cacheFileName, outFilePath, fileHandlerService, OFFICE_PREVIEW_TYPE_IMAGE, otherFilePreview);
|
2019-04-25 18:39:58 +08:00
|
|
|
|
}
|
2023-01-11 13:26:18 +08:00
|
|
|
|
cacheFileName = URLEncoder.encode(cacheFileName).replaceAll("\\+", "%20");
|
2022-07-21 03:19:46 +00:00
|
|
|
|
model.addAttribute("pdfUrl", cacheFileName);
|
2020-12-27 15:14:32 +08:00
|
|
|
|
return isHtml ? EXEL_FILE_PREVIEW_PAGE : PDF_FILE_PREVIEW_PAGE;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
}
|
2020-05-15 18:09:19 +08:00
|
|
|
|
|
2020-12-27 14:47:28 +08:00
|
|
|
|
static String getPreviewType(Model model, FileAttribute fileAttribute, String officePreviewType, String baseUrl, String pdfName, String outFilePath, FileHandlerService fileHandlerService, String officePreviewTypeImage, OtherFilePreviewImpl otherFilePreview) {
|
2021-07-05 11:09:53 +08:00
|
|
|
|
String suffix = fileAttribute.getSuffix();
|
|
|
|
|
|
boolean isPPT = suffix.equalsIgnoreCase("ppt") || suffix.equalsIgnoreCase("pptx");
|
2023-04-28 09:48:51 +08:00
|
|
|
|
List<String> imageUrls = null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
imageUrls = fileHandlerService.pdf2jpg(outFilePath, pdfName, fileAttribute);
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
Throwable[] throwableArray = ExceptionUtils.getThrowables(e);
|
|
|
|
|
|
for (Throwable throwable : throwableArray) {
|
|
|
|
|
|
if (throwable instanceof IOException || throwable instanceof EncryptedDocumentException) {
|
|
|
|
|
|
if (e.getMessage().toLowerCase().contains(OFFICE_PASSWORD_MSG)) {
|
|
|
|
|
|
model.addAttribute("needFilePassword", true);
|
|
|
|
|
|
return EXEL_FILE_PREVIEW_PAGE;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-05-15 18:09:19 +08:00
|
|
|
|
if (imageUrls == null || imageUrls.size() < 1) {
|
2020-12-27 14:47:28 +08:00
|
|
|
|
return otherFilePreview.notSupportedFile(model, fileAttribute, "office转图片异常,请联系管理员");
|
2020-05-15 18:09:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
model.addAttribute("imgurls", imageUrls);
|
|
|
|
|
|
model.addAttribute("currentUrl", imageUrls.get(0));
|
|
|
|
|
|
if (officePreviewTypeImage.equals(officePreviewType)) {
|
2021-07-05 11:09:53 +08:00
|
|
|
|
// PPT 图片模式使用专用预览页面
|
|
|
|
|
|
return (isPPT ? PPT_FILE_PREVIEW_PAGE : OFFICE_PICTURE_FILE_PREVIEW_PAGE);
|
2020-05-15 18:09:19 +08:00
|
|
|
|
} else {
|
2020-12-27 15:14:32 +08:00
|
|
|
|
return PICTURE_FILE_PREVIEW_PAGE;
|
2020-05-15 18:09:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-07-21 03:19:46 +00:00
|
|
|
|
|
2018-01-17 17:51:53 +08:00
|
|
|
|
}
|