Skip to content

Commit 90618af

Browse files
author
J.P
committed
feat(service): 支持处理TXT格式书籍文件
1 parent 647818f commit 90618af

2 files changed

Lines changed: 74 additions & 7 deletions

File tree

src/main/java/com/jp/epubbot/service/BookBot.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ public void onUpdateReceived(Update update) {
7979
return;
8080
}
8181
Document doc = update.getMessage().getDocument();
82-
if (doc.getFileName() != null && doc.getFileName().toLowerCase().endsWith(".epub")) {
83-
handleEpubFile(chatId, doc);
82+
String fileName = doc.getFileName();
83+
if (fileName != null && (fileName.toLowerCase().endsWith(".epub") || fileName.toLowerCase().endsWith(".txt"))) {
84+
handleBookFile(chatId, doc);
8485
} else {
8586
sendText(chatId, "请发送 .epub 格式的文件。");
8687
}
@@ -153,7 +154,7 @@ private void handleListBookmarks(Long chatId) {
153154
}
154155
}
155156

156-
private void handleEpubFile(Long chatId, Document doc) {
157+
private void handleBookFile(Long chatId, Document doc) {
157158
if (processingUsers.contains(chatId)) {
158159
sendText(chatId, "⚠️ 上一本书正在处理中,请稍候...");
159160
return;
@@ -189,7 +190,15 @@ private void handleEpubFile(Long chatId, Document doc) {
189190

190191
log.info("file url: [{}]", fileUrl);
191192
try (InputStream in = new URL(fileUrl).openStream()) {
192-
List<String> links = epubService.processEpub(in, doc.getFileName());
193+
String fileName = doc.getFileName().toLowerCase();
194+
List<String> links;
195+
if (fileName.endsWith(".epub")) {
196+
links = epubService.processEpub(in, doc.getFileName());
197+
} else if (fileName.endsWith(".txt")) {
198+
links = epubService.processTxt(in, doc.getFileName());
199+
} else {
200+
throw new IllegalArgumentException("不支持的文件格式");
201+
}
193202
if (links.isEmpty()) {
194203
sendText(chatId, "❌ 解析失败或内容为空。");
195204
} else {

src/main/java/com/jp/epubbot/service/EpubService.java

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@
1212
import org.springframework.beans.factory.annotation.Value;
1313
import org.springframework.stereotype.Service;
1414

15+
import java.io.BufferedReader;
16+
import java.io.InputStream;
17+
import java.io.InputStreamReader;
1518
import java.nio.charset.StandardCharsets;
1619
import java.util.ArrayList;
1720
import java.util.List;
1821
import java.util.UUID;
22+
import java.util.stream.Collectors;
1923

2024
@Slf4j
2125
@Service
@@ -48,7 +52,7 @@ public List<String> processEpub(java.io.InputStream epubStream, String fileName)
4852
int currentLength = 0;
4953
int pageCounter = 1;
5054

51-
log.info("开始解析书籍: {} (ID: {})", bookTitle, bookId);
55+
log.info("开始解析epub书籍: {} (ID: {})", bookTitle, bookId);
5256

5357
for (Resource res : contents) {
5458
try {
@@ -68,7 +72,7 @@ public List<String> processEpub(java.io.InputStream epubStream, String fileName)
6872
int childLen = child.text().length();
6973

7074
if (!child.select("img").isEmpty() || child.tagName().equalsIgnoreCase("img") || child.tagName().equalsIgnoreCase("svg")) {
71-
childLen += 1000;
75+
childLen += 500;
7276
}
7377

7478
int minPageThreshold = 800;
@@ -95,7 +99,7 @@ public List<String> processEpub(java.io.InputStream epubStream, String fileName)
9599
pageUrls.add(pageUrl);
96100
bookmarkService.createBookmarkToken(bookTitle, bookTitle + " (" + pageCounter + ") - End", pageUrl, token);
97101
}
98-
102+
log.info("解析epub书籍完成: {} (ID: {})", bookTitle, bookId);
99103
return pageUrls;
100104
}
101105

@@ -105,6 +109,60 @@ private String uploadPage(String bookId, String bookTitle, int pageIndex, String
105109
return r2StorageService.uploadFile(path, html.getBytes(StandardCharsets.UTF_8), "text/html");
106110
}
107111

112+
public List<String> processTxt(InputStream txtStream, String fileName) throws Exception {
113+
String bookTitle = fileName.replace(".txt", "").replace(".TXT", "");
114+
String bookId = UUID.randomUUID().toString().replace("-", "");
115+
List<String> pageUrls = new ArrayList<>();
116+
117+
log.info("开始解析TXT书籍: {} (ID: {})", bookTitle, bookId);
118+
119+
List<String> lines;
120+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(txtStream, StandardCharsets.UTF_8))) {
121+
lines = reader.lines().toList();
122+
}
123+
124+
StringBuilder currentHtmlBuffer = new StringBuilder();
125+
int currentLength = 0;
126+
int pageCounter = 1;
127+
128+
for (String line : lines) {
129+
String safeLine = line.replace("&", "&amp;")
130+
.replace("<", "&lt;")
131+
.replace(">", "&gt;");
132+
133+
String lineHtml = "<p>" + safeLine + "</p>";
134+
135+
if (safeLine.trim().isEmpty()) {
136+
lineHtml = "<br/>";
137+
}
138+
139+
int lineLen = safeLine.length();
140+
141+
if ((currentLength + lineLen > charsPerPage) && (currentLength > 1000)) {
142+
String token = "bm_" + UUID.randomUUID().toString().substring(0, 8);
143+
String pageUrl = uploadPage(bookId, bookTitle, pageCounter, currentHtmlBuffer.toString(), false, token);
144+
pageUrls.add(pageUrl);
145+
bookmarkService.createBookmarkToken(bookTitle, bookTitle + " (" + pageCounter + ")", pageUrl, token);
146+
147+
currentHtmlBuffer.setLength(0);
148+
currentLength = 0;
149+
pageCounter++;
150+
}
151+
152+
currentHtmlBuffer.append(lineHtml);
153+
currentLength += lineLen;
154+
}
155+
156+
if (!currentHtmlBuffer.isEmpty()) {
157+
String token = "bm_" + UUID.randomUUID().toString().substring(0, 8);
158+
String pageUrl = uploadPage(bookId, bookTitle, pageCounter, currentHtmlBuffer.toString(), true, token);
159+
pageUrls.add(pageUrl);
160+
bookmarkService.createBookmarkToken(bookTitle, bookTitle + " (" + pageCounter + ") - End", pageUrl, token);
161+
}
162+
log.info("解析TXT书籍完成: {} (ID: {})", bookTitle, bookId);
163+
return pageUrls;
164+
}
165+
108166
private void handleImagesR2(Document doc, Book book, String currentResourceHref, String bookId) {
109167
for (Element img : doc.select("img")) {
110168
String src = img.attr("src");

0 commit comments

Comments
 (0)