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;
|
|
|
|
|
|
import cn.keking.utils.FileUtils;
|
|
|
|
|
|
import cn.keking.utils.OfficeToPdf;
|
2019-04-25 18:39:58 +08:00
|
|
|
|
import cn.keking.utils.PdfUtils;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import org.springframework.ui.Model;
|
|
|
|
|
|
import org.springframework.util.StringUtils;
|
2019-10-31 16:52:58 +08:00
|
|
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
|
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 {
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
FileUtils fileUtils;
|
|
|
|
|
|
|
2019-04-25 18:39:58 +08:00
|
|
|
|
@Autowired
|
|
|
|
|
|
PdfUtils pdfUtils;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
DownloadUtils downloadUtils;
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
private OfficeToPdf officeToPdf;
|
|
|
|
|
|
|
2019-04-25 18:39:58 +08:00
|
|
|
|
String fileDir = ConfigConstants.getFileDir();
|
|
|
|
|
|
|
|
|
|
|
|
public static final String OFFICE_PREVIEW_TYPE_PDF = "pdf";
|
|
|
|
|
|
public static final String OFFICE_PREVIEW_TYPE_IMAGE = "image";
|
2019-04-26 14:10:37 +08:00
|
|
|
|
public static final String OFFICE_PREVIEW_TYPE_ALLIMAGES = "allImages";
|
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,参数传了就取参数的,没传取系统默认
|
|
|
|
|
|
String officePreviewType = model.asMap().get("officePreviewType") == null ? ConfigConstants.getOfficePreviewType() : model.asMap().get("officePreviewType").toString();
|
2019-10-31 16:52:58 +08:00
|
|
|
|
String baseUrl = (String) RequestContextHolder.currentRequestAttributes().getAttribute("baseUrl",0);
|
2018-01-17 17:51:53 +08:00
|
|
|
|
String suffix=fileAttribute.getSuffix();
|
|
|
|
|
|
String fileName=fileAttribute.getName();
|
|
|
|
|
|
boolean isHtml = suffix.equalsIgnoreCase("xls") || suffix.equalsIgnoreCase("xlsx");
|
|
|
|
|
|
String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + (isHtml ? "html" : "pdf");
|
2019-04-25 18:39:58 +08:00
|
|
|
|
String outFilePath = fileDir + pdfName;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
// 判断之前是否已转换过,如果转换过,直接返回,否则执行转换
|
2019-08-22 15:22:05 +08:00
|
|
|
|
if (!fileUtils.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
|
2018-01-17 17:51:53 +08:00
|
|
|
|
String filePath = fileDir + fileName;
|
2019-09-16 11:17:19 +08:00
|
|
|
|
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, null);
|
|
|
|
|
|
if (0 != response.getCode()) {
|
|
|
|
|
|
model.addAttribute("fileType", suffix);
|
|
|
|
|
|
model.addAttribute("msg", response.getMsg());
|
|
|
|
|
|
return "fileNotSupported";
|
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)) {
|
|
|
|
|
|
officeToPdf.openOfficeToPDF(filePath, outFilePath);
|
2019-08-24 14:38:42 +08:00
|
|
|
|
if (isHtml) {
|
|
|
|
|
|
// 对转换后的文件进行操作(改变编码方式)
|
|
|
|
|
|
fileUtils.doActionConvertedFile(outFilePath);
|
|
|
|
|
|
}
|
2019-08-22 15:22:05 +08:00
|
|
|
|
if (ConfigConstants.isCacheEnabled()) {
|
|
|
|
|
|
// 加入缓存
|
|
|
|
|
|
fileUtils.addConvertedFile(pdfName, fileUtils.getRelativePath(outFilePath));
|
2018-01-17 17:51:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-10-31 16:52:58 +08:00
|
|
|
|
if (!isHtml && baseUrl != null && (OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OFFICE_PREVIEW_TYPE_ALLIMAGES.equals(officePreviewType))) {
|
|
|
|
|
|
List<String> imageUrls = pdfUtils.pdf2jpg(outFilePath, pdfName, baseUrl);
|
2019-04-25 18:39:58 +08:00
|
|
|
|
if (imageUrls == null || imageUrls.size() < 1) {
|
|
|
|
|
|
model.addAttribute("msg", "office转图片异常,请联系管理员");
|
|
|
|
|
|
model.addAttribute("fileType",fileAttribute.getSuffix());
|
|
|
|
|
|
return "fileNotSupported";
|
|
|
|
|
|
}
|
|
|
|
|
|
model.addAttribute("imgurls", imageUrls);
|
|
|
|
|
|
model.addAttribute("currentUrl", imageUrls.get(0));
|
2019-04-26 14:10:37 +08:00
|
|
|
|
if (OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType)) {
|
|
|
|
|
|
return "officePicture";
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return "picture";
|
|
|
|
|
|
}
|
2019-04-25 18:39:58 +08:00
|
|
|
|
}
|
2018-01-17 17:51:53 +08:00
|
|
|
|
model.addAttribute("pdfUrl", pdfName);
|
|
|
|
|
|
return isHtml ? "html" : "pdf";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|