瀏覽代碼

不能预览的文件类型转到新页面

陈长荣 2 月之前
父節點
當前提交
608618cf51

+ 15 - 0
src/main/java/com/github/jfcloud/excel/editor/docdeal/constant/DocumentConstants.java

@@ -1,5 +1,7 @@
 package com.github.jfcloud.excel.editor.docdeal.constant;
 
+import org.springframework.http.MediaType;
+
 import java.time.Duration;
 import java.util.Arrays;
 import java.util.List;
@@ -52,4 +54,17 @@ public class DocumentConstants {
     public static final Duration CACHE_DURATION = Duration.ofDays(1);
 
     public static final String HASH_KEY = "lezhixing";
+
+    /**
+     * word 格式
+     */
+    public static final List<MediaType> DOC_MEDIA_TYPES = Arrays.asList(MediaType.APPLICATION_PDF,
+            new MediaType("application", "msword"),
+            new MediaType("application", "vnd.openxmlformats-officedocument.wordprocessingml.document"));
+
+    /**
+     * excel 格式
+     */
+    public static final List<MediaType> EXCEL_MEDIA_TYPES = Arrays.asList(new MediaType("application", "vnd.ms-excel"),
+            new MediaType("application", "vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
 }

+ 35 - 21
src/main/java/com/github/jfcloud/excel/editor/docdeal/controller/PreviewController.java

@@ -55,37 +55,51 @@ public class PreviewController {
         vo.setEdit(false);
         log.info("==> 查看文档 vo: {}", vo);
         Pair<Document, DocumentEditParam> pair = docInfo(vo);
-        model.addAttribute("name", vo.getName());
 
+        String filename = vo.getName();
         String bucketName = pair.getKey().getStorage();
-        if (!ossTemplate.exist(bucketName, vo.getName())) {
+        String downloadUrl = "/download?name=" + filename + "&bucket=" + bucketName;
+
+        model.addAttribute("name", filename);
+        model.addAttribute("downloadUrl", downloadUrl);
+        if (!ossTemplate.exist(bucketName, filename)) {
             return "/notFound";
         }
 
-        Optional<MediaType> opt = MediaTypeFactory.getMediaType(vo.getName());
-        if (opt.isPresent()) {
-            MediaType mediaType = opt.get();
-            //图片
-            if (mediaType.getType().equals("image")) {
-                model.addAttribute("src", "/download?name=" + vo.getName() + "&bucket=" + bucketName);
-                return "/viewerImg";
-            }
-            //文本
-            if (mediaType.getType().equals("text")) {
-                S3ObjectInputStream inputStream = ossTemplate.getObject(bucketName, vo.getName()).getObjectContent();
-                model.addAttribute("content", IoUtil.read(inputStream, StandardCharsets.UTF_8));
-                return "/viewerTxt";
-            }
+        Optional<MediaType> opt = MediaTypeFactory.getMediaType(filename);
+        if (!opt.isPresent()) {
+            return "/cannotPreview";
         }
+        MediaType mediaType = opt.get();
 
-        model.addAttribute("document", pair.getKey());
-        model.addAttribute("documentEditParam", pair.getValue());
+        //图片
+        if (mediaType.getType().equals("image")) {
+            return "/viewerImg";
+        }
 
-        String suffix = FileNameUtil.getSuffix(vo.getName());
-        if (suffix.equalsIgnoreCase("xls") || suffix.equalsIgnoreCase("xlsx")) {
+        //文本
+        if (mediaType.getType().equals("text")) {
+            S3ObjectInputStream inputStream = ossTemplate.getObject(bucketName, filename).getObjectContent();
+            model.addAttribute("content", IoUtil.read(inputStream, StandardCharsets.UTF_8));
+            return "/viewerTxt";
+        }
+
+        //表格
+        if (DocumentConstants.EXCEL_MEDIA_TYPES.contains(mediaType)) {
+            model.addAttribute("document", pair.getKey());
+            model.addAttribute("documentEditParam", pair.getValue());
             return "/viewerExcel";
         }
-        return "/viewer";
+
+        //文档
+        if (DocumentConstants.DOC_MEDIA_TYPES.contains(mediaType)) {
+            model.addAttribute("document", pair.getKey());
+            model.addAttribute("documentEditParam", pair.getValue());
+            return "/viewer";
+        }
+
+        //其他文件无法预览
+        return "/cannotPreview";
     }
 
     @GetMapping("/edit")

+ 69 - 0
src/main/resources/templates/cannotPreview.html

@@ -0,0 +1,69 @@
+<!DOCTYPE html>
+<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
+<head>
+  <meta charset="UTF-8">
+  <meta content="width=device-width, initial-scale=1.0" name="viewport">
+  <title>文件无法预览</title>
+  <style>
+      body {
+          font-family: 'Arial', sans-serif;
+          background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
+          height: 100vh;
+          margin: 0;
+          display: flex;
+          justify-content: center;
+          align-items: center;
+          text-align: center;
+      }
+
+      .container {
+          background-color: white;
+          padding: 2rem 3rem;
+          border-radius: 15px;
+          box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
+          max-width: 500px;
+      }
+
+      h1 {
+          color: #e74c3c;
+          margin-bottom: 1.5rem;
+      }
+
+      p {
+          font-size: 1.1rem;
+          color: #64748b;
+          margin: 0 0 25px 0;
+          line-height: 1.6;
+      }
+
+      .icon {
+          font-size: 5rem;
+          color: #e74c3c;
+          margin-bottom: 1rem;
+      }
+
+      .btn {
+          display: inline-block;
+          background: #3b82f6;
+          color: white;
+          padding: 12px 24px;
+          border-radius: 6px;
+          text-decoration: none;
+          font-weight: 500;
+          transition: background 0.2s;
+      }
+
+      .btn:hover {
+          background: #2563eb;
+      }
+  </style>
+</head>
+<body>
+<div class="container">
+  <div class="icon">⚠️</div>
+  <h1>无法预览此文件</h1>
+  <p>抱歉,我们无法在浏览器中预览此文件类型。您可以下载该文件并在本地打开查看。</p>
+  <a class="btn" th:href="${downloadUrl}">下载文件</a>
+</div>
+</body>
+</html>

+ 1 - 1
src/main/resources/templates/viewerImg.html

@@ -42,7 +42,7 @@
 <body>
 <div class="container">
   <div class="image-container">
-    <img class="image-display" th:src="${src}">
+    <img class="image-display" th:src="${downloadUrl}">
   </div>
 </div>
 </body>