2019-11-21 17:01:14 +08:00
|
|
|
|
package cn.keking.service.impl;
|
|
|
|
|
|
|
|
|
|
|
|
import cn.keking.config.ConfigConstants;
|
|
|
|
|
|
import cn.keking.model.FileAttribute;
|
|
|
|
|
|
import cn.keking.model.ReturnResponse;
|
|
|
|
|
|
import cn.keking.service.FilePreview;
|
|
|
|
|
|
import cn.keking.utils.DownloadUtils;
|
2020-12-26 19:13:50 +08:00
|
|
|
|
import cn.keking.service.FileHandlerService;
|
2020-05-13 19:40:31 +08:00
|
|
|
|
import cn.keking.web.filter.BaseUrlFilter;
|
2019-11-21 17:01:14 +08:00
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import org.springframework.ui.Model;
|
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
2020-05-15 18:09:19 +08:00
|
|
|
|
import static cn.keking.service.impl.OfficeFilePreviewImpl.getPreviewType;
|
2019-11-21 17:01:14 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @author chenjh
|
|
|
|
|
|
* @since 2019/11/21 14:28
|
|
|
|
|
|
*/
|
|
|
|
|
|
@Service
|
|
|
|
|
|
public class CadFilePreviewImpl implements FilePreview {
|
|
|
|
|
|
|
2020-05-18 09:46:52 +08:00
|
|
|
|
private static final String OFFICE_PREVIEW_TYPE_IMAGE = "image";
|
|
|
|
|
|
private static final String OFFICE_PREVIEW_TYPE_ALL_IMAGES = "allImages";
|
|
|
|
|
|
private static final String FILE_DIR = ConfigConstants.getFileDir();
|
2019-11-21 17:01:14 +08:00
|
|
|
|
|
2020-12-27 14:06:06 +08:00
|
|
|
|
private final FileHandlerService fileHandlerService;
|
|
|
|
|
|
|
|
|
|
|
|
public CadFilePreviewImpl(FileHandlerService fileHandlerService) {
|
|
|
|
|
|
this.fileHandlerService = fileHandlerService;
|
|
|
|
|
|
}
|
2019-11-21 17:01:14 +08:00
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
|
|
|
|
|
// 预览Type,参数传了就取参数的,没传取系统默认
|
|
|
|
|
|
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();
|
2020-12-27 14:06:06 +08:00
|
|
|
|
String suffix = fileAttribute.getSuffix();
|
|
|
|
|
|
String fileName = fileAttribute.getName();
|
2019-11-21 17:01:14 +08:00
|
|
|
|
String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + "pdf";
|
2020-05-18 09:46:52 +08:00
|
|
|
|
String outFilePath = FILE_DIR + pdfName;
|
2019-11-21 17:01:14 +08:00
|
|
|
|
// 判断之前是否已转换过,如果转换过,直接返回,否则执行转换
|
2020-12-26 19:13:50 +08:00
|
|
|
|
if (!fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
|
2020-05-15 18:09:19 +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()) {
|
2019-11-21 17:01:14 +08:00
|
|
|
|
model.addAttribute("fileType", suffix);
|
|
|
|
|
|
model.addAttribute("msg", response.getMsg());
|
|
|
|
|
|
return "fileNotSupported";
|
|
|
|
|
|
}
|
|
|
|
|
|
filePath = response.getContent();
|
|
|
|
|
|
if (StringUtils.hasText(outFilePath)) {
|
2020-12-27 14:06:06 +08:00
|
|
|
|
boolean convertResult = fileHandlerService.cadToPdf(filePath, outFilePath);
|
2019-11-21 17:01:14 +08:00
|
|
|
|
if (!convertResult) {
|
|
|
|
|
|
model.addAttribute("fileType", suffix);
|
|
|
|
|
|
model.addAttribute("msg", "cad文件转换异常,请联系管理员");
|
|
|
|
|
|
return "fileNotSupported";
|
|
|
|
|
|
}
|
|
|
|
|
|
if (ConfigConstants.isCacheEnabled()) {
|
|
|
|
|
|
// 加入缓存
|
2020-12-26 19:13:50 +08:00
|
|
|
|
fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(outFilePath));
|
2019-11-21 17:01:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-05-18 09:46:52 +08:00
|
|
|
|
if (baseUrl != null && (OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OFFICE_PREVIEW_TYPE_ALL_IMAGES.equals(officePreviewType))) {
|
2020-12-27 14:06:06 +08:00
|
|
|
|
return getPreviewType(model, fileAttribute, officePreviewType, baseUrl, pdfName, outFilePath, fileHandlerService, OFFICE_PREVIEW_TYPE_IMAGE);
|
2019-11-21 17:01:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
model.addAttribute("pdfUrl", pdfName);
|
|
|
|
|
|
return "pdf";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|