重构 pdf2jpg 的逻辑 (#452)

* 重构 pdf2jpg 的逻辑

* 重构 pdf2jpg 的逻辑
pull/111/head
kl 2023-04-24 10:08:18 +08:00 committed by GitHub
parent 06005201de
commit 2d5f5f0f23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 33 deletions

View File

@ -22,6 +22,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
@ -34,6 +35,8 @@ import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.IntStream;
/**
* @author yudian-it
@ -44,6 +47,7 @@ public class FileHandlerService {
private final Logger logger = LoggerFactory.getLogger(FileHandlerService.class);
private final String fileDir = ConfigConstants.getFileDir();
private final static String pdf2jpg_image_format = ".jpg";
private final CacheService cacheService;
@Value("${server.tomcat.uri-encoding:UTF-8}")
@ -71,7 +75,7 @@ public class FileHandlerService {
* @param key pdf
* @return pdf
*/
public Integer getConvertedPdfImage(String key) {
public Integer getPdf2jpgCache(String key) {
return cacheService.getPdfImageCache(key);
}
@ -112,7 +116,7 @@ public class FileHandlerService {
* @param pdfFilePath pdf
* @param num
*/
public void addConvertedPdfImage(String pdfFilePath, int num) {
public void addPdf2jpgCache(String pdfFilePath, int num) {
cacheService.putPdfImageCache(pdfFilePath, num);
}
@ -170,34 +174,60 @@ public class FileHandlerService {
}
/**
* pdfjpg
* @param pdfFilePath pdf
* @param pdfName pdf
* @param baseUrl 访
* @return 访
* pdf image web 访
* @param pdfName pdf
* @param index
* @return 访
*/
public List<String> pdf2jpg(String pdfFilePath, String pdfName, String baseUrl, FileAttribute fileAttribute) {
List<String> imageUrls = new ArrayList<>();
Integer imageCount = null;
String imageFileSuffix = ".jpg";
private String getPdf2jpgUrl(String pdfName, int index) {
String baseUrl = ConfigConstants.getBaseUrl();
String pdfFolder = pdfName.substring(0, pdfName.length() - 4);
boolean forceUpdatedCache=fileAttribute.forceUpdatedCache();
if (!forceUpdatedCache){
imageCount = this.getConvertedPdfImage(pdfFilePath);
}
String urlPrefix;
try {
urlPrefix = baseUrl + URLEncoder.encode(pdfFolder, uriEncoding).replaceAll("\\+", "%20");
} catch (UnsupportedEncodingException e) {
logger.error("UnsupportedEncodingException", e);
urlPrefix = baseUrl + pdfFolder;
}
if (imageCount != null && imageCount > 0) {
for (int i = 0; i < imageCount; i++) {
imageUrls.add(urlPrefix + "/" + i + imageFileSuffix);
}
return urlPrefix + "/" + index + pdf2jpg_image_format;
}
/**
* pdf jpg
* @param pdfFilePath pdf
* @param pdfName pdf
* @return 访
*/
private List<String> loadPdf2jpgCache(String pdfFilePath, String pdfName) {
List<String> imageUrls = new ArrayList<>();
Integer imageCount = this.getPdf2jpgCache(pdfFilePath);
if (Objects.isNull(imageCount)) {
return imageUrls;
}
IntStream.range(0, imageCount).forEach(i -> {
String imageUrl = this.getPdf2jpgUrl(pdfName, i);
imageUrls.add(imageUrl);
});
return imageUrls;
}
/**
* pdfjpg
*
* @param pdfFilePath pdf
* @param pdfName pdf
* @return 访
*/
public List<String> pdf2jpg(String pdfFilePath, String pdfName, FileAttribute fileAttribute) {
boolean forceUpdatedCache = fileAttribute.forceUpdatedCache();
if (!forceUpdatedCache) {
List<String> cacheResult = this.loadPdf2jpgCache(pdfFilePath, pdfName);
if (!CollectionUtils.isEmpty(cacheResult)) {
return cacheResult;
}
}
List<String> imageUrls = new ArrayList<>();
try {
File pdfFile = new File(pdfFilePath);
if (!pdfFile.exists()) {
@ -217,13 +247,14 @@ public class FileHandlerService {
}
String imageFilePath;
for (int pageIndex = 0; pageIndex < pageCount; pageIndex++) {
imageFilePath = folder + File.separator + pageIndex + imageFileSuffix;
imageFilePath = folder + File.separator + pageIndex + pdf2jpg_image_format;
BufferedImage image = pdfRenderer.renderImageWithDPI(pageIndex, 105, ImageType.RGB);
ImageIOUtil.writeImage(image, imageFilePath, 105);
imageUrls.add(urlPrefix + "/" + pageIndex + imageFileSuffix);
String imageUrl = this.getPdf2jpgUrl(pdfName, pageIndex);
imageUrls.add(imageUrl);
}
doc.close();
this.addConvertedPdfImage(pdfFilePath, pageCount);
this.addPdf2jpgCache(pdfFilePath, pageCount);
} catch (IOException e) {
logger.error("Convert pdf to jpg exception, pdfFilePath{}", pdfFilePath, e);
}
@ -232,11 +263,12 @@ public class FileHandlerService {
/**
* cadpdf
* @param inputFilePath cad
*
* @param inputFilePath cad
* @param outputFilePath pdf
* @return
*/
public boolean cadToPdf(String inputFilePath, String outputFilePath) {
public boolean cadToPdf(String inputFilePath, String outputFilePath) {
File outputFile = new File(outputFilePath);
LoadOptions opts = new LoadOptions();
opts.setSpecifiedEncoding(CodePages.SimpChinese);
@ -246,7 +278,7 @@ public class FileHandlerService {
cadRasterizationOptions.setPageWidth(1400);
cadRasterizationOptions.setPageHeight(650);
cadRasterizationOptions.setAutomaticLayoutsScaling(true);
cadRasterizationOptions.setNoScaling (false);
cadRasterizationOptions.setNoScaling(false);
cadRasterizationOptions.setDrawType(1);
PdfOptions pdfOptions = new PdfOptions();
pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);
@ -259,8 +291,8 @@ public class FileHandlerService {
return true;
} catch (IOException e) {
logger.error("PDFFileNotFoundExceptioninputFilePath{}", inputFilePath, e);
}finally{
if(cadImage != null){ //关闭
} finally {
if (cadImage != null) { //关闭
cadImage.close();
}
}
@ -297,12 +329,12 @@ public class FileHandlerService {
suffix = WebUtils.suffixFromUrl(url);
}
if (url.contains("?fileKey=")) {
fileName=urlStrr;
fileName = urlStrr;
attribute.setSkipDownLoad(true);
}
// System.out.println(fileName);
url = WebUtils.encodeUrlFileName(url);
fileName = KkFileUtils.htmlEscape(fileName); //文件名处理
fileName = KkFileUtils.htmlEscape(fileName); //文件名处理
attribute.setType(type);
attribute.setName(fileName);
attribute.setSuffix(suffix);
@ -310,14 +342,14 @@ public class FileHandlerService {
if (req != null) {
String officePreviewType = req.getParameter("officePreviewType");
String forceUpdatedCache = req.getParameter("forceUpdatedCache");
String fileKey = WebUtils.getUrlParameterReg(url,"fileKey");
String fileKey = WebUtils.getUrlParameterReg(url, "fileKey");
if (StringUtils.hasText(officePreviewType)) {
attribute.setOfficePreviewType(officePreviewType);
}
if (StringUtils.hasText(fileKey)) {
attribute.setFileKey(fileKey);
}
if ( "true".equalsIgnoreCase(forceUpdatedCache)) {
if ("true".equalsIgnoreCase(forceUpdatedCache)) {
attribute.setforceUpdatedCache(true);
}
@ -349,6 +381,7 @@ public class FileHandlerService {
/**
*
*
* @param fileName
* @param value
*/

View File

@ -133,7 +133,7 @@ public class OfficeFilePreviewImpl implements FilePreview {
static String getPreviewType(Model model, FileAttribute fileAttribute, String officePreviewType, String baseUrl, String pdfName, String outFilePath, FileHandlerService fileHandlerService, String officePreviewTypeImage, OtherFilePreviewImpl otherFilePreview) {
String suffix = fileAttribute.getSuffix();
boolean isPPT = suffix.equalsIgnoreCase("ppt") || suffix.equalsIgnoreCase("pptx");
List<String> imageUrls = fileHandlerService.pdf2jpg(outFilePath, pdfName, baseUrl, fileAttribute);
List<String> imageUrls = fileHandlerService.pdf2jpg(outFilePath, pdfName, fileAttribute);
if (imageUrls == null || imageUrls.size() < 1) {
return otherFilePreview.notSupportedFile(model, fileAttribute, "office转图片异常请联系管理员");
}

View File

@ -50,7 +50,7 @@ public class PdfFilePreviewImpl implements FilePreview {
fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(outFilePath));
}
}
List<String> imageUrls = fileHandlerService.pdf2jpg(outFilePath, pdfName, baseUrl, fileAttribute);
List<String> imageUrls = fileHandlerService.pdf2jpg(outFilePath, pdfName, fileAttribute);
if (imageUrls == null || imageUrls.size() < 1) {
return otherFilePreview.notSupportedFile(model, fileAttribute, "pdf转图片异常请联系管理员");
}