|
|
@@ -1,5 +1,6 @@
|
|
|
package nju.seec.helper.util.file;
|
|
|
|
|
|
+import cn.hutool.core.io.FileTypeUtil;
|
|
|
import com.google.common.collect.ImmutableMap;
|
|
|
import lombok.SneakyThrows;
|
|
|
import lombok.experimental.UtilityClass;
|
|
|
@@ -7,11 +8,10 @@ import nju.seec.helper.enums.ExceptionType;
|
|
|
import nju.seec.helper.exception.HelperException;
|
|
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
|
|
import org.apache.pdfbox.text.PDFTextStripper;
|
|
|
-import org.apache.poi.hslf.usermodel.HSLFSlideShow;
|
|
|
-import org.apache.poi.sl.extractor.SlideShowExtractor;
|
|
|
-import org.apache.poi.xslf.usermodel.XMLSlideShow;
|
|
|
-import org.springframework.web.multipart.MultipartFile;
|
|
|
+import org.apache.poi.util.IOUtils;
|
|
|
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.util.Map;
|
|
|
@@ -23,27 +23,28 @@ import java.util.Optional;
|
|
|
@UtilityClass
|
|
|
public class FileUtils {
|
|
|
private static final Map<String, FileInfoParser> FILE_INFO_PARSER_MAP = ImmutableMap.of(
|
|
|
- "application/pdf", file -> FileInfo.of(PDDocument.load(file.getInputStream()).getNumberOfPages(), "pdf")
|
|
|
- , "application/vnd.ms-powerpoint", file -> FileInfo.of(new HSLFSlideShow(file.getInputStream()).getSlides().size(), "ppt")
|
|
|
- , "application/vnd.openxmlformats-officedocument.presentationml.presentation", file -> FileInfo.of(new XMLSlideShow(file.getInputStream()).getSlides().size(), "pptx")
|
|
|
+ "pdf", inputStream -> {
|
|
|
+ System.out.println(inputStream);
|
|
|
+ return FileInfo.of(PDDocument.load(inputStream).getNumberOfPages(), "pdf");
|
|
|
+ }
|
|
|
);
|
|
|
|
|
|
private static final Map<String, FileContentParser> FILE_CONTENT_PARSER_MAP = ImmutableMap.of(
|
|
|
"pdf", inputStream -> new PDFTextStripper().getText(PDDocument.load(inputStream))
|
|
|
- , "ppt", inputStream -> new SlideShowExtractor<>(new HSLFSlideShow(inputStream)).getText()
|
|
|
- , "pptx", inputStream -> new SlideShowExtractor<>(new XMLSlideShow(inputStream)).getText()
|
|
|
);
|
|
|
|
|
|
@SneakyThrows
|
|
|
- public FileInfo getFileInfo(MultipartFile file) {
|
|
|
- return Optional.ofNullable(FILE_INFO_PARSER_MAP.get(file.getContentType()))
|
|
|
+ public FileInfo getFileInfo(InputStream inputStream) {
|
|
|
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
+ IOUtils.copy(inputStream, byteArrayOutputStream);
|
|
|
+ return Optional.ofNullable(FILE_INFO_PARSER_MAP.get(FileTypeUtil.getType(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()))))
|
|
|
.orElseThrow(() -> HelperException.of(ExceptionType.FORBIDDEN, "不支持的文件类型"))
|
|
|
- .parse(file);
|
|
|
+ .parse(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
|
|
|
}
|
|
|
|
|
|
@SneakyThrows
|
|
|
- public String getFileContent(String extension, InputStream inputStream) {
|
|
|
- return Optional.ofNullable(FILE_CONTENT_PARSER_MAP.get(extension))
|
|
|
+ public String getFileContent(InputStream inputStream) {
|
|
|
+ return Optional.ofNullable(FILE_CONTENT_PARSER_MAP.get(FileTypeUtil.getType(inputStream)))
|
|
|
.orElseThrow(() -> HelperException.of(ExceptionType.FORBIDDEN, "不支持的文件类型"))
|
|
|
.parse(inputStream);
|
|
|
}
|
|
|
@@ -53,11 +54,11 @@ public class FileUtils {
|
|
|
/**
|
|
|
* 解析文件信息
|
|
|
*
|
|
|
- * @param file
|
|
|
+ * @param inputStream
|
|
|
* @return
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
- FileInfo parse(MultipartFile file) throws IOException;
|
|
|
+ FileInfo parse(InputStream inputStream) throws IOException;
|
|
|
}
|
|
|
|
|
|
@FunctionalInterface
|