Skip to content

Commit

Permalink
add: add slider executor
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarrett committed Nov 10, 2024
1 parent 8b864c0 commit 69d4077
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 102 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/jiaruiblog/enums/DocType.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum DocType {
PDF,
// word文档
DOCX,
PPT,
PPTX,
XLSX,
// txt类的文档
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ private void updateDocTxt(FileDocument fileDocument) {
switch (suffix) {
case "pdf":
case "docx":
case "ppt":
case "pptx":
case "xlsx":
case "html":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -506,6 +507,7 @@ private FileDocument saveFileNew(MultipartFile file,
switch (suffix) {
case "pdf":
case "docx":
case "ppt":
case "pptx":
case "xlsx":
case "html":
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -41,6 +42,7 @@ private static TaskExecutor createTaskExecutor(DocType docType) {
case XLSX:
taskExecutor = new DocxExecutor();
break;
case PPT:
case PPTX:
taskExecutor = new PptxExecutor();
break;
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/com/jiaruiblog/task/executor/slider/PptExecutor.java
Original file line number Diff line number Diff line change
@@ -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;

/**
* <p></p>
* 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
}

}
64 changes: 0 additions & 64 deletions src/main/java/com/jiaruiblog/util/poi/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}








}
Loading

0 comments on commit 69d4077

Please sign in to comment.