Add in-browser EPUB reader (no JavaScript)
- internal/library/reader.go: new Chapter type and Service.ReadChapter/
ChapterAsset. Parses the EPUB spine, extracts and sanitizes each
chapter's <body> HTML using golang.org/x/net/html:
- strips <script>, <style>, <link>, <iframe>, <object>, <embed>,
<form>, <meta>, <base>, <audio>, <video>, <noscript> and any
on*-event-handler attributes
- rewrites relative <img>/xlink:href asset references to
/read/{id}/asset/{path}
- rewrites internal chapter links to /read/{id}/{spineIndex},
neutralizes javascript: hrefs, leaves external links untouched
- refactored CoverBytes to share the new findEPUBPath helper
- internal/web/handlers.go: GET /read/{id}/{idx} renders a chapter page
with prev/next navigation; GET /read/{id}/asset/{path...} serves
embedded chapter assets (images) with a size cap, mirroring the
existing cover-image guard
- views/pages.templ: new ReaderPage component (renders sanitized HTML
via templ.Raw); BookPage gets an "Im Browser lesen" button next to
the existing download button; templ regenerated
- static/styles.css: reader typography/layout, prev/next nav styling
- go.mod/go.sum: golang.org/x/net/html promoted to a direct dependency
- README: documents the new in-browser reader feature
Verified end-to-end with a hand-built test EPUB (spine ordering,
metadata, script/style stripping, event-handler stripping, chapter
link rewriting, image asset rewriting, javascript: neutralization,
external link passthrough, out-of-range chapter handling, and
path-traversal guard on chapter assets); test file removed afterwards
per repo convention (no test suite checked in yet).
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -102,29 +102,11 @@ func (s *Service) CoverBytes(id string) ([]byte, string, error) {
|
||||
return nil, "", nil
|
||||
}
|
||||
|
||||
entries, err := os.ReadDir(s.booksDir)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil, "", nil
|
||||
}
|
||||
fullPath, err := s.findEPUBPath(id)
|
||||
if err != nil || fullPath == "" {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
for _, e := range entries {
|
||||
if e.IsDir() {
|
||||
continue
|
||||
}
|
||||
ext := strings.ToLower(filepath.Ext(e.Name()))
|
||||
if ext != ".epub" {
|
||||
continue
|
||||
}
|
||||
if stableID(e.Name()) != id {
|
||||
continue
|
||||
}
|
||||
return readEPUBCover(filepath.Join(s.booksDir, e.Name()))
|
||||
}
|
||||
|
||||
return nil, "", nil
|
||||
return readEPUBCover(fullPath)
|
||||
}
|
||||
|
||||
func stableID(input string) string {
|
||||
@@ -161,6 +143,11 @@ type packageXML struct {
|
||||
Properties string `xml:"properties,attr"`
|
||||
} `xml:"item"`
|
||||
} `xml:"manifest"`
|
||||
Spine struct {
|
||||
Itemrefs []struct {
|
||||
IDref string `xml:"idref,attr"`
|
||||
} `xml:"itemref"`
|
||||
} `xml:"spine"`
|
||||
}
|
||||
|
||||
func readEPUBMetadata(filePath string) (epubMetadata, error) {
|
||||
|
||||
Reference in New Issue
Block a user