2021-02-08 18:02:49 +08:00
|
|
|
|
package cn.keking.service.impl;
|
|
|
|
|
|
|
2021-12-03 19:05:31 +08:00
|
|
|
|
import cn.keking.config.ConfigConstants;
|
2021-02-08 18:02:49 +08:00
|
|
|
|
import cn.keking.model.FileAttribute;
|
2021-12-03 19:05:31 +08:00
|
|
|
|
import cn.keking.model.ReturnResponse;
|
2023-04-03 07:08:33 +00:00
|
|
|
|
import cn.keking.service.FileHandlerService;
|
2021-02-08 18:02:49 +08:00
|
|
|
|
import cn.keking.service.FilePreview;
|
2021-12-03 19:05:31 +08:00
|
|
|
|
import cn.keking.utils.ConvertPicUtil;
|
|
|
|
|
|
import cn.keking.utils.DownloadUtils;
|
2023-04-21 09:56:46 +08:00
|
|
|
|
import cn.keking.utils.KkFileUtils;
|
2021-12-03 19:05:31 +08:00
|
|
|
|
import cn.keking.web.filter.BaseUrlFilter;
|
2021-02-08 18:02:49 +08:00
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import org.springframework.ui.Model;
|
2021-12-05 12:38:30 +08:00
|
|
|
|
import org.springframework.util.StringUtils;
|
2021-12-03 19:05:31 +08:00
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
import java.util.List;
|
2021-02-08 18:02:49 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* tiff 图片文件处理
|
2022-07-25 17:26:02 +08:00
|
|
|
|
*
|
2021-02-08 18:02:49 +08:00
|
|
|
|
* @author kl (http://kailing.pub)
|
|
|
|
|
|
* @since 2021/2/8
|
|
|
|
|
|
*/
|
|
|
|
|
|
@Service
|
|
|
|
|
|
public class TiffFilePreviewImpl implements FilePreview {
|
|
|
|
|
|
|
2023-04-03 07:08:33 +00:00
|
|
|
|
private final FileHandlerService fileHandlerService;
|
|
|
|
|
|
private final OtherFilePreviewImpl otherFilePreview;
|
|
|
|
|
|
public TiffFilePreviewImpl(FileHandlerService fileHandlerService,OtherFilePreviewImpl otherFilePreview) {
|
|
|
|
|
|
this.fileHandlerService = fileHandlerService;
|
|
|
|
|
|
this.otherFilePreview = otherFilePreview;
|
|
|
|
|
|
}
|
2021-12-03 19:05:31 +08:00
|
|
|
|
private final String fileDir = ConfigConstants.getFileDir();
|
2021-02-08 18:02:49 +08:00
|
|
|
|
@Override
|
|
|
|
|
|
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
2023-04-03 07:08:33 +00:00
|
|
|
|
String fileName = fileAttribute.getName();
|
|
|
|
|
|
String baseUrl = BaseUrlFilter.getBaseUrl();
|
2021-12-05 12:38:30 +08:00
|
|
|
|
String tifPreviewType = ConfigConstants.getTifPreviewType();
|
2021-12-10 15:36:25 +08:00
|
|
|
|
String tifOnLinePreviewType = fileAttribute.getTifPreviewType();
|
2023-04-23 11:25:39 +08:00
|
|
|
|
String suffix = fileAttribute.getSuffix();
|
2023-04-23 11:22:29 +08:00
|
|
|
|
boolean forceUpdatedCache=fileAttribute.forceUpdatedCache();
|
2021-12-10 15:36:25 +08:00
|
|
|
|
if (StringUtils.hasText(tifOnLinePreviewType)) {
|
|
|
|
|
|
tifPreviewType = tifOnLinePreviewType;
|
2021-12-09 19:10:23 +08:00
|
|
|
|
}
|
2022-07-25 17:26:02 +08:00
|
|
|
|
if ("tif".equalsIgnoreCase(tifPreviewType)) {
|
2023-04-08 06:07:09 +00:00
|
|
|
|
model.addAttribute("currentUrl", url);
|
2021-12-05 12:38:30 +08:00
|
|
|
|
return TIFF_FILE_PREVIEW_PAGE;
|
2022-07-25 17:26:02 +08:00
|
|
|
|
} else if ("jpg".equalsIgnoreCase(tifPreviewType) || "pdf".equalsIgnoreCase(tifPreviewType)) {
|
2023-04-23 11:25:39 +08:00
|
|
|
|
String pdfName = fileName.substring(0, fileName.lastIndexOf(".")) + suffix +"." + "pdf" ; //生成文件添加类型后缀 防止同名文件
|
2023-04-24 15:36:01 +08:00
|
|
|
|
String jpgName = fileName.substring(0, fileName.lastIndexOf(".")) + suffix; //生成文件添加类型后缀 防止同名文件
|
2023-04-03 07:08:33 +00:00
|
|
|
|
String strLocalTif = fileDir + fileName;
|
|
|
|
|
|
String outFilePath = fileDir + pdfName;
|
2022-07-25 17:26:02 +08:00
|
|
|
|
if ("pdf".equalsIgnoreCase(tifPreviewType)) {
|
2023-04-24 15:36:01 +08:00
|
|
|
|
//当文件不存在时,就去下载
|
|
|
|
|
|
if (forceUpdatedCache || !fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
|
|
|
|
|
|
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
|
|
|
|
|
|
if (response.isFailure()) {
|
|
|
|
|
|
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
|
|
|
|
|
}
|
|
|
|
|
|
String filePath = response.getContent();
|
2023-05-06 16:51:06 +08:00
|
|
|
|
if (ConvertPicUtil.convertJpg2Pdf(filePath, outFilePath)) {
|
|
|
|
|
|
//是否保留TIFF源文件
|
|
|
|
|
|
if (ConfigConstants.getDeleteSourceFile()) {
|
2023-04-24 15:36:01 +08:00
|
|
|
|
KkFileUtils.deleteFileByPath(filePath);
|
2023-04-03 07:08:33 +00:00
|
|
|
|
}
|
2023-04-24 15:36:01 +08:00
|
|
|
|
if (ConfigConstants.isCacheEnabled()) {
|
|
|
|
|
|
// 加入缓存
|
|
|
|
|
|
fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(outFilePath));
|
2023-04-03 07:08:33 +00:00
|
|
|
|
}
|
2023-04-24 15:36:01 +08:00
|
|
|
|
model.addAttribute("pdfUrl", pdfName);
|
|
|
|
|
|
return PDF_FILE_PREVIEW_PAGE;
|
2023-05-06 16:51:06 +08:00
|
|
|
|
} else {
|
2023-04-24 15:36:01 +08:00
|
|
|
|
return NOT_SUPPORTED_FILE_PAGE;
|
2023-04-03 07:08:33 +00:00
|
|
|
|
}
|
2023-05-06 16:51:06 +08:00
|
|
|
|
} else {
|
2023-04-03 07:08:33 +00:00
|
|
|
|
model.addAttribute("pdfUrl", pdfName);
|
2021-12-16 17:05:18 +08:00
|
|
|
|
return PDF_FILE_PREVIEW_PAGE;
|
|
|
|
|
|
}
|
2022-07-25 17:26:02 +08:00
|
|
|
|
} else {
|
2023-04-04 01:13:03 +00:00
|
|
|
|
File fileTiff = new File(strLocalTif);
|
|
|
|
|
|
// 如果本地不存在这个tif文件,则下载
|
|
|
|
|
|
if (!fileTiff.exists()) {
|
|
|
|
|
|
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
|
|
|
|
|
|
if (response.isFailure()) {
|
|
|
|
|
|
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
|
|
|
|
|
}
|
|
|
|
|
|
strLocalTif = response.getContent();
|
|
|
|
|
|
}
|
2021-12-16 17:05:18 +08:00
|
|
|
|
// 将tif转换为jpg,返回转换后的文件路径、文件名的list
|
2023-04-24 15:36:01 +08:00
|
|
|
|
List<String> listPic2Jpg = ConvertPicUtil.convertTif2Jpg(strLocalTif, jpgName);
|
2021-12-16 17:05:18 +08:00
|
|
|
|
// 将返回页面的图片url的list对象
|
|
|
|
|
|
List<String> listImageUrls = new ArrayList<>();
|
|
|
|
|
|
// 循环,拼装url的list对象
|
2022-07-25 17:26:02 +08:00
|
|
|
|
for (String strJpg : listPic2Jpg) {
|
2021-12-16 17:05:18 +08:00
|
|
|
|
listImageUrls.add(baseUrl + strJpg);
|
2021-12-05 12:38:30 +08:00
|
|
|
|
}
|
2021-12-16 17:05:18 +08:00
|
|
|
|
model.addAttribute("imgUrls", listImageUrls);
|
|
|
|
|
|
model.addAttribute("currentUrl", listImageUrls.get(0));
|
|
|
|
|
|
}
|
2021-12-05 12:38:30 +08:00
|
|
|
|
return PICTURE_FILE_PREVIEW_PAGE;
|
2021-12-03 19:05:31 +08:00
|
|
|
|
}
|
2021-12-05 12:38:30 +08:00
|
|
|
|
return NOT_SUPPORTED_FILE_PAGE;
|
2021-02-08 18:02:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|