Browse Source

自定义附件预览

wanghaiwu 3 days ago
parent
commit
c8abd3bcf3

+ 87 - 35
code/base/nckd-jimin-base-helper/src/main/java/nckd/base/helper/DefaultPreviewCusServiceImpl.java

@@ -68,12 +68,12 @@ public class DefaultPreviewCusServiceImpl  implements PreviewService {
     public void init(FileService fileService) {
         logger.info("DefaultPreviewCusServiceImpl:" + fileService.toString());
         this.fileService = fileService;
-//        this.isPreviewcacheEnable = AttachmentFileService.isPreviewcacheEnable() && fileService instanceof AttachmentFileService;
-//        this.isPreviewcacheRefresh = Boolean.parseBoolean(System.getProperty("filepreview.refresh.enable"));
-//        this.uploadTimeout = AttachmentFileService.getUploadTimeout();
-//        this.fileServerDispatcher = DispatcherFactory.build("attachmentServer.url", BosErrorCode.fileServerNotConfigured);
-//        this.previewServerDispatcher = DispatcherFactory.build("yunpan.previewUrl", BosErrorCode.previewFailed);
-//        this.yunHomeService = new YunHomeService(fileService);
+        this.isPreviewcacheEnable = AttachmentFileService.isPreviewcacheEnable() && fileService instanceof AttachmentFileService;
+        this.isPreviewcacheRefresh = Boolean.parseBoolean(System.getProperty("filepreview.refresh.enable"));
+        this.uploadTimeout = AttachmentFileService.getUploadTimeout();
+        this.fileServerDispatcher = DispatcherFactory.build("attachmentServer.url", BosErrorCode.fileServerNotConfigured);
+        this.previewServerDispatcher = DispatcherFactory.build("yunpan.previewUrl", BosErrorCode.previewFailed);
+        this.yunHomeService = new YunHomeService(fileService);
     }
 
     /**
@@ -105,43 +105,90 @@ public class DefaultPreviewCusServiceImpl  implements PreviewService {
     @Override
     public Map<String, Object> previewWPS(String fileName, String path, String userAgent, Map<String, String> config) {
         logger.info("testpreviewWPS...");
-        Map<String, Object> result = new HashMap<>();
-        String ext = getExt(fileName);
 
-        logger.info("testpreviewWPS...path:" + path);
+        path = DecodeFileFactory.getDecodeService().getDecodeFilePath(path);
+        path = path.replaceAll("//", "/");
+        InputStream in = null;
+        new HashMap();
+        Map<String, Object> previewResult = new HashMap(3);
+        String ext = fileName.substring(fileName.lastIndexOf(46) + 1);
+        long startTime = 0L;
+        long endTime = 0L;
+        Map result;
+        if (YunHomeService.isNotNeedDeal(ext)) {
+            logger.info("testpreviewWPS...10001001");
 
-        if(this.isKkViewSupport(ext)){
-            //1.需要转换的返回kkview的预览url
-            if (fileName.contains("\\")) { // 处理轻量级服务文件名
-                fileName = fileName.substring(fileName.lastIndexOf("\\"));
-                fileName = fileName.replaceAll("\\\\", "");
+            Map<String, String> headerMap = new HashMap();
+            if (userAgent != null) {
+                headerMap.put("USER-AGENT", userAgent);
             }
-            String downloadUrl = this.getDownloadUrl(path);
-            String url = getKkviewUrl(downloadUrl, fileName);
-            result.put(PreviewParams.STATUS.getEnumName(), PreviewParams.URL.getEnumName());
-            result.put(PreviewParams.RESULT.getEnumName(), url);
-            return result;
-        }
 
-        InputStream fileStream = null;
-        try {
+            try {
+                in = this.fileService.download(path, headerMap, new HashMap());
+            } catch (Exception var19) {
+                result = ExceptionUtil.setErrorInfo(var19.getMessage());
+                previewResult.put(PreviewParams.STATUS.getEnumName(), PreviewParams.ERROR.getEnumName());
+                previewResult.put(PreviewParams.RESULT.getEnumName(), result);
+                return previewResult;
+            }
 
-            fileStream = fileService.getInputStream(path);
+            previewResult.put(PreviewParams.STATUS.getEnumName(), PreviewParams.NOT_NEED_CHANGE.getEnumName());
+            previewResult.put(PreviewParams.RESULT.getEnumName(), in);
+            if ("txt".equalsIgnoreCase(ext)) {
+                previewResult.put(PreviewParams.CHARSET.getEnumName(), FileUtil.getFilecharsetNew(in));
+            }
 
-//            fileStream = FileSECUtils.processFileWithSEC(path, fileStream);
+            return previewResult;
+        } else if (this.isPreviewcacheRefresh) {
+            logger.info("testpreviewWPS...10001002");
 
-            //2.不需要转换的直接返回原来流 平台默认的预览
-            result.put(PreviewParams.STATUS.getEnumName(), PreviewParams.NOT_NEED_CHANGE.getEnumName());
-            result.put(PreviewParams.RESULT.getEnumName(), fileStream);
-        } catch (Exception e) {
-            String error = e.getMessage();
-            // 3.失败返回格式
-            result.put(PreviewParams.STATUS.getEnumName(), PreviewParams.ERROR.getEnumName());
-            result.put(PreviewParams.DESCRIPTION.getEnumName(), error);
-        } finally {
-        }
+            return this.getYunPanCovertRs(path, fileName, ext, userAgent, false);
+        } else {
+            if (this.isPreviewcacheEnable) {
+                logger.info("testpreviewWPS...10001003");
+                startTime = System.currentTimeMillis();
+                String previewUrl = path + (!"xls".equalsIgnoreCase(ext) && !"xlsx".equalsIgnoreCase(ext) ? ".pdf" : ".zip");
+                String previewMetaUrl = path + ".meta";
 
-        return result;
+                try {
+                    Map<String, String> headerMap = new HashMap();
+                    headerMap.put("preview", "true");
+                    headerMap.put("preview.url", URLEncoder.encode(previewUrl, StandardCharsets.UTF_8.name()));
+                    headerMap.put("preview.metaUrl", URLEncoder.encode(previewMetaUrl, StandardCharsets.UTF_8.name()));
+                    Map<String, String> resHeader = new HashMap();
+                    in = this.fileService.download(path, headerMap, resHeader);
+                    endTime = System.currentTimeMillis();
+                    if (!"xls".equalsIgnoreCase(ext) && !"xlsx".equalsIgnoreCase(ext)) {
+                        previewResult.put(PreviewParams.STATUS.getEnumName(), PreviewParams.PDF_SUCCESS.getEnumName());
+                        previewResult.put(PreviewParams.RESULT.getEnumName(), in);
+                        logger.info(String.format("从缓存中获取%s所耗费的时间为%s毫秒", fileName, endTime - startTime));
+                        return previewResult;
+                    }
+
+                    String id = (String)resHeader.get("cacheId");
+                    if (StringUtils.isNotEmpty(id)) {
+                        result = ExcelPreviewFactory.getExcelPreview().preview(id, in);
+                        if (result.containsKey("url")) {
+                            String accountId = TAUtil.getAccountId();
+                            String url = (String)result.get("url");
+                            url = url + "?kdcdc=" + accountId;
+                            result.put("url", url);
+                        }
+
+                        previewResult.put(PreviewParams.STATUS.getEnumName(), PreviewParams.XLSX_SUCCESS.getEnumName());
+                        previewResult.put(PreviewParams.RESULT.getEnumName(), result);
+                        logger.info(String.format("从缓存中获取%s所耗费的时间为%s毫秒", fileName, endTime - startTime));
+                        return previewResult;
+                    }
+                } catch (Exception var20) {
+                    logger.error("无法从文件服务器获取预览数据,执行getYunPanCovertRs方法,进行文件预览云盘转换", var20.getMessage());
+                }
+            }
+
+            logger.info("testpreviewWPS...10001004");
+
+            return this.getYunPanCovertRs(path, fileName, ext, userAgent, false);
+        }
     }
 
     /**
@@ -151,11 +198,16 @@ public class DefaultPreviewCusServiceImpl  implements PreviewService {
     @Override
     public Map<String, Object> previewFromCacheWPS(String fileName, String path, String userAgent, InputStream cacheIn, Map<String, String> config) {
         logger.info("testpreviewFromCacheWPS...");
+
         String ext = fileName.substring(fileName.lastIndexOf(46) + 1);
         if (YunHomeService.isNotNeedDeal(ext)) {
+            logger.info("testpreviewFromCacheWPS...10001001");
+
             InputStream in = DecodeFileFactory.getDecodeService().getDecodeFileStream(cacheIn);
             return YunHomeService.getNotNeedTransferRs(in, ext);
         } else {
+            logger.info("testpreviewFromCacheWPS...10001002");
+
             String decodeFilePath = DecodeFileFactory.getDecodeService().getDecodeFilePath(path);
             return this.getYunPanCovertRs(decodeFilePath, fileName, ext, userAgent, true);
         }