Add ebook metadata, author and cover display
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user