Skip to content

Commit c7318c2

Browse files
gitchenjhklboke
authored andcommitted
优化:PDF文件下载缓存
1 parent c16116c commit c7318c2

2 files changed

Lines changed: 29 additions & 14 deletions

File tree

jodconverter-web/src/main/java/cn/keking/service/impl/PdfFilePreviewImpl.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public class PdfFilePreviewImpl implements FilePreview {
2626

2727
private final DownloadUtils downloadUtils;
2828

29+
private static final String FILE_DIR = ConfigConstants.getFileDir();
30+
2931
public PdfFilePreviewImpl(FileUtils fileUtils,
3032
PdfUtils pdfUtils,
3133
DownloadUtils downloadUtils) {
@@ -41,16 +43,22 @@ public String filePreviewHandle(String url, Model model, FileAttribute fileAttri
4143
String officePreviewType = model.asMap().get("officePreviewType") == null ? ConfigConstants.getOfficePreviewType() : model.asMap().get("officePreviewType").toString();
4244
String baseUrl = BaseUrlFilter.getBaseUrl();
4345
String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + "pdf";
44-
String outFilePath;
46+
String outFilePath = FILE_DIR + pdfName;
4547
if (OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_ALL_IMAGES.equals(officePreviewType)) {
4648
//当文件不存在时,就去下载
47-
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, fileName);
48-
if (0 != response.getCode()) {
49-
model.addAttribute("fileType", suffix);
50-
model.addAttribute("msg", response.getMsg());
51-
return "fileNotSupported";
49+
if (!fileUtils.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
50+
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, fileName);
51+
if (0 != response.getCode()) {
52+
model.addAttribute("fileType", suffix);
53+
model.addAttribute("msg", response.getMsg());
54+
return "fileNotSupported";
55+
}
56+
outFilePath = response.getContent();
57+
if (ConfigConstants.isCacheEnabled()) {
58+
// 加入缓存
59+
fileUtils.addConvertedFile(pdfName, fileUtils.getRelativePath(outFilePath));
60+
}
5261
}
53-
outFilePath = response.getContent();
5462
List<String> imageUrls = pdfUtils.pdf2jpg(outFilePath, pdfName, baseUrl);
5563
if (imageUrls == null || imageUrls.size() < 1) {
5664
model.addAttribute("msg", "pdf转图片异常,请联系管理员");
@@ -67,13 +75,20 @@ public String filePreviewHandle(String url, Model model, FileAttribute fileAttri
6775
} else {
6876
// 不是http开头,浏览器不能直接访问,需下载到本地
6977
if (url != null && !url.toLowerCase().startsWith("http")) {
70-
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, pdfName);
71-
if (0 != response.getCode()) {
72-
model.addAttribute("fileType", suffix);
73-
model.addAttribute("msg", response.getMsg());
74-
return "fileNotSupported";
75-
} else {
78+
if (!fileUtils.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
79+
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, pdfName);
80+
if (0 != response.getCode()) {
81+
model.addAttribute("fileType", suffix);
82+
model.addAttribute("msg", response.getMsg());
83+
return "fileNotSupported";
84+
}
7685
model.addAttribute("pdfUrl", fileUtils.getRelativePath(response.getContent()));
86+
if (ConfigConstants.isCacheEnabled()) {
87+
// 加入缓存
88+
fileUtils.addConvertedFile(pdfName, fileUtils.getRelativePath(outFilePath));
89+
}
90+
} else {
91+
model.addAttribute("pdfUrl", pdfName);
7792
}
7893
} else {
7994
model.addAttribute("pdfUrl", url);

jodconverter-web/src/main/resources/web/index.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ window.open('http://127.0.0.1:8012/picturesPreview?urls='+encodeURIComponent(fil
195195
}).on('pre-body.bs.table', function (e,data) {
196196
// 每个data添加一列用来操作
197197
$(data).each(function (index, item) {
198-
item.action = "<a class='btn btn-default' target='_blank' href='${baseUrl}onlinePreview?url="+ encodeURIComponent('${baseUrl}' + encodeURI(item.fileName)) +"'>预览</a>" +
198+
item.action = "<a class='btn btn-default' target='_blank' href='${baseUrl}onlinePreview?url="+ encodeURIComponent('${baseUrl}' + item.fileName) +"'>预览</a>" +
199199
"<a class='btn btn-default' href='javascript:void(0);' onclick='deleteFile(\""+item.fileName+"\")'>删除</a>";
200200
});
201201
return data;

0 commit comments

Comments
 (0)