From 69d407729c47fc0a603fb29d16698b1b9e92329d Mon Sep 17 00:00:00 2001 From: Jarrett Date: Mon, 11 Nov 2024 07:49:03 +0800 Subject: [PATCH] add: add slider executor --- .../java/com/jiaruiblog/enums/DocType.java | 1 + .../service/impl/DocReviewServiceImpl.java | 1 + .../service/impl/FileServiceImpl.java | 2 + .../task/executor/TaskExecutorFactory.java | 2 + .../task/executor/slider/PptExecutor.java | 69 +++++++ .../executor/{ => slider}/PptxExecutor.java | 26 ++- .../com/jiaruiblog/util/poi/Converter.java | 64 ------ .../java/com/jiaruiblog/util/poi/PPTUtil.java | 195 +++++++++++++++--- .../util/poi/PptToPDFConverter.java | 5 - 9 files changed, 263 insertions(+), 102 deletions(-) create mode 100644 src/main/java/com/jiaruiblog/task/executor/slider/PptExecutor.java rename src/main/java/com/jiaruiblog/task/executor/{ => slider}/PptxExecutor.java (67%) 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 pages = ppt.getSlides(); + + + BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + Graphics2D graphics = img.createGraphics(); + + HSLFSlide slide = pages.get(0); + + // 设置字体, 解决中文乱码 + for (HSLFShape shape : slide.getShapes()) { + if (shape instanceof HSLFTextShape) { + HSLFTextShape textShape = (HSLFTextShape) shape; + + for (HSLFTextParagraph textParagraph : textShape.getTextParagraphs()) { + for (HSLFTextRun textRun : textParagraph.getTextRuns()) { + textRun.setFontFamily("宋体"); + } + } + } + + } + + // 清空画板 + graphics.setPaint(Color.white); + graphics.fill(new Rectangle2D.Float(0, 0, width, height)); + slide.draw(graphics); + // 输出为图片 + File f = new File(pngPath); + FileOutputStream fos = new FileOutputStream(f); + javax.imageio.ImageIO.write(img, "PNG", fos); + fos.close(); + + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (fis != null) { + try { + fis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + public static void extractFirstPPTx(String pptName, String pngPath) { + // 读取ppt + FileInputStream fis = null; + try { + fis = new FileInputStream(pptName); + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } + extractFirstPPTx(fis, pngPath); + } + + public static void extractFirstPPTx(InputStream fis, String pngPath) { + try { + // 获取系统可用字体 + GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment(); + String[] fontNames = e.getAvailableFontFamilyNames(); + + // 读取ppt + XMLSlideShow ppt = new XMLSlideShow(fis); + + /* + * 解析PPT基本内容 + * */ + Dimension sheet = ppt.getPageSize(); + int width = sheet.width, height = sheet.height; + + BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + Graphics2D graphics = img.createGraphics(); + int i = 1; + XSLFSlide slide = ppt.getSlides().get(0); + + // 设置字体, 解决中文乱码 + for (XSLFShape shape : slide.getShapes()) { + if (shape instanceof XSLFTextShape) { + XSLFTextShape textShape = (XSLFTextShape) shape; + + for (XSLFTextParagraph textParagraph : textShape.getTextParagraphs()) { + for (XSLFTextRun textRun : textParagraph.getTextRuns()) { + textRun.setFontFamily("宋体"); + } + } + } + + } + + // 清空画板 + graphics.setPaint(Color.white); + graphics.fill(new Rectangle2D.Float(0, 0, width, height)); + slide.draw(graphics); + // 输出为图片 + File f = new File(pngPath); + FileOutputStream fos = new FileOutputStream(f); + javax.imageio.ImageIO.write(img, "PNG", fos); + fos.close(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (fis != null) { + try { + fis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + // PPT输出为图片 hslf解析 - private void transPPTToPic(String filePath, String fileName){ + private void transPPTToPic(String filePath, String fileName) { // 生成输出 String outRoot = filePath + fileName.substring(0, fileName.indexOf('.')) + File.separator; System.err.printf("图片输出路径为:%s\n", outRoot); @@ -55,10 +188,10 @@ private void transPPTToPic(String filePath, String fileName){ System.err.printf("PPT读取路径为:%s\n", pptName); FileInputStream fis = null; - try{ + try { // 获取系统可用字体 GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment(); - String[] fontNames = e.getAvailableFontFamilyNames(); + String[] fontNames = e.getAvailableFontFamilyNames(); // 读取ppt fis = new FileInputStream(new File(pptName)); @@ -74,11 +207,11 @@ private void transPPTToPic(String filePath, String fileName){ System.err.printf("ppt基本信息: 共%s页,尺寸: %s , %s", pages.size(), width, height); - BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); - Graphics2D graphics = img.createGraphics(); + BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + Graphics2D graphics = img.createGraphics(); int i = 1; // 逐页遍历 - for(HSLFSlide slide : pages){ + for (HSLFSlide slide : pages) { // 清空画板 graphics.setPaint(Color.white); @@ -91,10 +224,10 @@ private void transPPTToPic(String filePath, String fileName){ javax.imageio.ImageIO.write(img, "PNG", fos); fos.close(); } - }catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); - }finally { - if (fis != null){ + } finally { + if (fis != null) { try { fis.close(); } catch (IOException e) { @@ -105,7 +238,7 @@ private void transPPTToPic(String filePath, String fileName){ } // PPTX输出为图片 xmls包解析 - private void transPPTXToPic(String filePath, String fileName){ + private void transPPTXToPic(String filePath, String fileName) { // 生成输出 String outRoot = filePath + fileName.substring(0, fileName.indexOf('.')) + File.separator; System.err.printf("图片输出路径为:%s\n", outRoot); @@ -116,10 +249,10 @@ private void transPPTXToPic(String filePath, String fileName){ System.err.printf("PPT读取路径为:%s\n", pptName); FileInputStream fis = null; - try{ + try { // 获取系统可用字体 GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment(); - String[] fontNames = e.getAvailableFontFamilyNames(); + String[] fontNames = e.getAvailableFontFamilyNames(); // 读取ppt fis = new FileInputStream(new File(pptName)); @@ -127,19 +260,19 @@ private void transPPTXToPic(String filePath, String fileName){ /* - * 解析PPT基本内容 - * */ + * 解析PPT基本内容 + * */ Dimension sheet = ppt.getPageSize(); int width = sheet.width, height = sheet.height; int count = ppt.getSlides().size(); System.err.printf("ppt基本信息: 共%s页,尺寸: %s , %s", count, width, height); - BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); - Graphics2D graphics = img.createGraphics(); + BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + Graphics2D graphics = img.createGraphics(); int i = 1; // 逐页遍历 - for(XSLFSlide shape : ppt.getSlides()){ + for (XSLFSlide shape : ppt.getSlides()) { // 清空画板 graphics.setPaint(Color.white); @@ -152,11 +285,11 @@ private void transPPTXToPic(String filePath, String fileName){ javax.imageio.ImageIO.write(img, "PNG", fos); fos.close(); } - }catch (Exception e){ + } catch (Exception e) { System.err.println("======ppt转换异常"); e.printStackTrace(); - }finally { - if (fis != null){ + } finally { + if (fis != null) { try { fis.close(); } catch (IOException e) { @@ -168,9 +301,9 @@ private void transPPTXToPic(String filePath, String fileName){ } // 生成文件夹 - private void mkdir(String path){ + private void mkdir(String path) { File f = new File(path); - if(!f.exists()){ + if (!f.exists()) { f.mkdirs(); } } diff --git a/src/main/java/com/jiaruiblog/util/poi/PptToPDFConverter.java b/src/main/java/com/jiaruiblog/util/poi/PptToPDFConverter.java index 904acc0..68aa2a9 100644 --- a/src/main/java/com/jiaruiblog/util/poi/PptToPDFConverter.java +++ b/src/main/java/com/jiaruiblog/util/poi/PptToPDFConverter.java @@ -63,7 +63,6 @@ public void convert() throws Exception { Graphics2D graphics = bufImg.createGraphics(); graphics.setTransform(at); //clear the drawing area -// graphics.setPaint(getSlideBGColor(i)); graphics.setPaint(Color.white); graphics.fill(new Rectangle2D.Float(0, 0, pgSize.width, pgSize.height)); try { @@ -103,9 +102,5 @@ protected void drawOntoThisGraphic(int index, Graphics2D graphics) { slides[index].draw(graphics); } - protected Color getSlideBGColor(int index) { -// return slides[index].getBackground().getFillColor(); - return null; - } }