mirror of
https://gitee.com/kekingcn/file-online-preview.git
synced 2025-10-07 23:24:30 +08:00
重构 pdf2jpg 的逻辑
This commit is contained in:
@@ -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}")
|
||||
@@ -170,34 +174,60 @@ public class FileHandlerService {
|
||||
}
|
||||
|
||||
/**
|
||||
* pdf文件转换成jpg图片集
|
||||
* @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.getConvertedPdfImage(pdfFilePath);
|
||||
if (Objects.isNull(imageCount)) {
|
||||
return imageUrls;
|
||||
}
|
||||
IntStream.range(0, imageCount).forEach(i -> {
|
||||
String imageUrl = this.getPdf2jpgUrl(pdfName, i);
|
||||
imageUrls.add(imageUrl);
|
||||
});
|
||||
return imageUrls;
|
||||
}
|
||||
|
||||
/**
|
||||
* pdf文件转换成jpg图片集
|
||||
*
|
||||
* @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,10 +247,11 @@ 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);
|
||||
@@ -232,6 +263,7 @@ public class FileHandlerService {
|
||||
|
||||
/**
|
||||
* cad文件转pdf
|
||||
*
|
||||
* @param inputFilePath cad文件路径
|
||||
* @param outputFilePath pdf输出文件路径
|
||||
* @return 转换是否成功
|
||||
@@ -349,6 +381,7 @@ public class FileHandlerService {
|
||||
|
||||
/**
|
||||
* 添加转换后的视频文件缓存
|
||||
*
|
||||
* @param fileName
|
||||
* @param value
|
||||
*/
|
||||
|
@@ -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转图片异常,请联系管理员");
|
||||
}
|
||||
|
@@ -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转图片异常,请联系管理员");
|
||||
}
|
||||
|
Reference in New Issue
Block a user