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;
|
2023-07-20 10:54:47 +08:00
|
|
|
|
import cn.keking.service.FileHandlerService;
|
2019-11-21 17:01:14 +08:00
|
|
|
|
import cn.keking.service.FilePreview;
|
|
|
|
|
|
import cn.keking.utils.DownloadUtils;
|
2023-04-21 09:56:46 +08:00
|
|
|
|
import cn.keking.utils.KkFileUtils;
|
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;
|
2020-12-27 14:47:28 +08:00
|
|
|
|
private final OtherFilePreviewImpl otherFilePreview;
|
2020-12-27 14:06:06 +08:00
|
|
|
|
|
2020-12-27 14:47:28 +08:00
|
|
|
|
public CadFilePreviewImpl(FileHandlerService fileHandlerService, OtherFilePreviewImpl otherFilePreview) {
|
2020-12-27 14:06:06 +08:00
|
|
|
|
this.fileHandlerService = fileHandlerService;
|
2020-12-27 14:47:28 +08:00
|
|
|
|
this.otherFilePreview = otherFilePreview;
|
2020-12-27 14:06:06 +08:00
|
|
|
|
}
|
2019-11-21 17:01:14 +08:00
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
|
|
|
|
|
// 预览Type,参数传了就取参数的,没传取系统默认
|
2021-01-21 15:47:13 +08:00
|
|
|
|
String officePreviewType = fileAttribute.getOfficePreviewType() == null ? ConfigConstants.getOfficePreviewType() : fileAttribute.getOfficePreviewType();
|
2020-05-13 19:40:31 +08:00
|
|
|
|
String baseUrl = BaseUrlFilter.getBaseUrl();
|
2023-04-23 11:22:29 +08:00
|
|
|
|
boolean forceUpdatedCache=fileAttribute.forceUpdatedCache();
|
2020-12-27 14:06:06 +08:00
|
|
|
|
String fileName = fileAttribute.getName();
|
2023-04-23 11:25:39 +08:00
|
|
|
|
String suffix = fileAttribute.getSuffix();
|
2023-07-20 10:54:47 +08:00
|
|
|
|
String cadPreviewType = ConfigConstants.getCadPreviewType();
|
|
|
|
|
|
String pdfName = fileName.substring(0, fileName.lastIndexOf(".")) + suffix +"." + cadPreviewType ; //生成文件添加类型后缀 防止同名文件
|
2020-05-18 09:46:52 +08:00
|
|
|
|
String outFilePath = FILE_DIR + pdfName;
|
2019-11-21 17:01:14 +08:00
|
|
|
|
// 判断之前是否已转换过,如果转换过,直接返回,否则执行转换
|
2023-04-23 11:22:29 +08:00
|
|
|
|
if (forceUpdatedCache || !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()) {
|
2020-12-27 14:47:28 +08:00
|
|
|
|
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
2019-11-21 17:01:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
filePath = response.getContent();
|
2023-05-12 01:51:27 +00:00
|
|
|
|
String imageUrls = null;
|
2019-11-21 17:01:14 +08:00
|
|
|
|
if (StringUtils.hasText(outFilePath)) {
|
2023-05-12 01:51:27 +00:00
|
|
|
|
try {
|
2023-07-20 10:54:47 +08:00
|
|
|
|
imageUrls = fileHandlerService.cadToPdf(filePath, outFilePath,cadPreviewType);
|
2023-05-12 01:51:27 +00:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
}
|
|
|
|
|
|
if (imageUrls == null ) {
|
|
|
|
|
|
return otherFilePreview.notSupportedFile(model, fileAttribute, "office转图片异常,请联系管理员");
|
2019-11-21 17:01:14 +08:00
|
|
|
|
}
|
2023-05-06 16:51:06 +08:00
|
|
|
|
//是否保留CAD源文件
|
|
|
|
|
|
if( ConfigConstants.getDeleteSourceFile()) {
|
2023-04-21 09:56:46 +08:00
|
|
|
|
KkFileUtils.deleteFileByPath(filePath);
|
|
|
|
|
|
}
|
2019-11-21 17:01:14 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-07-20 10:54:47 +08:00
|
|
|
|
if("tif".equalsIgnoreCase(cadPreviewType)){
|
|
|
|
|
|
model.addAttribute("currentUrl", pdfName);
|
|
|
|
|
|
return TIFF_FILE_PREVIEW_PAGE;
|
|
|
|
|
|
}else if("svg".equalsIgnoreCase(cadPreviewType)){
|
|
|
|
|
|
model.addAttribute("currentUrl", pdfName);
|
|
|
|
|
|
return SVG_FILE_PREVIEW_PAGE;
|
|
|
|
|
|
}
|
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:47:28 +08:00
|
|
|
|
return getPreviewType(model, fileAttribute, officePreviewType, baseUrl, pdfName, outFilePath, fileHandlerService, OFFICE_PREVIEW_TYPE_IMAGE,otherFilePreview);
|
2019-11-21 17:01:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
model.addAttribute("pdfUrl", pdfName);
|
2020-12-27 15:14:32 +08:00
|
|
|
|
return PDF_FILE_PREVIEW_PAGE;
|
2019-11-21 17:01:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|