Add ebook metadata, author and cover display

This commit is contained in:
Arne
2026-08-06 20:40:40 +02:00
parent 060d822654
commit b163aade71
6 changed files with 481 additions and 30 deletions
+26
View File
@@ -24,6 +24,7 @@ func (h *Handler) RegisterRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET /", h.listBooks)
mux.HandleFunc("GET /book/{id}", h.bookDetails)
mux.HandleFunc("GET /download/{id}", h.downloadBook)
mux.HandleFunc("GET /cover/{id}", h.bookCover)
mux.Handle("GET /static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
}
@@ -84,6 +85,31 @@ func (h *Handler) downloadBook(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, cleanPath)
}
func (h *Handler) bookCover(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
if id == "" {
http.NotFound(w, r)
return
}
data, contentType, err := h.lib.CoverBytes(id)
if err != nil {
http.Error(w, "failed to load cover", http.StatusInternalServerError)
return
}
if len(data) == 0 {
http.NotFound(w, r)
return
}
if contentType == "" {
contentType = "application/octet-stream"
}
w.Header().Set("Content-Type", contentType)
w.Header().Set("Cache-Control", "public, max-age=3600")
_, _ = w.Write(data)
}
func render(w http.ResponseWriter, ctx context.Context, c templ.Component) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if err := c.Render(ctx, w); err != nil && !errors.Is(err, context.Canceled) {