Browse Source

!107 新增功能:添加更新缓存方法&forceUpdatedCache=true
Merge pull request !107 from 高雄/forceUpdatedCache

kailing 1 năm trước cách đây
mục cha
commit
06005201de

+ 7 - 0
server/src/main/java/cn/keking/model/FileAttribute.java

@@ -18,6 +18,7 @@ public class FileAttribute {
     private String officePreviewType = ConfigConstants.getOfficePreviewType();
     private String tifPreviewType;
     private Boolean skipDownLoad = false;
+    private Boolean forceUpdatedCache = false;
 
     public FileAttribute() {
     }
@@ -116,5 +117,11 @@ public class FileAttribute {
     public void setTifPreviewType(String previewType) {
         this.tifPreviewType = previewType;
     }
+    public Boolean forceUpdatedCache() {
+        return forceUpdatedCache;
+    }
+    public  void setforceUpdatedCache(Boolean forceUpdatedCache) {
+        this.forceUpdatedCache = forceUpdatedCache;
+    }
 
 }

+ 10 - 2
server/src/main/java/cn/keking/service/FileHandlerService.java

@@ -176,11 +176,15 @@ public class FileHandlerService {
      * @param baseUrl 基础访问地址
      * @return 图片访问集合
      */
-    public List<String> pdf2jpg(String pdfFilePath, String pdfName, String baseUrl) {
+    public List<String> pdf2jpg(String pdfFilePath, String pdfName, String baseUrl, FileAttribute fileAttribute) {
         List<String> imageUrls = new ArrayList<>();
-        Integer imageCount = this.getConvertedPdfImage(pdfFilePath);
+        Integer imageCount = null;
         String imageFileSuffix = ".jpg";
         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");
@@ -305,6 +309,7 @@ public class FileHandlerService {
         attribute.setUrl(url);
         if (req != null) {
             String officePreviewType = req.getParameter("officePreviewType");
+            String forceUpdatedCache = req.getParameter("forceUpdatedCache");
             String fileKey = WebUtils.getUrlParameterReg(url,"fileKey");
             if (StringUtils.hasText(officePreviewType)) {
                 attribute.setOfficePreviewType(officePreviewType);
@@ -312,6 +317,9 @@ public class FileHandlerService {
             if (StringUtils.hasText(fileKey)) {
                 attribute.setFileKey(fileKey);
             }
+            if ( "true".equalsIgnoreCase(forceUpdatedCache)) {
+                attribute.setforceUpdatedCache(true);
+            }
 
             String tifPreviewType = req.getParameter("tifPreviewType");
             if (StringUtils.hasText(tifPreviewType)) {

+ 2 - 1
server/src/main/java/cn/keking/service/impl/CadFilePreviewImpl.java

@@ -38,11 +38,12 @@ public class CadFilePreviewImpl implements FilePreview {
         // 预览Type,参数传了就取参数的,没传取系统默认
         String officePreviewType = fileAttribute.getOfficePreviewType() == null ? ConfigConstants.getOfficePreviewType() : fileAttribute.getOfficePreviewType();
         String baseUrl = BaseUrlFilter.getBaseUrl();
+        boolean forceUpdatedCache=fileAttribute.forceUpdatedCache();
         String fileName = fileAttribute.getName();
         String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + "pdf";
         String outFilePath = FILE_DIR + pdfName;
         // 判断之前是否已转换过,如果转换过,直接返回,否则执行转换
-        if (!fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
+        if (forceUpdatedCache || !fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
             String filePath;
             ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, null);
             if (response.isFailure()) {

+ 2 - 1
server/src/main/java/cn/keking/service/impl/CompressFilePreviewImpl.java

@@ -38,9 +38,10 @@ public class CompressFilePreviewImpl implements FilePreview {
     public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
         String fileName=fileAttribute.getName();
         String filePassword = fileAttribute.getFilePassword();
+        boolean forceUpdatedCache=fileAttribute.forceUpdatedCache();
         String fileTree = null;
         // 判断文件名是否存在(redis缓存读取)
-        if (!StringUtils.hasText(fileHandlerService.getConvertedFile(fileName))  || !ConfigConstants.isCacheEnabled()) {
+        if (forceUpdatedCache || !StringUtils.hasText(fileHandlerService.getConvertedFile(fileName))  || !ConfigConstants.isCacheEnabled()) {
             ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
             if (response.isFailure()) {
                 return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());

+ 4 - 3
server/src/main/java/cn/keking/service/impl/OfficeFilePreviewImpl.java

@@ -47,12 +47,13 @@ public class OfficeFilePreviewImpl implements FilePreview {
         String suffix = fileAttribute.getSuffix();
         String fileName = fileAttribute.getName();
         String filePassword = fileAttribute.getFilePassword();
+        boolean forceUpdatedCache=fileAttribute.forceUpdatedCache();
         String userToken = fileAttribute.getUserToken();
          boolean isHtml = suffix.equalsIgnoreCase("xls") || suffix.equalsIgnoreCase("xlsx") || suffix.equalsIgnoreCase("csv") || suffix.equalsIgnoreCase("xlsm") || suffix.equalsIgnoreCase("xlt") || suffix.equalsIgnoreCase("xltm") || suffix.equalsIgnoreCase("et") || suffix.equalsIgnoreCase("ett") || suffix.equalsIgnoreCase("xlam");
         String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + (isHtml ? "html" : "pdf");
         String cacheFileName = userToken == null ? pdfName : userToken + "_" + pdfName;
         String outFilePath = FILE_DIR + cacheFileName;
-        if ( !fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
+        if (forceUpdatedCache|| !fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
         // 下载远程文件到本地,如果文件在本地已存在不会重复下载
         ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
         if (response.isFailure()) {
@@ -70,7 +71,7 @@ public class OfficeFilePreviewImpl implements FilePreview {
         if (ConfigConstants.isCacheEnabled()) {
             // 全局开启缓存
             isUseCached = true;
-            if (fileHandlerService.listConvertedFiles().containsKey(cacheFileName)) {
+            if (!forceUpdatedCache && fileHandlerService.listConvertedFiles().containsKey(cacheFileName)) {
                 // 存在缓存
                 isCached = true;
             }
@@ -132,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);
+        List<String> imageUrls = fileHandlerService.pdf2jpg(outFilePath, pdfName, baseUrl, fileAttribute);
         if (imageUrls == null || imageUrls.size() < 1) {
             return otherFilePreview.notSupportedFile(model, fileAttribute, "office转图片异常,请联系管理员");
         }

+ 3 - 2
server/src/main/java/cn/keking/service/impl/PdfFilePreviewImpl.java

@@ -34,11 +34,12 @@ public class PdfFilePreviewImpl implements FilePreview {
         String fileName = fileAttribute.getName();
         String officePreviewType = fileAttribute.getOfficePreviewType();
         String baseUrl = BaseUrlFilter.getBaseUrl();
+        boolean forceUpdatedCache=fileAttribute.forceUpdatedCache();
         String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + "pdf";
         String outFilePath = FILE_DIR + pdfName;
         if (OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_ALL_IMAGES.equals(officePreviewType)) {
             //当文件不存在时,就去下载
-            if (!fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
+            if (forceUpdatedCache || !fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
                 ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
                 if (response.isFailure()) {
                     return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
@@ -49,7 +50,7 @@ public class PdfFilePreviewImpl implements FilePreview {
                     fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(outFilePath));
                 }
             }
-            List<String> imageUrls = fileHandlerService.pdf2jpg(outFilePath, pdfName, baseUrl);
+            List<String> imageUrls = fileHandlerService.pdf2jpg(outFilePath, pdfName, baseUrl, fileAttribute);
             if (imageUrls == null || imageUrls.size() < 1) {
                 return otherFilePreview.notSupportedFile(model, fileAttribute, "pdf转图片异常,请联系管理员");
             }

+ 2 - 1
server/src/main/java/cn/keking/service/impl/SimTextFilePreviewImpl.java

@@ -34,8 +34,9 @@ public class SimTextFilePreviewImpl implements FilePreview {
     @Override
     public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
         String fileName = fileAttribute.getName();
+        boolean forceUpdatedCache=fileAttribute.forceUpdatedCache();
         String filePath = FILE_DIR + fileName;
-        if (!fileHandlerService.listConvertedFiles().containsKey(fileName) || !ConfigConstants.isCacheEnabled()) {
+        if (forceUpdatedCache || !fileHandlerService.listConvertedFiles().containsKey(fileName) || !ConfigConstants.isCacheEnabled()) {
             ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
             if (response.isFailure()) {
                 return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());

+ 2 - 1
server/src/main/java/cn/keking/service/impl/TiffFilePreviewImpl.java

@@ -39,6 +39,7 @@ public class TiffFilePreviewImpl implements FilePreview {
         String baseUrl = BaseUrlFilter.getBaseUrl();
         String tifPreviewType = ConfigConstants.getTifPreviewType();
         String tifOnLinePreviewType = fileAttribute.getTifPreviewType();
+        boolean forceUpdatedCache=fileAttribute.forceUpdatedCache();
         if (StringUtils.hasText(tifOnLinePreviewType)) {
             tifPreviewType = tifOnLinePreviewType;
         }
@@ -52,7 +53,7 @@ public class TiffFilePreviewImpl implements FilePreview {
             String outFilePath = fileDir + pdfName;
             if ("pdf".equalsIgnoreCase(tifPreviewType)) {
                     //当文件不存在时,就去下载
-                    if (!fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
+                    if (forceUpdatedCache || !fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
                         ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
                         if (response.isFailure()) {
                             return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());

+ 1 - 0
server/src/main/resources/web/main/record.ftl

@@ -55,6 +55,7 @@
             <div>
                 1.新增 dcm 等医疗数位影像预览<br>
                 2.新增 drawio 绘图预览<br>
+                3.新增开启缓存的情况下重新生成的命令 &forceUpdatedCache=true <br>
             </div>
         </div>
     <div class="panel panel-success">