Browse Source

预览增删改查代码提交

pans 8 months ago
parent
commit
89febcccd1

+ 38 - 0
snowy-plugin/snowy-plugin-disk/snowy-plugin-disk-func/pom.xml

@@ -221,6 +221,11 @@
             <version>2.0.1</version>
             <scope>runtime</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-exec</artifactId>
+            <version>1.3</version>
+        </dependency>
 
         <!-- Apache POI -->
 <!--        <dependency>-->
@@ -233,6 +238,11 @@
 <!--            <artifactId>poi-ooxml</artifactId>-->
 <!--            <version>5.2.3</version>-->
 <!--        </dependency>-->
+        <dependency><!--jar破解工具-->
+            <groupId>org.javassist</groupId>
+            <artifactId>javassist</artifactId>
+            <version>3.20.0-GA</version>
+        </dependency>
 
         <!-- Apache POI核心依赖 -->
         <dependency>
@@ -258,7 +268,35 @@
             <version>2.13.0</version>
         </dependency>
 
+        <dependency>
+            <groupId>com.aspose</groupId>
+            <artifactId>aspose-words-jdk16</artifactId>
+            <version>15.8.0</version>
+            <scope>system</scope>
+            <systemPath>${project.basedir}/src/main/resources/lib/aspose-words-15.8.0.jar</systemPath>
+        </dependency>
+        <dependency>
+            <groupId>com.aspose</groupId>
+            <artifactId>aspose-cells</artifactId>
+            <version>8.5.2</version>
+            <scope>system</scope>
+            <systemPath>${project.basedir}/src/main/resources/lib/aspose-cells-8.5.2.jar</systemPath>
+        </dependency>
+        <dependency>
+            <groupId>com.aspose</groupId>
+            <artifactId>aspose-slides</artifactId>
+            <version>15.9.0</version>
+            <scope>system</scope>
+            <systemPath>${project.basedir}/src/main/resources/lib/aspose-slides-15.9.0.jar</systemPath>
+        </dependency>
 
+        <dependency>
+            <groupId>com.aspose</groupId>
+            <artifactId>aspose-pdf</artifactId>
+            <version>22.4.0</version>
+            <scope>system</scope>
+            <systemPath>${project.basedir}/src/main/resources/lib/aspose-pdf-22.4.jar</systemPath>
+        </dependency>
     </dependencies>
 
     <repositories>

+ 4 - 6
snowy-plugin/snowy-plugin-disk/snowy-plugin-disk-func/src/main/java/vip/xiaonuo/disk/service/impl/TranscodingServiceImpl.java

@@ -172,6 +172,7 @@ public class TranscodingServiceImpl implements TranscodingService {
             // pdf doc docx ppt pptx xls xlsx
             // doc转docx,ppt转pptx,xls转xlsx 所有均转pdf
             // 判断目标格式
+
             try {
                 if ("pdf".equals(transcodingResourceReqDTO.getFormat())) {
                     pdfUtils.convertToPdf(fileBean.getFileUrl(), fileBean.getFileId() + "." + transcodingResourceReqDTO.getFormat());
@@ -179,17 +180,14 @@ public class TranscodingServiceImpl implements TranscodingService {
                     officeConverter.convertAndUpload(fileBean.getFileUrl(), fileBean.getFileId() + "." + transcodingResourceReqDTO.getFormat());
                 }
                 //根据文件id修改预览地址
+                ResourceFile newFile = new ResourceFile();
+                newFile.setFileId(fileBean.getFileId());
                 if(videoList.contains(userFile.getExtendName())||wordList.contains(userFile.getExtendName())){
-                    ResourceFile newFile = new ResourceFile();
-                    newFile.setFileId(fileBean.getFileId());
                     newFile.setPreviewFileUrl("converted/" + fileBean.getFileId() + "." + transcodingResourceReqDTO.getFormat());
-                    resourceFileMapper.updateById(newFile);
                 }else{
-                    ResourceFile newFile = new ResourceFile();
-                    newFile.setFileId(fileBean.getFileId());
                     newFile.setPreviewFileUrl(fileBean.getFileUrl());
-                    resourceFileMapper.updateById(newFile);
                 }
+                resourceFileMapper.updateById(newFile);
             } catch (Exception ex) {
                 log.error("格式转换:"+ex.getMessage());
                 return CommonResult.error("格式转换失败");

+ 218 - 0
snowy-plugin/snowy-plugin-disk/snowy-plugin-disk-func/src/main/java/vip/xiaonuo/disk/util/FileToPdfUtils.java

@@ -0,0 +1,218 @@
+package vip.xiaonuo.disk.util;
+
+import com.aspose.cells.Workbook;
+import com.aspose.slides.Presentation;
+import com.aspose.slides.SaveFormat;
+import com.aspose.words.Document;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+
+
+public class FileToPdfUtils {
+/**
+ * 文件转换
+ *
+ * @param source:源文件地址 如:C://test/test.doc,target:转换后文件路径  如 C://test/pdf
+ * @return
+ */
+public static String officeToPdf(String source, String target) {
+    File file = new File(source);
+    // 文件名字
+    String fileName = file.getName();
+    //office文档转pdf
+    String fileExt = source.substring(source.lastIndexOf(".") + 1);
+    if ("doc".equals(fileExt) || "docx".equals(fileExt)) {
+        doc2pdf(source, target, fileExt);
+    }
+    if ("xls".equals(fileExt) || "xlsx".equals(fileExt)) {
+        excel2pdf(source, target, fileExt);
+    }
+    if ("ppt".equals(fileExt) || "pptx".equals(fileExt)) {
+        ppt2pdf(source, target, fileExt);
+    }
+
+    if ("doc,docx,xls,xlsx,ppt,pptx".indexOf(fileExt) > 0) {
+        return target +  File.separator + (fileName.replace(fileExt, "pdf"));
+    }
+    return null;
+}
+
+/**
+ * @description: 验证ExcelLicense
+ * @params:
+ * @return:
+ * @author: com.liuhm
+ * @Date: 2019/10/10 13:40
+ */
+public static boolean getExcelLicense() {
+    boolean result = false;
+    try {
+        //  license.xml应放在..\WebRoot\WEB-INF\classes路径下
+        InputStream is = FileToPdfUtils.class.getClassLoader().getResourceAsStream("license.xml");
+        com.aspose.cells.License aposeLic = new com.aspose.cells.License();
+        aposeLic.setLicense(is);
+        result = true;
+    } catch (Exception e) {
+        e.printStackTrace();
+    }
+    return result;
+}
+
+/**
+ * @description: 验证PPTtlLicense
+ * @params:
+ * @return:
+ * @author: com.liuhm
+ * @Date: 2019/10/10 13:40
+ */
+public static boolean getPPTLicense() {
+    boolean result = false;
+    try {
+        //  license.xml应放在..\WebRoot\WEB-INF\classes路径下
+        InputStream is = FileToPdfUtils.class.getClassLoader().getResourceAsStream("license.xml");
+        com.aspose.slides.License aposeLic = new com.aspose.slides.License();
+        aposeLic.setLicense(is);
+        result = true;
+    } catch (Exception e) {
+        e.printStackTrace();
+    }
+    return result;
+}
+
+/**
+ * @description: 验证License
+ * @params:
+ * @return:
+ * @author: com.liuhm
+ * @Date: 2019/10/10 13:40
+ */
+public static boolean getDocLicense() {
+    boolean result = false;
+    try {
+        //  license.xml应放在..\WebRoot\WEB-INF\classes路径下
+        InputStream is = FileToPdfUtils.class.getClassLoader().getResourceAsStream("license.xml");
+        com.aspose.words.License aposeLic = new com.aspose.words.License();
+        aposeLic.setLicense(is);
+        result = true;
+    } catch (Exception e) {
+        e.printStackTrace();
+    }
+    return result;
+}
+
+/**
+ * @description: excel转pdf
+ * @params: source:源文件地址,target:转换后文件路径,fileExt:后缀
+ * @return:
+ * @author: com.liuhm
+ * @Date: 2019/10/10 13:41
+ */
+public static void excel2pdf(String source, String target, String fileExt) {
+    // 验证License 若不验证则转化出的pdf文档会有水印产生
+    if (!getExcelLicense()) {
+        return;
+    }
+    try {
+        // 原始excel路径
+        Workbook wb = new Workbook(source);
+        //验证路径
+        try {
+            if (!(new File(target).isDirectory())) {
+                new File(target).mkdirs();
+            }
+        } catch (SecurityException e) {
+            e.printStackTrace();
+        }
+        // 文件名字
+        String fileName = new File(source).getName();
+        // 输出路径
+        File pdfFile = new File(target +  File.separator + (fileName.replace(fileExt, "pdf")));
+        FileOutputStream fileOS = new FileOutputStream(pdfFile);
+        wb.save(fileOS, com.aspose.cells.SaveFormat.PDF);
+    } catch (Exception e) {
+        e.printStackTrace();
+    }
+}
+
+/**
+ * @description: ppt转pdf
+ * @params: source:源文件地址,target:转换后文件路径,fileExt:后缀
+ * @return:
+ * @author: com.liuhm
+ * @Date: 2019/10/10 13:46
+ */
+public static void ppt2pdf(String source, String target, String fileExt) {
+    // 验证License 若不验证则转化出的pdf文档会有水印产生
+    if (!getPPTLicense()) {
+        return;
+    }
+    try {
+        //验证路径
+        try {
+            if (!(new File(target).isDirectory())) {
+                new File(target).mkdirs();
+            }
+        } catch (SecurityException e) {
+            e.printStackTrace();
+        }
+        // 文件名字
+        String fileName = new File(source).getName();
+        //新建一个空白pdf文档
+        File file = new File(target +  File.separator + (fileName.replace(fileExt, "pdf")));
+        //输入pdf路径
+        Presentation pres = new Presentation(source);
+        FileOutputStream fileOS = new FileOutputStream(file);
+        pres.save(fileOS, SaveFormat.Pdf);
+        fileOS.close();
+    } catch (Exception e) {
+        e.printStackTrace();
+    }
+}
+
+/**
+ * @description: doc转pdf
+ * @params: source:源文件地址,target:转换后文件路径,fileExt:后缀
+ * @return:
+ * @author: com.liuhm
+ * @Date: 2019/10/10 13:46
+ */
+public static void doc2pdf(String source, String target, String fileExt) {
+    // 验证License 若不验证则转化出的pdf文档会有水印产生
+    if (!getDocLicense()) {
+        return;
+    }
+    try {
+        //新建一个空白pdf文档
+        try {
+            if (!(new File(target).isDirectory())) {
+                new File(target).mkdirs();
+            }
+        } catch (SecurityException e) {
+            e.printStackTrace();
+        }
+        // 文件名字
+
+        String fileName = new File(source).getName();
+        // 输出路径
+        File file = new File(target +  File.separator + (fileName.replace(fileExt, "pdf")));
+        FileOutputStream os = new FileOutputStream(file);
+        Document doc = new Document(source);
+        doc.save(os, com.aspose.words.SaveFormat.PDF);
+    } catch (Exception e) {
+        e.printStackTrace();
+    }
+}
+
+
+    public static void main(String[] args) {
+        FileToPdfUtils.officeToPdf("C:\\Users\\Lenovo\\AppData\\Local\\Temp\\minio-converter\\10380841-1caa-44d8-be4e-0ca147fa0a7a_4126f65c6160e8daa287c6309e06a020.docx", "C:\\Users\\Lenovo\\AppData\\Local\\Temp\\minio-converter\\");
+    }
+
+
+
+
+
+}
+

+ 61 - 52
snowy-plugin/snowy-plugin-disk/snowy-plugin-disk-func/src/main/java/vip/xiaonuo/disk/util/PdfUtils.java

@@ -5,13 +5,9 @@ import io.minio.MinioClient;
 import io.minio.UploadObjectArgs;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
-import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Component;
 
 import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.nio.file.Files;
 import java.util.UUID;
 
 @Component
@@ -56,16 +52,16 @@ public class PdfUtils {
     }
 
 //    @Async
-    public void convertToPdf(String objectName, String fileId) throws Exception {
+    public String  convertToPdf(String objectName, String fileId) throws Exception {
         long start = System.currentTimeMillis();
         File tempFile = null;
-
+        String outputPath=null;
         try {
             // 1. 下载源文件
             tempFile = downloadFile(objectName);
 
             // 2. 执行转换
-            String outputPath = convertToPdfLocal(tempFile.getAbsolutePath(), "convert");
+             outputPath = convertToPdfLocal(tempFile.getName(),tempFile.getAbsolutePath(), "convert");
 
             // 3. 上传转换结果
             uploadToMinio(outputPath, fileId);
@@ -74,9 +70,11 @@ public class PdfUtils {
             if (tempFile != null && tempFile.exists()) {
                 tempFile.delete();
             }
-            cleanTempDir();
+            cleanTempDir(tempFile.getAbsolutePath(),outputPath);
         }
         System.out.println("总耗时:" + (System.currentTimeMillis() - start) + "ms");
+
+        return  outputPath;
     }
 
     private File downloadFile(String objectName) throws Exception {
@@ -105,46 +103,53 @@ public class PdfUtils {
         }
     }
 
-    private String convertToPdfLocal(String filePath, String targetFolder) {
-        String osName = System.getProperty("os.name").toLowerCase();
-        String outputPath = tempDir + "converted_" + System.currentTimeMillis() + ".pdf";
+    private String convertToPdfLocal(String fileName,String filePath, String targetFolder) {
+    //    String osName = System.getProperty("os.name").toLowerCase();
+        fileName=fileName.substring(0, fileName.lastIndexOf("."));
+        String outputPath = tempDir  + fileName + ".pdf";
         // 在执行转换前确保输出目录存在
-        ensureDirectoryExists(outputPath);
-        try {
-            ProcessBuilder processBuilder = new ProcessBuilder();
-            if (osName.contains("windows")) {
-                processBuilder.command(
-                        "cmd.exe", "/c",
-                        "soffice", "--headless", "--convert-to", "pdf",
-                        filePath, "--outdir", tempDir);
-            } else {
-                processBuilder.command(
-                        "soffice", "--headless", "--convert-to", "pdf:writer_pdf_Export",
-                        filePath, "--outdir", tempDir);
-            }
+       // ensureDirectoryExists(outputPath);
+      //  try {
+//            ProcessBuilder processBuilder = new ProcessBuilder();
+//            if (osName.contains("windows")) {
+//                processBuilder.command(
+//                        "cmd.exe", "/c",
+//                        "soffice", "--headless", "--convert-to", "pdf",
+//                        filePath, "--outdir", tempDir);
+//            } else {
+//                processBuilder.command(
+//                        "soffice", "--headless", "--convert-to", "pdf:writer_pdf_Export",
+//                        filePath, "--outdir", tempDir);
+//            }
 
-            Process process = processBuilder.start();
-            int exitCode = process.waitFor();
-            if (exitCode != 0) {
-                throw new RuntimeException("转换失败,退出码:" + exitCode);
-            }
+            FileToPdfUtils.officeToPdf(filePath, tempDir);
 
 
-            // 查找生成的PDF文件
-            File outputDir = new File(tempDir);
-            File[] files = outputDir.listFiles((dir, name) ->
-                    name.toLowerCase().endsWith(".pdf") && !name.equals(filePath));
 
-            if (files != null && files.length > 0) {
-                File pdfFile = files[0];
-                Files.move(pdfFile.toPath(), new File(outputPath).toPath());
-                return outputPath;
-            }
-            throw new FileNotFoundException("未找到生成的PDF文件");
+//            Process process = processBuilder.start();
+//            int exitCode = process.waitFor();
+//            if (exitCode != 0) {
+//                throw new RuntimeException("转换失败,退出码:" + exitCode);
+//            }
+//
+//
+//            // 查找生成的PDF文件
+//            File outputDir = new File(tempDir);
+//            File[] files = outputDir.listFiles((dir, name) ->
+//                    name.toLowerCase().endsWith(".pdf") && !name.equals(filePath));
+//
+//            if (files != null && files.length > 0) {
+//                File pdfFile = files[0];
+//                Files.move(pdfFile.toPath(), new File(outputPath).toPath());
+//                return outputPath;
+//            }
+//            throw new FileNotFoundException("未找到生成的PDF文件");
 
-        } catch (IOException | InterruptedException e) {
-            throw new RuntimeException("转换过程中发生错误", e);
-        }
+//        } catch (IOException | InterruptedException e) {
+//            throw new RuntimeException("转换过程中发生错误", e);
+//        }
+
+        return outputPath;
     }
 
     private void uploadToMinio(String filePath, String fileId) throws Exception {
@@ -158,16 +163,20 @@ public class PdfUtils {
                         .build());
     }
 
-    private void cleanTempDir() {
-        File[] files = new File(tempDir).listFiles();
-        if (files != null) {
-            for (File file : files) {
-                if (file.getName().startsWith("converted_") ||
-                        file.getName().startsWith("temp_")) {
-                    file.delete();
-                }
-            }
-        }
+    private void cleanTempDir(String filePath,String outputPath) {
+//        File[] files = new File(tempDir).listFiles();
+//        if (files != null) {
+//            for (File file : files) {
+//                if (file.getName().startsWith("converted_") ||
+//                        file.getName().startsWith("temp_")|| file.getName().startsWith("Temp")) {
+//                    file.delete();
+//                }
+//            }
+//        }
+        File file = new File(filePath);
+        file.delete();
+        File pdffile = new File(outputPath);
+        pdffile.delete();
     }
 
 

+ 13 - 0
snowy-plugin/snowy-plugin-disk/snowy-plugin-disk-func/src/main/resources/license.xml

@@ -0,0 +1,13 @@
+<License>
+    <Data>
+        <Products>
+            <Product>Aspose.Total for Java</Product>
+            <Product>Aspose.Words for Java</Product>
+        </Products>
+        <EditionType>Enterprise</EditionType>
+        <SubscriptionExpiry>20991231</SubscriptionExpiry>
+        <LicenseExpiry>20991231</LicenseExpiry>
+        <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
+    </Data>
+    <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
+</License>