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:
2026-08-15 16:17:16 +02:00
co-authored by Copilot
parent be3dfaeb47
commit 2399d8a5a6
9 changed files with 727 additions and 48 deletions
+27 -1
View File
@@ -1,6 +1,7 @@
package views
import (
"fmt"
"strings"
"github.com/arnef/ebooks/internal/library"
)
@@ -145,12 +146,37 @@ templ BookPage(book library.Book) {
<dd>{ book.Format }</dd>
</div>
</dl>
<p><a class="btn" href={ "/download/" + book.ID }>Auf Tolino herunterladen</a></p>
<p>
<a class="btn" href={ "/read/" + book.ID + "/0" }>Im Browser lesen</a>
<a class="btn" href={ "/download/" + book.ID }>Auf Tolino herunterladen</a>
</p>
</div>
</article>
}
}
templ ReaderPage(book library.Book, chapter library.Chapter) {
@Layout(book.Title, "/book/" + book.ID) {
<article class="reader">
<p class="reader-progress">{ fmt.Sprintf("Kapitel %d von %d", chapter.Index+1, chapter.Total) }</p>
if chapter.Title != "" {
<h2 class="reader-chapter-title">{ chapter.Title }</h2>
}
<div class="reader-content">
@templ.Raw(chapter.HTML)
</div>
<nav class="reader-nav">
if chapter.HasPrev {
<a class="btn reader-nav-prev" href={ fmt.Sprintf("/read/%s/%d", book.ID, chapter.PrevIndex) }> Zurück</a>
}
if chapter.HasNext {
<a class="btn reader-nav-next" href={ fmt.Sprintf("/read/%s/%d", book.ID, chapter.NextIndex) }>Weiter </a>
}
</nav>
</article>
}
}
func coverClass(detail bool) string {
if detail {
return "book-cover book-cover-detail"