1212import org .springframework .beans .factory .annotation .Value ;
1313import org .springframework .stereotype .Service ;
1414
15+ import java .io .BufferedReader ;
16+ import java .io .InputStream ;
17+ import java .io .InputStreamReader ;
1518import java .nio .charset .StandardCharsets ;
1619import java .util .ArrayList ;
1720import java .util .List ;
1821import 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 ("&" , "&" )
130+ .replace ("<" , "<" )
131+ .replace (">" , ">" );
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