电子书拆分修改

This commit is contained in:
wuchunlei
2024-11-26 15:11:58 +08:00
parent 708e925bf0
commit 3fc7a80580

View File

@@ -27,13 +27,15 @@ import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.xwpf.usermodel.*;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -450,53 +452,113 @@ public class BookServiceImpl extends ServiceImpl<BookDao, BookEntity> implements
*/
@Override
public boolean getWordSection(Integer bookId) {
BookEntity bookEntity = this.getBaseMapper().selectById(bookId);
String novel = bookEntity.getNovel();
InputStream inputStream = FileDownloadUtil.getInputStream(novel);
Document doc = new Document(inputStream);
int index = 0;
int x1 = 0;
int x2 = 0;
String chapterName = "";
int chapterId = 0;
for (int i = 0; i < doc.getSections().getCount(); i++) {
Section section = doc.getSections().get(i);
for (int j = 0; j < section.getParagraphs().getCount() - 1; j++) {
Paragraph paragraph = section.getParagraphs().get(j);
BookChapterEntity chapter = new BookChapterEntity();
chapter.setBookId(bookId);
chapter.setNumber(index++);
chapter.setSort(j);
if (paragraph.getStyleName().equals("Heading1")&&StringUtils.isNotBlank(paragraph.getText())) {
x1 = j;
chapterName = paragraph.getText();
}else if (paragraph.getStyleName().equals("Heading2")&&StringUtils.isNotBlank(paragraph.getText())){
x2 = j;
chapter.setChapter(chapterName);
chapter.setContent(paragraph.getText());
System.out.println(paragraph.getText());
bookChapterService.save(chapter);
chapterId = chapter.getId();
}else if (StringUtils.isNotBlank(paragraph.getText())){
if (x1 > x2){
chapter.setChapter(chapterName);
chapter.setContent(paragraph.getText());
System.out.println(paragraph.getText());
bookChapterService.save(chapter);
}else {
BookChapterContentEntity content = new BookChapterContentEntity();
content.setBookId(bookId);
content.setBookChatperId(chapterId);
content.setNumber(index);
content.setContent(paragraph.getText());
bookChapterContentService.save(content);
try{
int topText = 22;
int secText = 16;
BookEntity bookEntity = this.getBaseMapper().selectById(bookId);
String novel = bookEntity.getNovel();
InputStream inputStream = FileDownloadUtil.getInputStream(novel);
XWPFDocument document = new XWPFDocument(inputStream);
// 遍历文档中的所有元素(段落和表格)
List<IBodyElement> bodyElements = document.getBodyElements();
String chapterName = "";
int bookChapterId = 0;
int number = 0;
for (int i=0;i < bodyElements.size(); i++) {
IBodyElement element = bodyElements.get(i);
if (element instanceof XWPFParagraph) {
XWPFParagraph paragraph = (XWPFParagraph) element;
String text = paragraph.getText();
if (text != null && !text.isEmpty()) {//处理段落或正文
if (paragraph.getRuns().get(0).getFontSize()>=topText){//判断是否是顶级标题
chapterName = text;
BookChapterEntity bookChapter = new BookChapterEntity();
bookChapter.setBookId(bookId);
bookChapter.setChapter(text);
bookChapter.setNumber(number++);
for (int j=1;j<bodyElements.size()-i;j++){//判断下一段落是二级标题还是正文,正文直接保存
IBodyElement e = bodyElements.get(i+j);
if (e instanceof XWPFParagraph) {
XWPFParagraph p = (XWPFParagraph) e;
if (p.getText() != null && !p.getText().isEmpty()){
if (p.getRuns().get(0).getFontSize()<secText){
bookChapterService.save(bookChapter);
bookChapterId = bookChapter.getId();
}
break;
}
}
}
}else if (paragraph.getRuns().get(0).getFontSize()>=secText){//判断是否是二级标题
if (StringUtils.isNotBlank(chapterName)){
BookChapterEntity bookChapter = new BookChapterEntity();
bookChapter.setBookId(bookId);
bookChapter.setChapter(chapterName);
bookChapter.setContent(text);
bookChapter.setNumber(number++);
bookChapterService.save(bookChapter);
bookChapterId = bookChapter.getId();
}
}else {//正文
if (bookChapterId!=0){
BookChapterContentEntity contentEntity = new BookChapterContentEntity();
contentEntity.setBookId(bookId);
contentEntity.setBookChatperId(bookChapterId);
contentEntity.setContent(text);
contentEntity.setNumber(number++);
bookChapterContentService.save(contentEntity);
}
}
} else {// 顺序遍历图片
List<IRunElement> runs = paragraph.getIRuns();
for (IRunElement run : runs) {
if (run instanceof XWPFRun) {
XWPFRun xWPFRun = (XWPFRun) run;
for (XWPFPicture picture : xWPFRun.getEmbeddedPictures()) {
XWPFPictureData pictureData = picture.getPictureData();
String fileName = pictureData.getFileName();
String url = uploadFile(new ByteArrayInputStream(pictureData.getData()),fileName);
BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(pictureData.getData()));
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
BookChapterContentEntity contentEntity = new BookChapterContentEntity();
contentEntity.setBookId(bookId);
contentEntity.setBookChatperId(bookChapterId);
contentEntity.setContent(url);
contentEntity.setOtherContent(width+","+height);
contentEntity.setNumber(number++);
bookChapterContentService.save(contentEntity);
}
}
}
}
}
}
}catch (Exception e) {
e.printStackTrace();;
}
return false;
}
public String uploadFile(InputStream inputStream,String fileName) {
String endpoint = ConstantPropertiesUtils.END_POIND;
String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;
String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;
String bucketName = ConstantPropertiesUtils.BUCKET_NAME;
try {
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
fileName = uuid + fileName;
String datePath = new DateTime().toString("yyyy/MM/dd");
fileName = datePath + "/" + fileName;
ossClient.putObject(bucketName, fileName, inputStream);
ossClient.shutdown();
String url = "https://" + bucketName + "." + endpoint + "/" + fileName;
return url;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
public PageUtils queryPagebooks(Map<String, Object> params) {
IPage<BookEntity> page = this.page(