Skip to content

Commit 5196536

Browse files
gitchenjhklboke
authored andcommitted
修复:修复预览ftp时,图片图片、pdf预览异常
1 parent 59ac8ef commit 5196536

3 files changed

Lines changed: 43 additions & 5 deletions

File tree

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public String filePreviewHandle(String url, Model model, FileAttribute fileAttri
3939
String fileName=fileAttribute.getName();
4040
String officePreviewType = model.asMap().get("officePreviewType") == null ? ConfigConstants.getOfficePreviewType() : model.asMap().get("officePreviewType").toString();
4141
String baseUrl = BaseUrlFilter.getBaseUrl();
42-
model.addAttribute("pdfUrl", url);
4342
String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + "pdf";
4443
String outFilePath = fileDir + pdfName;
4544
if (OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_ALLIMAGES.equals(officePreviewType)) {
@@ -64,6 +63,20 @@ public String filePreviewHandle(String url, Model model, FileAttribute fileAttri
6463
} else {
6564
return "picture";
6665
}
66+
} else {
67+
// 不是http开头,浏览器不能直接访问,需下载到本地
68+
if (url != null && !url.toLowerCase().startsWith("http")) {
69+
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, pdfName);
70+
if (0 != response.getCode()) {
71+
model.addAttribute("fileType", suffix);
72+
model.addAttribute("msg", response.getMsg());
73+
return "fileNotSupported";
74+
} else {
75+
model.addAttribute("pdfUrl", fileUtils.getRelativePath(response.getContent()));
76+
}
77+
} else {
78+
model.addAttribute("pdfUrl", url);
79+
}
6780
}
6881
return "pdf";
6982
}

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package cn.keking.service.impl;
22

3+
import cn.keking.config.ConfigConstants;
34
import cn.keking.model.FileAttribute;
5+
import cn.keking.model.ReturnResponse;
46
import cn.keking.service.FilePreview;
7+
import cn.keking.utils.DownloadUtils;
58
import cn.keking.utils.FileUtils;
69
import com.google.common.collect.Lists;
710
import org.springframework.beans.factory.annotation.Autowired;
@@ -21,18 +24,35 @@ public class PictureFilePreviewImpl implements FilePreview {
2124
@Autowired
2225
FileUtils fileUtils;
2326

27+
@Autowired
28+
DownloadUtils downloadUtils;
29+
2430
@Override
2531
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
2632
String fileKey = (String) RequestContextHolder.currentRequestAttributes().getAttribute("fileKey",0);
2733
List imgUrls = Lists.newArrayList(url);
28-
try{
34+
try {
2935
imgUrls.clear();
3036
imgUrls.addAll(fileUtils.getImgCache(fileKey));
31-
}catch (Exception e){
37+
} catch (Exception e){
3238
imgUrls = Lists.newArrayList(url);
3339
}
34-
model.addAttribute("imgurls", imgUrls);
35-
model.addAttribute("currentUrl",url);
40+
// 不是http开头,浏览器不能直接访问,需下载到本地
41+
if (url != null && !url.toLowerCase().startsWith("http")) {
42+
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, null);
43+
if (0 != response.getCode()) {
44+
model.addAttribute("fileType", fileAttribute.getSuffix());
45+
model.addAttribute("msg", response.getMsg());
46+
return "fileNotSupported";
47+
} else {
48+
String file = fileUtils.getRelativePath(response.getContent());
49+
model.addAttribute("imgurls", Lists.newArrayList(file));
50+
model.addAttribute("currentUrl", file);
51+
}
52+
} else {
53+
model.addAttribute("imgurls", imgUrls);
54+
model.addAttribute("currentUrl", url);
55+
}
3656
return "picture";
3757
}
3858
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
<body>
2525
<ul id="dowebok">
2626
<#list imgurls as img>
27+
<#if img?contains("http://") || img?contains("https://")>
28+
<#assign img="${img}">
29+
<#else>
30+
<#assign img="${baseUrl}${img}">
31+
</#if>
2732
<li><img id="${img}" url="${img}" src="${img}" width="1px" height="1px"></li>
2833
</#list>
2934
</ul>

0 commit comments

Comments
 (0)