Initial MVP: Go + templ Tolino-optimized ebook library scaffold
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
booksDir string
|
||||
}
|
||||
|
||||
func New(booksDir string) *Service {
|
||||
return &Service{booksDir: booksDir}
|
||||
}
|
||||
|
||||
func (s *Service) BooksDir() string { return s.booksDir }
|
||||
|
||||
func (s *Service) ListBooks() ([]Book, error) {
|
||||
entries, err := os.ReadDir(s.booksDir)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return []Book{}, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
books := make([]Book, 0, len(entries))
|
||||
for _, e := range entries {
|
||||
if e.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
ext := strings.ToLower(filepath.Ext(e.Name()))
|
||||
if ext != ".epub" && ext != ".pdf" {
|
||||
continue
|
||||
}
|
||||
|
||||
fullPath := filepath.Join(s.booksDir, e.Name())
|
||||
id := stableID(e.Name())
|
||||
title := strings.TrimSuffix(e.Name(), filepath.Ext(e.Name()))
|
||||
|
||||
books = append(books, Book{
|
||||
ID: id,
|
||||
Title: title,
|
||||
Filename: e.Name(),
|
||||
Path: fullPath,
|
||||
Format: strings.TrimPrefix(ext, "."),
|
||||
})
|
||||
}
|
||||
|
||||
sort.Slice(books, func(i, j int) bool {
|
||||
return strings.ToLower(books[i].Title) < strings.ToLower(books[j].Title)
|
||||
})
|
||||
|
||||
return books, nil
|
||||
}
|
||||
|
||||
func (s *Service) FindBook(id string) (*Book, error) {
|
||||
books, err := s.ListBooks()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for i := range books {
|
||||
if books[i].ID == id {
|
||||
return &books[i], nil
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func stableID(input string) string {
|
||||
h := sha1.Sum([]byte(strings.ToLower(input)))
|
||||
return hex.EncodeToString(h[:8])
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package library
|
||||
|
||||
type Book struct {
|
||||
ID string
|
||||
Title string
|
||||
Filename string
|
||||
Path string
|
||||
Format string
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/arnef/ebooks/internal/library"
|
||||
"github.com/arnef/ebooks/views"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
lib *library.Service
|
||||
}
|
||||
|
||||
func NewHandler(lib *library.Service) *Handler {
|
||||
return &Handler{lib: lib}
|
||||
}
|
||||
|
||||
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.Handle("GET /static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
|
||||
}
|
||||
|
||||
func (h *Handler) listBooks(w http.ResponseWriter, r *http.Request) {
|
||||
books, err := h.lib.ListBooks()
|
||||
if err != nil {
|
||||
http.Error(w, "failed to load books", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
render(w, r.Context(), views.IndexPage(books))
|
||||
}
|
||||
|
||||
func (h *Handler) bookDetails(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("id")
|
||||
if id == "" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
book, err := h.lib.FindBook(id)
|
||||
if err != nil {
|
||||
http.Error(w, "failed to load book", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if book == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
render(w, r.Context(), views.BookPage(*book))
|
||||
}
|
||||
|
||||
func (h *Handler) downloadBook(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("id")
|
||||
if id == "" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
book, err := h.lib.FindBook(id)
|
||||
if err != nil {
|
||||
http.Error(w, "failed to load book", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if book == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
cleanBase := filepath.Clean(h.lib.BooksDir())
|
||||
cleanPath := filepath.Clean(book.Path)
|
||||
if !strings.HasPrefix(cleanPath, cleanBase+string(filepath.Separator)) && cleanPath != cleanBase {
|
||||
http.Error(w, "invalid file path", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Disposition", "attachment; filename=\""+book.Filename+"\"")
|
||||
http.ServeFile(w, r, cleanPath)
|
||||
}
|
||||
|
||||
func render(w http.ResponseWriter, ctx context.Context, page interface{ Render(context.Context, http.ResponseWriter) error }) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
if err := page.Render(ctx, w); err != nil && !errors.Is(err, context.Canceled) {
|
||||
http.Error(w, "render failed", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user