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;
|
2021-12-05 12:38:30 +08:00
|
|
|
|
import cn.keking.utils.WebUtils;
|
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-05 12:38:30 +08:00
|
|
|
|
private static final String INITIALIZE_MEMORY_SIZE = "initializeMemorySize";
|
|
|
|
|
|
//默认初始化 50MB 内存
|
|
|
|
|
|
private static final long INITIALIZE_MEMORY_SIZE_VALUE_DEFAULT = 1024L * 1024 * 50;
|
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();
|
|
|
|
|
|
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)) {
|
|
|
|
|
|
String fileSize = WebUtils.getUrlParameterReg(url, INITIALIZE_MEMORY_SIZE);
|
|
|
|
|
|
if (StringUtils.hasText(fileSize)) {
|
|
|
|
|
|
model.addAttribute(INITIALIZE_MEMORY_SIZE, fileSize);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
model.addAttribute(INITIALIZE_MEMORY_SIZE, Long.toString(INITIALIZE_MEMORY_SIZE_VALUE_DEFAULT));
|
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-03 07:08:33 +00:00
|
|
|
|
String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + "pdf";
|
|
|
|
|
|
String jpgName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + "jpg";
|
|
|
|
|
|
String strLocalTif = fileDir + fileName;
|
|
|
|
|
|
String outFilePath = fileDir + pdfName;
|
2022-07-25 17:26:02 +08:00
|
|
|
|
if ("pdf".equalsIgnoreCase(tifPreviewType)) {
|
2023-04-03 07:08:33 +00:00
|
|
|
|
//当文件不存在时,就去下载
|
|
|
|
|
|
if (!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();
|
|
|
|
|
|
if (ConfigConstants.isCacheEnabled()) {
|
|
|
|
|
|
// 加入缓存
|
|
|
|
|
|
fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(outFilePath));
|
|
|
|
|
|
}
|
|
|
|
|
|
if(ConvertPicUtil.convertJpg2Pdf(filePath, outFilePath)){
|
|
|
|
|
|
model.addAttribute("pdfUrl", pdfName);
|
|
|
|
|
|
return PDF_FILE_PREVIEW_PAGE;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
model.addAttribute("pdfUrl", pdfName);
|
2021-12-16 17:05:18 +08:00
|
|
|
|
return PDF_FILE_PREVIEW_PAGE;
|
|
|
|
|
|
}
|
2023-04-03 07:08:33 +00:00
|
|
|
|
|
2022-07-25 17:26:02 +08:00
|
|
|
|
} else {
|
2021-12-16 17:05:18 +08:00
|
|
|
|
// 以JPG模式预览的过程
|
2023-04-03 07:08:33 +00:00
|
|
|
|
String strJpgFilePathName = fileDir + jpgName;
|
2021-12-16 17:05:18 +08:00
|
|
|
|
// 将tif转换为jpg,返回转换后的文件路径、文件名的list
|
|
|
|
|
|
List<String> listPic2Jpg = ConvertPicUtil.convertTif2Jpg(strLocalTif, strJpgFilePathName);
|
|
|
|
|
|
// 将返回页面的图片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
|
|
|
|
}
|
|
|
|
|
|
}
|