diff --git a/src/main/java/com/jiaruiblog/enums/DocType.java b/src/main/java/com/jiaruiblog/enums/DocType.java index 6f03ad3..f23c804 100644 --- a/src/main/java/com/jiaruiblog/enums/DocType.java +++ b/src/main/java/com/jiaruiblog/enums/DocType.java @@ -13,6 +13,7 @@ public enum DocType { PDF, // word文档 DOCX, + PPT, PPTX, XLSX, // txt类的文档 diff --git a/src/main/java/com/jiaruiblog/service/impl/DocReviewServiceImpl.java b/src/main/java/com/jiaruiblog/service/impl/DocReviewServiceImpl.java index 90b53c1..e9fa76f 100644 --- a/src/main/java/com/jiaruiblog/service/impl/DocReviewServiceImpl.java +++ b/src/main/java/com/jiaruiblog/service/impl/DocReviewServiceImpl.java @@ -147,6 +147,7 @@ private void updateDocTxt(FileDocument fileDocument) { switch (suffix) { case "pdf": case "docx": + case "ppt": case "pptx": case "xlsx": case "html": diff --git a/src/main/java/com/jiaruiblog/service/impl/FileServiceImpl.java b/src/main/java/com/jiaruiblog/service/impl/FileServiceImpl.java index 6776258..39761ee 100644 --- a/src/main/java/com/jiaruiblog/service/impl/FileServiceImpl.java +++ b/src/main/java/com/jiaruiblog/service/impl/FileServiceImpl.java @@ -293,6 +293,7 @@ public BaseApiResult documentUpload(MultipartFile file, String userId, String us switch (suffix) { case "pdf": case "docx": + case "ppt": case "pptx": case "xlsx": case "html": @@ -506,6 +507,7 @@ private FileDocument saveFileNew(MultipartFile file, switch (suffix) { case "pdf": case "docx": + case "ppt": case "pptx": case "xlsx": case "html": diff --git a/src/main/java/com/jiaruiblog/task/executor/TaskExecutorFactory.java b/src/main/java/com/jiaruiblog/task/executor/TaskExecutorFactory.java index d455d75..472c96d 100644 --- a/src/main/java/com/jiaruiblog/task/executor/TaskExecutorFactory.java +++ b/src/main/java/com/jiaruiblog/task/executor/TaskExecutorFactory.java @@ -1,6 +1,7 @@ package com.jiaruiblog.task.executor; import com.jiaruiblog.enums.DocType; +import com.jiaruiblog.task.executor.slider.PptxExecutor; import java.util.EnumMap; import java.util.Map; @@ -41,6 +42,7 @@ private static TaskExecutor createTaskExecutor(DocType docType) { case XLSX: taskExecutor = new DocxExecutor(); break; + case PPT: case PPTX: taskExecutor = new PptxExecutor(); break; diff --git a/src/main/java/com/jiaruiblog/task/executor/slider/PptExecutor.java b/src/main/java/com/jiaruiblog/task/executor/slider/PptExecutor.java new file mode 100644 index 0000000..c86e48f --- /dev/null +++ b/src/main/java/com/jiaruiblog/task/executor/slider/PptExecutor.java @@ -0,0 +1,69 @@ +package com.jiaruiblog.task.executor.slider; + +import com.jiaruiblog.entity.FileDocument; +import com.jiaruiblog.entity.FileObj; +import com.jiaruiblog.enums.FileFormatEnum; +import com.jiaruiblog.task.data.TaskData; +import com.jiaruiblog.task.exception.TaskRunException; +import com.jiaruiblog.task.executor.TaskExecutor; +import com.jiaruiblog.util.poi.Converter; +import com.jiaruiblog.util.poi.PPTUtil; +import com.jiaruiblog.util.poi.PptxToPDFConverter; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.UUID; + +import static com.jiaruiblog.task.executor.slider.PptxExecutor.getOutFileStream; + +/** + *
+ * edit at 2024/11/11 6:47 + * + * @author Jarrett Luo + * @version 1.0 + */ +public class PptExecutor extends TaskExecutor { + + @Override + protected void readText(InputStream is, String textFilePath) throws IOException { + + } + + @Override + protected void makeThumb(InputStream is, String picPath) throws IOException { + PPTUtil.extractFirstPPT(is, picPath); + } + + @Override + protected void makePreviewFile(InputStream inStream, TaskData taskData) { + taskData.setPreviewFilePath(UUID.randomUUID() + ".pdf"); + try { + OutputStream outStream = getOutFileStream(taskData.getPreviewFilePath()); + Converter converter = new PptxToPDFConverter(inStream, outStream, true, + true); + converter.convert(); + } catch (Exception e) { + throw new TaskRunException("转换预览文件报错", e); + } + // 解析出来的预览文件存储到文件系统中 + String objId = saveFileToDFS(taskData.getPreviewFilePath(), FileFormatEnum.PDF, "preview-"); + FileDocument fileDocument = taskData.getFileDocument(); + fileDocument.setPreviewFileId(objId); + } + + public void uploadFileToEs(InputStream is, FileDocument fileDocument, TaskData taskData) { + try { + FileObj fileObj = new FileObj(); + fileObj.setId(fileDocument.getMd5()); + fileObj.setName(fileDocument.getName()); + fileObj.setType(fileDocument.getContentType()); + fileObj.readFile(is); + this.upload(fileObj); + + } catch (IOException | TaskRunException e) { + throw new TaskRunException("存入es的过程中报错了", e); + } + } +} diff --git a/src/main/java/com/jiaruiblog/task/executor/PptxExecutor.java b/src/main/java/com/jiaruiblog/task/executor/slider/PptxExecutor.java similarity index 67% rename from src/main/java/com/jiaruiblog/task/executor/PptxExecutor.java rename to src/main/java/com/jiaruiblog/task/executor/slider/PptxExecutor.java index ff4683a..a134d0d 100644 --- a/src/main/java/com/jiaruiblog/task/executor/PptxExecutor.java +++ b/src/main/java/com/jiaruiblog/task/executor/slider/PptxExecutor.java @@ -1,10 +1,13 @@ -package com.jiaruiblog.task.executor; +package com.jiaruiblog.task.executor.slider; import com.jiaruiblog.entity.FileDocument; +import com.jiaruiblog.entity.FileObj; import com.jiaruiblog.enums.FileFormatEnum; import com.jiaruiblog.task.data.TaskData; import com.jiaruiblog.task.exception.TaskRunException; +import com.jiaruiblog.task.executor.DocxExecutor; import com.jiaruiblog.util.poi.Converter; +import com.jiaruiblog.util.poi.PPTUtil; import com.jiaruiblog.util.poi.PptxToPDFConverter; import lombok.extern.slf4j.Slf4j; @@ -19,7 +22,12 @@ * @Version 1.0 **/ @Slf4j -public class PptxExecutor extends DocxExecutor{ +public class PptxExecutor extends DocxExecutor { + + @Override + protected void makeThumb(InputStream is, String picPath) { + PPTUtil.extractFirstPPTx(is, picPath); + } @Override protected void makePreviewFile(InputStream inStream, TaskData taskData) { @@ -56,4 +64,18 @@ protected static OutputStream getOutFileStream(String outputFilePath) throws IOE return new FileOutputStream(outFile); } + public void uploadFileToEs(InputStream is, FileDocument fileDocument, TaskData taskData) { + try { + FileObj fileObj = new FileObj(); + fileObj.setId(fileDocument.getMd5()); + fileObj.setName(fileDocument.getName()); + fileObj.setType(fileDocument.getContentType()); + fileObj.readFile(is); + this.upload(fileObj); + + } catch (IOException | TaskRunException e) { + throw new TaskRunException("存入es的过程中报错了", e); + } + } + } diff --git a/src/main/java/com/jiaruiblog/util/poi/Converter.java b/src/main/java/com/jiaruiblog/util/poi/Converter.java index 95e6e9a..b76ae42 100644 --- a/src/main/java/com/jiaruiblog/util/poi/Converter.java +++ b/src/main/java/com/jiaruiblog/util/poi/Converter.java @@ -13,12 +13,6 @@ **/ public abstract class Converter { - private final String LOADING_FORMAT = "\nLoading stream\n\n"; - private final String PROCESSING_FORMAT = "Load completed in %1$dms, now converting...\n\n"; - private final String SAVING_FORMAT = "Conversion took %1$dms.\n\nTotal: %2$dms\n"; - - private long startTime; - private long startOfProcessTime; protected InputStream inStream; protected OutputStream outStream; @@ -36,62 +30,4 @@ protected Converter(InputStream inStream, OutputStream outStream, public abstract void convert() throws Exception; - private void startTime(){ - startTime = System.currentTimeMillis(); - startOfProcessTime = startTime; - } - - protected void loading(){ - sendToOutputOrNot(String.format(LOADING_FORMAT)); - startTime(); - } - - protected void processing(){ - long currentTime = System.currentTimeMillis(); - long prevProcessTook = currentTime - startOfProcessTime; - - sendToOutputOrNot(String.format(PROCESSING_FORMAT, prevProcessTook)); - - startOfProcessTime = System.currentTimeMillis(); - - } - - protected void finished(){ - long currentTime = System.currentTimeMillis(); - long timeTaken = currentTime - startTime; - long prevProcessTook = currentTime - startOfProcessTime; - - startOfProcessTime = System.currentTimeMillis(); - - if(closeStreamsWhenComplete){ - try { - inStream.close(); - outStream.close(); - } catch (IOException e) { - //Nothing done - } - } - - sendToOutputOrNot(String.format(SAVING_FORMAT, prevProcessTook, timeTaken)); - } - - - private void sendToOutputOrNot(String toBePrinted){ - if(showOutputMessages){ - actuallySendToOutput(toBePrinted); - } - } - - - protected void actuallySendToOutput(String toBePrinted){ - System.out.println(toBePrinted); - } - - - - - - - - } diff --git a/src/main/java/com/jiaruiblog/util/poi/PPTUtil.java b/src/main/java/com/jiaruiblog/util/poi/PPTUtil.java index 9079320..11beb5a 100644 --- a/src/main/java/com/jiaruiblog/util/poi/PPTUtil.java +++ b/src/main/java/com/jiaruiblog/util/poi/PPTUtil.java @@ -16,26 +16,21 @@ */ public class PPTUtil { - public static void main(String[] args) { - PPTUtil pptUtil = new PPTUtil(); - pptUtil.transPPTXToPic(); - } - /** * @Author: RedRush - * @Date: 2022/6/29 22:32 + * @Date: 2022/6/29 22:32 * @description: ppt/pptx 转换为图片 */ - public void transPPTXToPic(){ + public void transPPTXToPic() { String root = "C:\\project\\java\\test-files\\"; String inPath = "C:\\project\\java\\test-files\\《草船借箭》ppt课件[1].ppt2.ppt"; String outPath = "C:\\project\\java\\test-files\\《草船借箭》ppt课件[1]_01.pdf"; // String fileName = "Test.pptx"; String fileName = "《草船借箭》ppt课件[1].ppt2.ppt"; try { - if(fileName.toUpperCase().endsWith(".PPTX")){ + if (fileName.toUpperCase().endsWith(".PPTX")) { transPPTXToPic(root, fileName); - }else if(fileName.toUpperCase().endsWith(".PPT")){ + } else if (fileName.toUpperCase().endsWith(".PPT")) { transPPTToPic(root, fileName); } } catch (Exception e) { @@ -43,8 +38,146 @@ public void transPPTXToPic(){ } } + public static void extractFirstPPT(String pptName, String pngPath) { + // 读取ppt + FileInputStream fis = null; + try { + fis = new FileInputStream(pptName); + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } + extractFirstPPT(fis, pngPath); + } + + + public static void extractFirstPPT(InputStream fis, String pngPath) { + try { + // 获取系统可用字体 + GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment(); + String[] fontNames = e.getAvailableFontFamilyNames(); + + HSLFSlideShow ppt = new HSLFSlideShow(fis); + + /* + * 解析PPT基本内容 + * */ + Dimension sheet = ppt.getPageSize(); + int width = sheet.width, height = sheet.height; + List