2018-01-17 17:51:53 +08:00
|
|
|
|
package cn.keking.service.impl;
|
|
|
|
|
|
|
2019-04-28 09:07:23 +08:00
|
|
|
|
import cn.keking.config.ConfigConstants;
|
|
|
|
|
|
import cn.keking.model.FileAttribute;
|
|
|
|
|
|
import cn.keking.model.ReturnResponse;
|
2024-03-09 10:35:13 +08:00
|
|
|
|
import cn.keking.service.FileHandlerService;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
import cn.keking.service.FilePreview;
|
2019-04-28 09:07:23 +08:00
|
|
|
|
import cn.keking.utils.DownloadUtils;
|
2024-03-09 10:35:13 +08:00
|
|
|
|
import cn.keking.utils.WebUtils;
|
2023-04-28 09:48:51 +08:00
|
|
|
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
|
|
|
|
|
import org.apache.poi.EncryptedDocumentException;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import org.springframework.ui.Model;
|
|
|
|
|
|
|
2023-04-28 09:48:51 +08:00
|
|
|
|
import java.io.IOException;
|
2019-04-28 09:07:23 +08:00
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
2018-01-17 17:51:53 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* Created by kl on 2018/1/17.
|
|
|
|
|
|
* Content :处理pdf文件
|
|
|
|
|
|
*/
|
|
|
|
|
|
@Service
|
2020-05-18 09:46:52 +08:00
|
|
|
|
public class PdfFilePreviewImpl implements FilePreview {
|
2018-01-17 17:51:53 +08:00
|
|
|
|
|
2020-12-26 19:13:50 +08:00
|
|
|
|
private final FileHandlerService fileHandlerService;
|
2020-12-27 14:47:28 +08:00
|
|
|
|
private final OtherFilePreviewImpl otherFilePreview;
|
2023-04-28 09:48:51 +08:00
|
|
|
|
private static final String PDF_PASSWORD_MSG = "password";
|
2020-12-27 14:47:28 +08:00
|
|
|
|
public PdfFilePreviewImpl(FileHandlerService fileHandlerService, OtherFilePreviewImpl otherFilePreview) {
|
2020-12-26 19:13:50 +08:00
|
|
|
|
this.fileHandlerService = fileHandlerService;
|
2020-12-27 14:47:28 +08:00
|
|
|
|
this.otherFilePreview = otherFilePreview;
|
2020-05-18 09:46:52 +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) {
|
2023-10-23 17:06:05 +08:00
|
|
|
|
String pdfName = fileAttribute.getName(); //获取原始文件名
|
|
|
|
|
|
String officePreviewType = fileAttribute.getOfficePreviewType(); //转换类型
|
|
|
|
|
|
boolean forceUpdatedCache=fileAttribute.forceUpdatedCache(); //是否启用强制更新命令
|
2023-12-19 08:55:51 +08:00
|
|
|
|
String outFilePath = fileAttribute.getOutFilePath(); //生成的文件路径
|
|
|
|
|
|
String originFilePath = fileAttribute.getOriginFilePath(); //原始文件路径
|
2020-05-18 09:46:52 +08:00
|
|
|
|
if (OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_ALL_IMAGES.equals(officePreviewType)) {
|
2019-04-28 09:07:23 +08:00
|
|
|
|
//当文件不存在时,就去下载
|
2023-04-23 11:22:29 +08:00
|
|
|
|
if (forceUpdatedCache || !fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
|
2023-10-23 17:06:05 +08:00
|
|
|
|
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, pdfName);
|
2020-12-27 14:06:06 +08:00
|
|
|
|
if (response.isFailure()) {
|
2020-12-27 14:47:28 +08:00
|
|
|
|
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
2020-05-19 14:21:38 +08:00
|
|
|
|
}
|
2023-12-19 08:55:51 +08:00
|
|
|
|
originFilePath = response.getContent();
|
2020-05-19 14:21:38 +08:00
|
|
|
|
if (ConfigConstants.isCacheEnabled()) {
|
|
|
|
|
|
// 加入缓存
|
2023-12-19 08:55:51 +08:00
|
|
|
|
fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(originFilePath));
|
2020-05-19 14:21:38 +08:00
|
|
|
|
}
|
2019-04-28 09:07:23 +08:00
|
|
|
|
}
|
2023-04-28 09:48:51 +08:00
|
|
|
|
List<String> imageUrls;
|
|
|
|
|
|
try {
|
2023-12-19 08:55:51 +08:00
|
|
|
|
imageUrls = fileHandlerService.pdf2jpg(originFilePath,outFilePath, pdfName, fileAttribute);
|
2023-04-28 09:48:51 +08:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
Throwable[] throwableArray = ExceptionUtils.getThrowables(e);
|
|
|
|
|
|
for (Throwable throwable : throwableArray) {
|
|
|
|
|
|
if (throwable instanceof IOException || throwable instanceof EncryptedDocumentException) {
|
|
|
|
|
|
if (e.getMessage().toLowerCase().contains(PDF_PASSWORD_MSG)) {
|
|
|
|
|
|
model.addAttribute("needFilePassword", true);
|
|
|
|
|
|
return EXEL_FILE_PREVIEW_PAGE;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return otherFilePreview.notSupportedFile(model, fileAttribute, "pdf转图片异常,请联系管理员");
|
|
|
|
|
|
}
|
2019-04-28 09:07:23 +08:00
|
|
|
|
if (imageUrls == null || imageUrls.size() < 1) {
|
2020-12-27 14:47:28 +08:00
|
|
|
|
return otherFilePreview.notSupportedFile(model, fileAttribute, "pdf转图片异常,请联系管理员");
|
2019-04-28 09:07:23 +08:00
|
|
|
|
}
|
2023-06-18 09:58:29 +08:00
|
|
|
|
model.addAttribute("imgUrls", imageUrls);
|
2019-04-28 09:07:23 +08:00
|
|
|
|
model.addAttribute("currentUrl", imageUrls.get(0));
|
|
|
|
|
|
if (OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType)) {
|
2020-12-27 15:14:32 +08:00
|
|
|
|
return OFFICE_PICTURE_FILE_PREVIEW_PAGE;
|
2019-04-28 09:07:23 +08:00
|
|
|
|
} else {
|
2020-12-27 15:14:32 +08:00
|
|
|
|
return PICTURE_FILE_PREVIEW_PAGE;
|
2019-04-28 09:07:23 +08:00
|
|
|
|
}
|
2020-05-14 16:22:48 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
// 不是http开头,浏览器不能直接访问,需下载到本地
|
|
|
|
|
|
if (url != null && !url.toLowerCase().startsWith("http")) {
|
2020-12-26 19:13:50 +08:00
|
|
|
|
if (!fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
|
2020-12-27 01:43:50 +08:00
|
|
|
|
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, pdfName);
|
2020-12-27 14:06:06 +08:00
|
|
|
|
if (response.isFailure()) {
|
2020-12-27 14:47:28 +08:00
|
|
|
|
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
2020-05-19 14:21:38 +08:00
|
|
|
|
}
|
2020-12-26 19:13:50 +08:00
|
|
|
|
model.addAttribute("pdfUrl", fileHandlerService.getRelativePath(response.getContent()));
|
2020-05-19 14:21:38 +08:00
|
|
|
|
if (ConfigConstants.isCacheEnabled()) {
|
|
|
|
|
|
// 加入缓存
|
2020-12-26 19:13:50 +08:00
|
|
|
|
fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(outFilePath));
|
2020-05-19 14:21:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2024-03-09 10:35:13 +08:00
|
|
|
|
model.addAttribute("pdfUrl", WebUtils.encodeFileName(pdfName));
|
2020-05-14 16:22:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
model.addAttribute("pdfUrl", url);
|
|
|
|
|
|
}
|
2019-04-28 09:07:23 +08:00
|
|
|
|
}
|
2020-12-27 15:14:32 +08:00
|
|
|
|
return PDF_FILE_PREVIEW_PAGE;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|