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;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
import cn.keking.service.FilePreview;
|
|
|
|
|
|
import cn.keking.utils.DownloadUtils;
|
2020-12-26 19:13:50 +08:00
|
|
|
|
import cn.keking.service.FileHandlerService;
|
2020-12-26 02:31:34 +08:00
|
|
|
|
import cn.keking.service.OfficeToPdfService;
|
2020-05-13 19:40:31 +08:00
|
|
|
|
import cn.keking.web.filter.BaseUrlFilter;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import org.springframework.ui.Model;
|
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
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();
|
2018-01-17 17:51:53 +08:00
|
|
|
|
boolean isHtml = suffix.equalsIgnoreCase("xls") || suffix.equalsIgnoreCase("xlsx");
|
|
|
|
|
|
String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + (isHtml ? "html" : "pdf");
|
2020-05-18 09:46:52 +08:00
|
|
|
|
String outFilePath = FILE_DIR + pdfName;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
// 判断之前是否已转换过,如果转换过,直接返回,否则执行转换
|
2020-12-26 19:13:50 +08:00
|
|
|
|
if (!fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
|
2020-05-18 09:46:52 +08:00
|
|
|
|
String filePath;
|
2020-12-27 01:43:50 +08:00
|
|
|
|
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, null);
|
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());
|
2018-01-17 17:51:53 +08:00
|
|
|
|
}
|
2019-09-16 11:17:19 +08:00
|
|
|
|
filePath = response.getContent();
|
2018-01-17 17:51:53 +08:00
|
|
|
|
if (StringUtils.hasText(outFilePath)) {
|
2020-12-26 02:31:34 +08:00
|
|
|
|
officeToPdfService.openOfficeToPDF(filePath, outFilePath);
|
2019-08-24 14:38:42 +08:00
|
|
|
|
if (isHtml) {
|
|
|
|
|
|
// 对转换后的文件进行操作(改变编码方式)
|
2020-12-26 19:13:50 +08:00
|
|
|
|
fileHandlerService.doActionConvertedFile(outFilePath);
|
2019-08-24 14:38:42 +08:00
|
|
|
|
}
|
2019-08-22 15:22:05 +08:00
|
|
|
|
if (ConfigConstants.isCacheEnabled()) {
|
|
|
|
|
|
// 加入缓存
|
2020-12-26 19:13:50 +08:00
|
|
|
|
fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(outFilePath));
|
2018-01-17 17:51:53 +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))) {
|
2020-12-27 14:47:28 +08:00
|
|
|
|
return getPreviewType(model, fileAttribute, officePreviewType, baseUrl, pdfName, outFilePath, fileHandlerService, OFFICE_PREVIEW_TYPE_IMAGE, otherFilePreview);
|
2019-04-25 18:39:58 +08:00
|
|
|
|
}
|
2018-01-17 17:51:53 +08:00
|
|
|
|
model.addAttribute("pdfUrl", pdfName);
|
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");
|
2020-12-27 14:06:06 +08:00
|
|
|
|
List<String> imageUrls = fileHandlerService.pdf2jpg(outFilePath, pdfName, baseUrl);
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-01-17 17:51:53 +08:00
|
|
|
|
}
|