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-02-08 18:02:49 +08:00
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import org.springframework.ui.Model;
|
2023-10-23 17:06:05 +08:00
|
|
|
|
import org.springframework.util.ObjectUtils;
|
2021-12-03 19:05:31 +08:00
|
|
|
|
|
2023-10-23 17:06:05 +08:00
|
|
|
|
import java.io.IOException;
|
2021-12-03 19:05:31 +08:00
|
|
|
|
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-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();
|
2021-12-05 12:38:30 +08:00
|
|
|
|
String tifPreviewType = ConfigConstants.getTifPreviewType();
|
2023-10-23 17:06:05 +08:00
|
|
|
|
String cacheName = fileAttribute.getcacheName();
|
|
|
|
|
|
String outFilePath = fileAttribute.getoutFilePath();
|
|
|
|
|
|
String fileKey = fileAttribute.getFileKey(); //判断是否压缩包
|
2023-04-23 11:22:29 +08:00
|
|
|
|
boolean forceUpdatedCache=fileAttribute.forceUpdatedCache();
|
2023-10-23 17:06:05 +08:00
|
|
|
|
if ("jpg".equalsIgnoreCase(tifPreviewType) || "pdf".equalsIgnoreCase(tifPreviewType)) {
|
|
|
|
|
|
if (forceUpdatedCache || !fileHandlerService.listConvertedFiles().containsKey(cacheName) || !ConfigConstants.isCacheEnabled()) {
|
|
|
|
|
|
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
|
|
|
|
|
|
if (response.isFailure()) {
|
|
|
|
|
|
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
|
|
|
|
|
}
|
|
|
|
|
|
String filePath = response.getContent();
|
|
|
|
|
|
if ("pdf".equalsIgnoreCase(tifPreviewType)) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
ConvertPicUtil.convertJpg2Pdf(filePath, outFilePath);
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
if (e.getMessage().contains("Bad endianness tag (not 0x4949 or 0x4d4d)") ) {
|
|
|
|
|
|
model.addAttribute("imgUrls", url);
|
|
|
|
|
|
model.addAttribute("currentUrl", url);
|
|
|
|
|
|
return PICTURE_FILE_PREVIEW_PAGE;
|
|
|
|
|
|
}else {
|
|
|
|
|
|
return otherFilePreview.notSupportedFile(model, fileAttribute, "TIF转pdf异常,请联系系统管理员!" );
|
2023-04-03 07:08:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-10-23 17:06:05 +08:00
|
|
|
|
//是否保留TIFF源文件
|
|
|
|
|
|
if (ObjectUtils.isEmpty(fileKey) && ConfigConstants.getDeleteSourceFile()) {
|
|
|
|
|
|
// KkFileUtils.deleteFileByPath(filePath);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (ConfigConstants.isCacheEnabled()) {
|
|
|
|
|
|
// 加入缓存
|
|
|
|
|
|
fileHandlerService.addConvertedFile(cacheName, fileHandlerService.getRelativePath(outFilePath));
|
|
|
|
|
|
}
|
|
|
|
|
|
model.addAttribute("pdfUrl", cacheName);
|
2021-12-16 17:05:18 +08:00
|
|
|
|
return PDF_FILE_PREVIEW_PAGE;
|
2023-10-23 17:06:05 +08:00
|
|
|
|
}else {
|
|
|
|
|
|
// 将tif转换为jpg,返回转换后的文件路径、文件名的list
|
|
|
|
|
|
List<String> listPic2Jpg;
|
|
|
|
|
|
try {
|
|
|
|
|
|
listPic2Jpg = ConvertPicUtil.convertTif2Jpg(filePath, outFilePath,forceUpdatedCache);
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
if (e.getMessage().contains("Bad endianness tag (not 0x4949 or 0x4d4d)") ) {
|
|
|
|
|
|
model.addAttribute("imgUrls", url);
|
|
|
|
|
|
model.addAttribute("currentUrl", url);
|
|
|
|
|
|
return PICTURE_FILE_PREVIEW_PAGE;
|
|
|
|
|
|
}else {
|
|
|
|
|
|
return otherFilePreview.notSupportedFile(model, fileAttribute, "TIF转JPG异常,请联系系统管理员!" );
|
|
|
|
|
|
}
|
2023-04-04 01:13:03 +00:00
|
|
|
|
}
|
2023-10-23 17:06:05 +08:00
|
|
|
|
//是否保留源文件,转换失败保留源文件,转换成功删除源文件
|
|
|
|
|
|
if(ObjectUtils.isEmpty(fileKey) && ConfigConstants.getDeleteSourceFile()) {
|
|
|
|
|
|
KkFileUtils.deleteFileByPath(filePath);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (ConfigConstants.isCacheEnabled()) {
|
|
|
|
|
|
// 加入缓存
|
|
|
|
|
|
fileHandlerService.putImgCache(cacheName, listPic2Jpg);
|
|
|
|
|
|
fileHandlerService.addConvertedFile(cacheName, fileHandlerService.getRelativePath(outFilePath));
|
|
|
|
|
|
}
|
|
|
|
|
|
model.addAttribute("imgUrls", listPic2Jpg);
|
|
|
|
|
|
model.addAttribute("currentUrl", listPic2Jpg.get(0));
|
|
|
|
|
|
return PICTURE_FILE_PREVIEW_PAGE;
|
2023-04-04 01:13:03 +00:00
|
|
|
|
}
|
2023-10-23 17:06:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
if ("pdf".equalsIgnoreCase(tifPreviewType)) {
|
|
|
|
|
|
model.addAttribute("pdfUrl", fileHandlerService.listConvertedFiles().get(cacheName));
|
|
|
|
|
|
return PDF_FILE_PREVIEW_PAGE;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if ("jpg".equalsIgnoreCase(tifPreviewType)) {
|
|
|
|
|
|
model.addAttribute("imgUrls", fileHandlerService.getImgCache(cacheName));
|
|
|
|
|
|
model.addAttribute("currentUrl", fileHandlerService.getImgCache(cacheName).get(0));
|
|
|
|
|
|
return PICTURE_FILE_PREVIEW_PAGE;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// 不是http开头,浏览器不能直接访问,需下载到本地
|
|
|
|
|
|
if (url != null && !url.toLowerCase().startsWith("http")) {
|
|
|
|
|
|
if (forceUpdatedCache || !fileHandlerService.listConvertedFiles().containsKey(fileName) || !ConfigConstants.isCacheEnabled()) {
|
|
|
|
|
|
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
|
|
|
|
|
|
if (response.isFailure()) {
|
|
|
|
|
|
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
|
|
|
|
|
}
|
|
|
|
|
|
model.addAttribute("currentUrl", fileHandlerService.getRelativePath(response.getContent()));
|
|
|
|
|
|
if (ConfigConstants.isCacheEnabled()) {
|
|
|
|
|
|
// 加入缓存
|
|
|
|
|
|
fileHandlerService.addConvertedFile(fileName, fileHandlerService.getRelativePath(outFilePath));
|
2021-12-05 12:38:30 +08:00
|
|
|
|
}
|
2023-10-23 17:06:05 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
model.addAttribute("currentUrl", fileName);
|
2021-12-16 17:05:18 +08:00
|
|
|
|
}
|
2023-10-23 17:06:05 +08:00
|
|
|
|
return TIFF_FILE_PREVIEW_PAGE;
|
2021-12-03 19:05:31 +08:00
|
|
|
|
}
|
2023-10-23 17:06:05 +08:00
|
|
|
|
model.addAttribute("currentUrl", url);
|
|
|
|
|
|
return TIFF_FILE_PREVIEW_PAGE;
|
2021-02-08 18:02:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-10-23 17:06:05 +08:00
|
|
|
|
|