@@ -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 );
0 commit comments