Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(parser): resolve EPUB manifest hrefs as URLs (#20228)

Jindca84f6b25dc
diff --git a/internal/parser/parser/epub_parser.go b/internal/parser/parser/epub_parser.go
index e3cc30f1e3..d26574d37a 100644
--- a/internal/parser/parser/epub_parser.go
+++ b/internal/parser/parser/epub_parser.go
@@ -35,6 +35,7 @@ import (
"encoding/xml"
"fmt"
"io"
+ "net/url"
"path"
"regexp"
"strings"
@@ -239,6 +240,14 @@ func extractEPUBTextItems(r *zip.Reader, opfDir string, spineHrefs []string) ([]
// readEPUBContentFile resolves a spine href (relative to the OPF
// directory) inside the ZIP and returns the item's raw bytes.
func readEPUBContentFile(r *zip.Reader, opfDir, href string) ([]byte, error) {
+ // Manifest hrefs are URLs: drop the fragment and decode %-escapes to get
+ // the ZIP entry name.
+ if i := strings.IndexByte(href, '#'); i >= 0 {
+ href = href[:i]
+ }
+ if unescaped, err := url.PathUnescape(href); err == nil {
+ href = unescaped
+ }
// Resolve href relative to the directory containing the OPF.
// Use path.Join/Dir (slash separator) because ZIP entry paths are always POSIX.
resolved := path.Join(path.Dir(opfDir), href)
diff --git a/internal/parser/parser/epub_parser_test.go b/internal/parser/parser/epub_parser_test.go
index 05fc917189..3e22fc8c6c 100644
--- a/internal/parser/parser/epub_parser_test.go
+++ b/internal/parser/parser/epub_parser_test.go
@@ -30,6 +30,7 @@ import (
// stored as a Deflate entry whose data is not a valid Deflate stream.
type epubTestChapter struct {
name string
+ entry string // ZIP entry under OEBPS/ when it differs from the manifest href
content string
missing bool
damaged bool
@@ -73,6 +74,8 @@ func buildTestEPUB(t *testing.T, chapters []epubTestChapter) []byte {
if _, err := w.Write([]byte{0xff, 0xff, 0xff, 0xff}); err != nil {
t.Fatalf("write raw %s: %v", ch.name, err)
}
+ case ch.entry != "":
+ write("OEBPS/"+ch.entry, ch.content)
default:
write("OEBPS/"+ch.name, ch.content)
}
@@ -185,3 +188,21 @@ func TestEPUBParser_OnlyEmptyChaptersIsNotAnError(t *testing.T) {
t.Fatalf("ParseWithResult: %v", res.Err)
}
}
+
+// Manifest hrefs are URLs, so non-ASCII and space file names are
+// percent-encoded and may carry a fragment.
+func TestEPUBParser_ResolvesManifestHrefsAsURLs(t *testing.T) {
+ res := parseTestEPUB(t, []epubTestChapter{
+ {name: "Text/%E7%AC%AC%E4%B8%80%E7%AB%A0.xhtml", entry: "Text/第一章.xhtml", content: epubTestHTML("<p>ALPHA chapter</p>")},
+ {name: "Text/chapter%202.xhtml#start", entry: "Text/chapter 2.xhtml", content: epubTestHTML("<p>BRAVO chapter</p>")},
+ })
+ if res.Err != nil {
+ t.Fatalf("ParseWithResult: %v", res.Err)
+ }
+ text := epubTestText(res)
+ for _, want := range []string{"ALPHA", "BRAVO"} {
+ if !strings.Contains(text, want) {
+ t.Errorf("missing %q in %q", want, text)
+ }
+ }
+}