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;
|
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;
|
|
|
|
|
import cn.keking.utils.FileUtils;
|
|
|
|
|
import cn.keking.utils.PdfUtils;
|
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;
|
|
|
|
|
|
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-05-18 09:46:52 +08:00
|
|
|
|
private final FileUtils fileUtils;
|
2019-04-28 09:07:23 +08:00
|
|
|
|
|
2020-05-18 09:46:52 +08:00
|
|
|
|
private final PdfUtils pdfUtils;
|
2019-04-28 09:07:23 +08:00
|
|
|
|
|
2020-05-18 09:46:52 +08:00
|
|
|
|
private final DownloadUtils downloadUtils;
|
2019-04-28 09:07:23 +08:00
|
|
|
|
|
2020-05-19 14:21:38 +08:00
|
|
|
|
private static final String FILE_DIR = ConfigConstants.getFileDir();
|
|
|
|
|
|
2020-05-18 09:46:52 +08:00
|
|
|
|
public PdfFilePreviewImpl(FileUtils fileUtils,
|
|
|
|
|
PdfUtils pdfUtils,
|
|
|
|
|
DownloadUtils downloadUtils) {
|
|
|
|
|
this.fileUtils = fileUtils;
|
|
|
|
|
this.pdfUtils = pdfUtils;
|
|
|
|
|
this.downloadUtils = downloadUtils;
|
|
|
|
|
}
|
2019-04-28 09:07:23 +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-28 09:07:23 +08:00
|
|
|
|
String suffix=fileAttribute.getSuffix();
|
|
|
|
|
String fileName=fileAttribute.getName();
|
2019-05-09 15:00:13 +08:00
|
|
|
|
String officePreviewType = model.asMap().get("officePreviewType") == null ? ConfigConstants.getOfficePreviewType() : model.asMap().get("officePreviewType").toString();
|
2020-05-13 19:40:31 +08:00
|
|
|
|
String baseUrl = BaseUrlFilter.getBaseUrl();
|
2019-04-28 09:07:23 +08:00
|
|
|
|
String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + "pdf";
|
2020-05-19 14:21:38 +08:00
|
|
|
|
String outFilePath = FILE_DIR + pdfName;
|
2020-12-25 18:19:30 +08:00
|
|
|
|
model.addAttribute("switchDisabled", ConfigConstants.getOfficePreviewSwitchDisabled());
|
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
|
|
|
|
//当文件不存在时,就去下载
|
2020-05-19 14:21:38 +08:00
|
|
|
|
if (!fileUtils.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
|
|
|
|
|
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, fileName);
|
|
|
|
|
if (0 != response.getCode()) {
|
|
|
|
|
model.addAttribute("fileType", suffix);
|
|
|
|
|
model.addAttribute("msg", response.getMsg());
|
|
|
|
|
return "fileNotSupported";
|
|
|
|
|
}
|
|
|
|
|
outFilePath = response.getContent();
|
|
|
|
|
if (ConfigConstants.isCacheEnabled()) {
|
|
|
|
|
// 加入缓存
|
|
|
|
|
fileUtils.addConvertedFile(pdfName, fileUtils.getRelativePath(outFilePath));
|
|
|
|
|
}
|
2019-04-28 09:07:23 +08:00
|
|
|
|
}
|
2019-10-31 16:52:58 +08:00
|
|
|
|
List<String> imageUrls = pdfUtils.pdf2jpg(outFilePath, pdfName, baseUrl);
|
2019-04-28 09:07:23 +08:00
|
|
|
|
if (imageUrls == null || imageUrls.size() < 1) {
|
|
|
|
|
model.addAttribute("msg", "pdf转图片异常,请联系管理员");
|
|
|
|
|
model.addAttribute("fileType",fileAttribute.getSuffix());
|
|
|
|
|
return "fileNotSupported";
|
|
|
|
|
}
|
|
|
|
|
model.addAttribute("imgurls", imageUrls);
|
|
|
|
|
model.addAttribute("currentUrl", imageUrls.get(0));
|
|
|
|
|
if (OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType)) {
|
|
|
|
|
return "officePicture";
|
|
|
|
|
} else {
|
|
|
|
|
return "picture";
|
|
|
|
|
}
|
2020-05-14 16:22:48 +08:00
|
|
|
|
} else {
|
|
|
|
|
// 不是http开头,浏览器不能直接访问,需下载到本地
|
|
|
|
|
if (url != null && !url.toLowerCase().startsWith("http")) {
|
2020-05-19 14:21:38 +08:00
|
|
|
|
if (!fileUtils.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
|
|
|
|
|
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, pdfName);
|
|
|
|
|
if (0 != response.getCode()) {
|
|
|
|
|
model.addAttribute("fileType", suffix);
|
|
|
|
|
model.addAttribute("msg", response.getMsg());
|
|
|
|
|
return "fileNotSupported";
|
|
|
|
|
}
|
2020-05-14 16:22:48 +08:00
|
|
|
|
model.addAttribute("pdfUrl", fileUtils.getRelativePath(response.getContent()));
|
2020-05-19 14:21:38 +08:00
|
|
|
|
if (ConfigConstants.isCacheEnabled()) {
|
|
|
|
|
// 加入缓存
|
|
|
|
|
fileUtils.addConvertedFile(pdfName, fileUtils.getRelativePath(outFilePath));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
model.addAttribute("pdfUrl", pdfName);
|
2020-05-14 16:22:48 +08:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
model.addAttribute("pdfUrl", url);
|
|
|
|
|
}
|
2019-04-28 09:07:23 +08:00
|
|
|
|
}
|
2018-01-17 17:51:53 +08:00
|
|
|
|
return "pdf";
|
|
|
|
|
}
|
|
|
|
|
}
|