diff --git a/.dockerignore b/.dockerignore
new file mode 100644
index 0000000..e55cc45
--- /dev/null
+++ b/.dockerignore
@@ -0,0 +1,5 @@
+.env
+.git
+library/
+.superpowers/
+backend/booklib*
diff --git a/.env.example b/.env.example
new file mode 100644
index 0000000..95ccbac
--- /dev/null
+++ b/.env.example
@@ -0,0 +1,4 @@
+JWT_SECRET=change-me-openssl-rand-hex-32
+ADMIN_USER=admin
+ADMIN_PASSWORD=change-me-min-8
+SCAN_INTERVAL_SEC=60
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..8f12811
--- /dev/null
+++ b/README.md
@@ -0,0 +1,38 @@
+# Book & Comic Library
+
+个人书库/漫画库:Go+Gin 后端(扫描/上传入库、多用户 JWT、阅读进度、磁盘+Redis 缓存)+ Docker Compose 部署。设计见 `docs/superpowers/specs/2026-09-04-book-comic-library-design.md`。
+
+## 跑起来(生产形态)
+
+```bash
+cp .env.example .env # 填 JWT_SECRET、ADMIN_USER、ADMIN_PASSWORD(≥8 位,低于 8 位 seed 会跳过并 log)
+docker compose up -d --build
+./scripts/smoke.sh # 端到端验收(登录、建库、上传、扫描、进度、删除、immutable 头)
+```
+
+- web: `http://localhost:8080`,API 走 nginx `/api/` 前缀反代到无状态 api 副本(`--scale api=N`)。
+- 原始书放在 `./library/`(挂到 `/data/books`),scanner 周期入库(默认 60s)。
+
+## 可信代理与限流
+
+- nginx 在 compose 网络内,api 的 `ClientIP` 只信 `TRUSTED_PROXY_CIDRS`(逗号分隔 CIDR,默认 `172.16.0.0/12`,即 compose 网段)。外部伪造 `X-Forwarded-For` 换不掉限流桶;换部署网络时改这个 env。
+- 登录限流 5 次/分钟/IP **按尝试计数,成功登录也计**——爆破和正常高频登录同账。
+
+## 开发 / 测试
+
+```bash
+docker compose -f deploy/docker-compose.dev.yml up -d # PG :5433, Redis :6380(避开本机默认端口)
+cd backend
+export DATABASE_URL='postgres://lib:lib@localhost:5433/lib?sslmode=disable'
+export REDIS_URL='redis://localhost:6380'
+go vet ./... && gofmt -l .
+go test -p 1 -count=1 ./...
+```
+
+`-p 1` 是必须的:集成测试共用同一个 PG 库,各自 `DELETE FROM ...` 清表——并行跑会互相删数据导致随机失败。
+
+无 PG/Redis 时依赖它们的测试自动 skip;Redis 挂掉不影响功能(全链路降级为 miss/放行,见 spec §9)。
+
+## 改 schema 前必读
+
+`db.Migrate` 只执行 `schema.sql` 的 `CREATE TABLE IF NOT EXISTS`——对已存在的库**加列/改列不会生效**。任何列变更之前,必须先引入 `schema_migrations` 版本表 + 有序迁移脚本,否则老部署会静默跑在旧结构上。
diff --git a/backend/cmd/server/main.go b/backend/cmd/server/main.go
new file mode 100644
index 0000000..0548531
--- /dev/null
+++ b/backend/cmd/server/main.go
@@ -0,0 +1,62 @@
+package main
+
+import (
+ "context"
+ "errors"
+ "log"
+ "net/http"
+ "os/signal"
+ "syscall"
+ "time"
+
+ "booklib/internal/api"
+ "booklib/internal/config"
+ "booklib/internal/db"
+ "booklib/internal/redispkg"
+ "booklib/internal/scanner"
+ "booklib/internal/seed"
+ "booklib/internal/store"
+)
+
+func main() {
+ cfg, err := config.Load()
+ if err != nil {
+ log.Fatalf("config: %v", err)
+ }
+ ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
+ defer stop()
+
+ p, err := db.Connect(ctx, cfg.DatabaseURL)
+ if err != nil {
+ log.Fatalf("db connect: %v", err)
+ }
+ defer p.Close()
+ if err := db.Migrate(ctx, p); err != nil {
+ log.Fatalf("migrate: %v", err)
+ }
+ st := store.New(p)
+ if err := seed.Admin(ctx, st, cfg.AdminUser, cfg.AdminPassword); err != nil {
+ log.Fatalf("seed: %v", err)
+ }
+ rdb := redispkg.New(cfg.RedisURL)
+ sc := scanner.New(st, cfg, rdb)
+ go sc.Run(ctx)
+
+ srv := &http.Server{Addr: cfg.Addr, Handler: api.NewRouter(cfg, st, rdb, sc),
+ ReadHeaderTimeout: 10 * time.Second}
+ go func() {
+ log.Printf("listening on %s", cfg.Addr)
+ if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
+ log.Fatalf("serve: %v", err)
+ }
+ }()
+ <-ctx.Done()
+ // stop() 先取消 ctx → scanner 循环退出;再等 HTTP 收尾。
+ // 在途 ScanLibraryByID(WithoutCancel)不受 ctx 控制,靠 redis 锁 TTL 兜底(已文档化的上限)。
+ stop()
+ shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+ defer cancel()
+ if err := srv.Shutdown(shutdownCtx); err != nil {
+ log.Printf("shutdown: %v", err)
+ }
+}
diff --git a/backend/go.mod b/backend/go.mod
new file mode 100644
index 0000000..2d32a42
--- /dev/null
+++ b/backend/go.mod
@@ -0,0 +1,48 @@
+module booklib
+
+go 1.26.0
+
+require (
+ github.com/gin-gonic/gin v1.12.0
+ github.com/golang-jwt/jwt/v5 v5.3.1
+ github.com/jackc/pgx/v5 v5.10.0
+ github.com/jackc/puddle/v2 v2.2.2
+ github.com/redis/go-redis/v9 v9.22.0
+ golang.org/x/crypto v0.56.0
+)
+
+require (
+ github.com/bytedance/gopkg v0.1.3 // indirect
+ github.com/bytedance/sonic v1.15.0 // indirect
+ github.com/bytedance/sonic/loader v0.5.0 // indirect
+ github.com/cespare/xxhash/v2 v2.3.0 // indirect
+ github.com/cloudwego/base64x v0.1.6 // indirect
+ github.com/gabriel-vasile/mimetype v1.4.12 // indirect
+ github.com/gin-contrib/sse v1.1.0 // indirect
+ github.com/go-playground/locales v0.14.1 // indirect
+ github.com/go-playground/universal-translator v0.18.1 // indirect
+ github.com/go-playground/validator/v10 v10.30.1 // indirect
+ github.com/goccy/go-json v0.10.5 // indirect
+ github.com/goccy/go-yaml v1.19.2 // indirect
+ github.com/jackc/pgpassfile v1.0.0 // indirect
+ github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
+ github.com/json-iterator/go v1.1.12 // indirect
+ github.com/klauspost/cpuid/v2 v2.3.0 // indirect
+ github.com/leodido/go-urn v1.4.0 // indirect
+ github.com/mattn/go-isatty v0.0.20 // indirect
+ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
+ github.com/modern-go/reflect2 v1.0.2 // indirect
+ github.com/pelletier/go-toml/v2 v2.2.4 // indirect
+ github.com/quic-go/qpack v0.6.0 // indirect
+ github.com/quic-go/quic-go v0.59.0 // indirect
+ github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
+ github.com/ugorji/go/codec v1.3.1 // indirect
+ go.mongodb.org/mongo-driver/v2 v2.5.0 // indirect
+ go.uber.org/atomic v1.11.0 // indirect
+ golang.org/x/arch v0.22.0 // indirect
+ golang.org/x/net v0.57.0 // indirect
+ golang.org/x/sync v0.22.0 // indirect
+ golang.org/x/sys v0.47.0 // indirect
+ golang.org/x/text v0.41.0 // indirect
+ google.golang.org/protobuf v1.36.10 // indirect
+)
diff --git a/backend/go.sum b/backend/go.sum
new file mode 100644
index 0000000..0f4291f
--- /dev/null
+++ b/backend/go.sum
@@ -0,0 +1,114 @@
+github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
+github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
+github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
+github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
+github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M=
+github.com/bytedance/gopkg v0.1.3/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM=
+github.com/bytedance/sonic v1.15.0 h1:/PXeWFaR5ElNcVE84U0dOHjiMHQOwNIx3K4ymzh/uSE=
+github.com/bytedance/sonic v1.15.0/go.mod h1:tFkWrPz0/CUCLEF4ri4UkHekCIcdnkqXw9VduqpJh0k=
+github.com/bytedance/sonic/loader v0.5.0 h1:gXH3KVnatgY7loH5/TkeVyXPfESoqSBSBEiDd5VjlgE=
+github.com/bytedance/sonic/loader v0.5.0/go.mod h1:AR4NYCk5DdzZizZ5djGqQ92eEhCCcdf5x77udYiSJRo=
+github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
+github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
+github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M=
+github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU=
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
+github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/gabriel-vasile/mimetype v1.4.12 h1:e9hWvmLYvtp846tLHam2o++qitpguFiYCKbn0w9jyqw=
+github.com/gabriel-vasile/mimetype v1.4.12/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s=
+github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w=
+github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM=
+github.com/gin-gonic/gin v1.12.0 h1:b3YAbrZtnf8N//yjKeU2+MQsh2mY5htkZidOM7O0wG8=
+github.com/gin-gonic/gin v1.12.0/go.mod h1:VxccKfsSllpKshkBWgVgRniFFAzFb9csfngsqANjnLc=
+github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
+github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
+github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
+github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
+github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
+github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
+github.com/go-playground/validator/v10 v10.30.1 h1:f3zDSN/zOma+w6+1Wswgd9fLkdwy06ntQJp0BBvFG0w=
+github.com/go-playground/validator/v10 v10.30.1/go.mod h1:oSuBIQzuJxL//3MelwSLD5hc2Tu889bF0Idm9Dg26cM=
+github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
+github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
+github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM=
+github.com/goccy/go-yaml v1.19.2/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
+github.com/golang-jwt/jwt/v5 v5.3.1 h1:kYf81DTWFe7t+1VvL7eS+jKFVWaUnK9cB1qbwn63YCY=
+github.com/golang-jwt/jwt/v5 v5.3.1/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
+github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
+github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
+github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
+github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
+github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
+github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
+github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
+github.com/jackc/pgx/v5 v5.10.0 h1:VhSvgU2jSli8o3AqIEOTJr7rZwAEUVo4E4XhR94Zfr0=
+github.com/jackc/pgx/v5 v5.10.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4=
+github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
+github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
+github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
+github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
+github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=
+github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
+github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
+github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
+github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
+github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
+github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
+github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
+github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
+github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
+github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
+github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
+github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
+github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8=
+github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII=
+github.com/quic-go/quic-go v0.59.0 h1:OLJkp1Mlm/aS7dpKgTc6cnpynnD2Xg7C1pwL6vy/SAw=
+github.com/quic-go/quic-go v0.59.0/go.mod h1:upnsH4Ju1YkqpLXC305eW3yDZ4NfnNbmQRCMWS58IKU=
+github.com/redis/go-redis/v9 v9.22.0 h1:laDvpYXTJtZLloinw1fA5Kqd6HAEH2XKxOkG/PDq2F0=
+github.com/redis/go-redis/v9 v9.22.0/go.mod h1:y2g0Wj8rQvuK0ELM+oxSudcLtC09JScs98I/X9gRWY4=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
+github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
+github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
+github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
+github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
+github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
+github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
+github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
+github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
+github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
+github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
+github.com/ugorji/go/codec v1.3.1 h1:waO7eEiFDwidsBN6agj1vJQ4AG7lh2yqXyOXqhgQuyY=
+github.com/ugorji/go/codec v1.3.1/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4=
+github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
+github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
+go.mongodb.org/mongo-driver/v2 v2.5.0 h1:yXUhImUjjAInNcpTcAlPHiT7bIXhshCTL3jVBkF3xaE=
+go.mongodb.org/mongo-driver/v2 v2.5.0/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0=
+go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
+go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
+go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y=
+go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU=
+golang.org/x/arch v0.22.0 h1:c/Zle32i5ttqRXjdLyyHZESLD/bB90DCU1g9l/0YBDI=
+golang.org/x/arch v0.22.0/go.mod h1:dNHoOeKiyja7GTvF9NJS1l3Z2yntpQNzgrjh1cU103A=
+golang.org/x/crypto v0.56.0 h1:GUh5Ii4J5jtcseSMiRqr1jXCNHoxjeV9Fmekc2oLy6Y=
+golang.org/x/crypto v0.56.0/go.mod h1:OMW5y6CY9l38uPLmxU6l6pwcXp1obtLo3e6gT7gQR2I=
+golang.org/x/net v0.57.0 h1:K5+3DljvIuDG9/Jv9rvyMywYNFCQ9RSUY6OOTTkT+tE=
+golang.org/x/net v0.57.0/go.mod h1:KpXc8iv+r3XplLAG/f7Jsf9RPszJzdR0f58q9vGOuEU=
+golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek=
+golang.org/x/sync v0.22.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
+golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
+golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
+golang.org/x/text v0.41.0 h1:vz/seA0lnX87Othu2f/0L24RcgrXD9/YFTSuGjj3rH8=
+golang.org/x/text v0.41.0/go.mod h1:jvf1O8ajNzZqhSrQBPbutR/EB83Cc0CFrezNQIwbb5M=
+google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE=
+google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
+gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
diff --git a/backend/internal/api/api.go b/backend/internal/api/api.go
new file mode 100644
index 0000000..b4ab45b
--- /dev/null
+++ b/backend/internal/api/api.go
@@ -0,0 +1,79 @@
+package api
+
+import (
+ "errors"
+ "io"
+ "log"
+ "net"
+ "net/http"
+ "strings"
+ "syscall"
+
+ "github.com/gin-gonic/gin"
+ "github.com/jackc/pgx/v5/pgconn"
+ "github.com/jackc/puddle/v2"
+
+ "booklib/internal/auth"
+ "booklib/internal/config"
+ "booklib/internal/redispkg"
+ "booklib/internal/scanner"
+ "booklib/internal/store"
+)
+
+type api struct {
+ cfg *config.Config
+ st *store.Store
+ rdb *redispkg.R
+ sc *scanner.Scanner
+}
+
+func err(c *gin.Context, status int, code, msg string) {
+ c.AbortWithStatusJSON(status, gin.H{"error": gin.H{"code": code, "message": msg}})
+}
+
+// dbErr 统一处理 store 层失败:记日志;连接类错误 503(Service Unavailable),其余 500
+// 注:brief 里的 pgxpool.ErrClosedPool 在 pgx v5 不存在,实际由 puddle 原样透出,用它替代;
+// PG 停机时池内连接先收到 SQLSTATE 57P01(administrator shutdown),故把 08xx/57Pxx 也归为 503
+func dbErr(c *gin.Context, e error) {
+ log.Printf("db: %v", e)
+ status, code := http.StatusInternalServerError, "internal"
+ var pgErr *pgconn.PgError
+ connClass := errors.As(e, &pgErr) && (strings.HasPrefix(pgErr.Code, "08") || strings.HasPrefix(pgErr.Code, "57P"))
+ if connClass || errors.Is(e, syscall.ECONNREFUSED) || errors.Is(e, io.ErrUnexpectedEOF) ||
+ errors.Is(e, net.ErrClosed) || errors.Is(e, puddle.ErrClosedPool) {
+ status, code = http.StatusServiceUnavailable, "unavailable"
+ }
+ err(c, status, code, "db error")
+}
+
+func (a *api) authMw() gin.HandlerFunc {
+ return func(c *gin.Context) {
+ h := c.GetHeader("Authorization")
+ tok, ok := strings.CutPrefix(h, "Bearer ")
+ if !ok {
+ err(c, http.StatusUnauthorized, "unauthorized", "missing bearer token")
+ return
+ }
+ cl, perr := auth.Parse(a.cfg.JWTSecret, tok)
+ if perr != nil {
+ err(c, http.StatusUnauthorized, "unauthorized", "invalid token")
+ return
+ }
+ c.Set("uid", cl.UID)
+ c.Set("role", cl.Role)
+ c.Next()
+ }
+}
+
+func (a *api) adminOnly() gin.HandlerFunc {
+ return func(c *gin.Context) {
+ if c.GetString("role") != "admin" {
+ err(c, http.StatusForbidden, "forbidden", "admin only")
+ return
+ }
+ c.Next()
+ }
+}
+
+func uid(c *gin.Context) int64 { return c.GetInt64("uid") }
+func isAdmin(c *gin.Context) bool { return c.GetString("role") == "admin" }
diff --git a/backend/internal/api/auth.go b/backend/internal/api/auth.go
new file mode 100644
index 0000000..4330d37
--- /dev/null
+++ b/backend/internal/api/auth.go
@@ -0,0 +1,57 @@
+package api
+
+import (
+ "errors"
+ "net/http"
+ "time"
+
+ "github.com/gin-gonic/gin"
+ "github.com/jackc/pgx/v5"
+
+ "booklib/internal/auth"
+)
+
+const loginWindow = time.Minute
+const loginMax = 5
+
+func (a *api) login(c *gin.Context) {
+ var req struct{ Username, Password string }
+ if c.ShouldBindJSON(&req) != nil || req.Username == "" || req.Password == "" {
+ err(c, http.StatusBadRequest, "bad_request", "username and password required")
+ return
+ }
+ if n := a.rdb.IncrWindow(c, "loginrl:"+c.ClientIP(), loginWindow); n > loginMax {
+ err(c, http.StatusTooManyRequests, "rate_limited", "too many login attempts")
+ return
+ }
+ u, qerr := a.st.GetUserByName(c, req.Username)
+ if qerr != nil {
+ if !errors.Is(qerr, pgx.ErrNoRows) {
+ dbErr(c, qerr)
+ return
+ }
+ // 用户不存在也走一次 bcrypt,防用户名枚举时序差
+ auth.CheckPassword("$2a$12$V5TmlpkEi9G/DFAmnkt9YunNdGLnnW921/5TcSo4OeE6iaaLDPN1K", req.Password)
+ err(c, http.StatusUnauthorized, "unauthorized", "bad credentials")
+ return
+ }
+ if !auth.CheckPassword(u.PasswordHash, req.Password) {
+ err(c, http.StatusUnauthorized, "unauthorized", "bad credentials")
+ return
+ }
+ tok, serr := auth.Sign(a.cfg.JWTSecret, u.ID, u.Role)
+ if serr != nil {
+ err(c, http.StatusInternalServerError, "internal", "sign")
+ return
+ }
+ c.JSON(http.StatusOK, gin.H{"token": tok})
+}
+
+func (a *api) me(c *gin.Context) {
+ u, qerr := a.st.GetUserByID(c, uid(c))
+ if qerr != nil {
+ err(c, http.StatusUnauthorized, "unauthorized", "no such user")
+ return
+ }
+ c.JSON(http.StatusOK, gin.H{"id": u.ID, "username": u.Username, "role": u.Role})
+}
diff --git a/backend/internal/api/auth_test.go b/backend/internal/api/auth_test.go
new file mode 100644
index 0000000..dbbd9bc
--- /dev/null
+++ b/backend/internal/api/auth_test.go
@@ -0,0 +1,145 @@
+package api
+
+import (
+ "bytes"
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/redis/go-redis/v9"
+
+ "booklib/internal/auth"
+ "booklib/internal/db"
+ "booklib/internal/redispkg"
+ "booklib/internal/scanner"
+ "booklib/internal/store"
+)
+
+func setupAPI(t *testing.T) (*store.Store, *scanner.Scanner, http.Handler, string) {
+ t.Helper()
+ url := os.Getenv("DATABASE_URL")
+ if url == "" {
+ t.Skip("DATABASE_URL not set")
+ }
+ ctx := context.Background()
+ p, _ := db.Connect(ctx, url)
+ if err := db.Migrate(ctx, p); err != nil {
+ t.Fatal(err)
+ }
+ st := store.New(p)
+ p.Exec(ctx, "DELETE FROM reading_progress; DELETE FROM books; DELETE FROM libraries; DELETE FROM users")
+ h, _ := auth.HashPassword(testPW)
+ if _, err := st.CreateUser(ctx, "alice", h, "admin"); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := st.CreateUser(ctx, "bob", h, "member"); err != nil {
+ t.Fatal(err)
+ }
+ booksParent := t.TempDir()
+ booksDir, err := filepath.EvalSymlinks(booksParent) // macOS 上 /var→/private,root 校验要用真实路径
+ if err != nil {
+ t.Fatal(err)
+ }
+ cfg := testCfg()
+ cfg.BooksDir = booksDir
+ cfg.CacheDir = t.TempDir()
+ rdb := redispkg.New(os.Getenv("REDIS_URL"))
+ sc := scanner.New(st, cfg, rdb)
+ r := NewRouter(cfg, st, rdb, sc)
+ if u := os.Getenv("REDIS_URL"); u != "" { // 测试卫生: 共享 redis 上重置登录限流桶, 防跨测试累计 429
+ if opt, e := redis.ParseURL(u); e == nil {
+ rc := redis.NewClient(opt)
+ rc.Del(ctx, "loginrl:192.0.2.1")
+ rc.Close()
+ }
+ }
+ return st, sc, r, booksDir
+}
+
+// testPW 是唯一的 fixture 口令常量: 直接种子 (HashPassword) 与所有登录/建户必须同值, 且 >=8 位
+const testPW = "pw123456"
+
+func do(h http.Handler, method, path, token string, body any) *httptest.ResponseRecorder {
+ var r *bytes.Reader
+ if body != nil {
+ b, _ := json.Marshal(body)
+ r = bytes.NewReader(b)
+ } else {
+ r = bytes.NewReader(nil)
+ }
+ req := httptest.NewRequest(method, path, r)
+ if token != "" {
+ req.Header.Set("Authorization", "Bearer "+token)
+ }
+ w := httptest.NewRecorder()
+ h.ServeHTTP(w, req)
+ return w
+}
+
+func TestLoginMe(t *testing.T) {
+ _, _, h, _ := setupAPI(t)
+ w := do(h, "POST", "/api/auth/login", "", map[string]string{"username": "alice", "password": testPW})
+ if w.Code != 200 {
+ t.Fatalf("login %d %s", w.Code, w.Body)
+ }
+ var tok struct{ Token string }
+ json.Unmarshal(w.Body.Bytes(), &tok)
+ if tok.Token == "" {
+ t.Fatal("no token")
+ }
+ w = do(h, "GET", "/api/auth/me", tok.Token, nil)
+ var me map[string]any
+ json.Unmarshal(w.Body.Bytes(), &me)
+ if w.Code != 200 || me["username"] != "alice" || me["role"] != "admin" {
+ t.Fatalf("me %d %s", w.Code, w.Body)
+ }
+ // 错密码 → 401 统一错误体
+ w = do(h, "POST", "/api/auth/login", "", map[string]string{"username": "alice", "password": "nope"})
+ if w.Code != 401 {
+ t.Fatalf("want 401 got %d", w.Code)
+ }
+ // 无 token / 坏 token 访问受保护端点 → 401(me 已注册;books 路由 Task 10 才有)
+ if w = do(h, "GET", "/api/auth/me", "", nil); w.Code != 401 {
+ t.Fatal("me without token must 401")
+ }
+ if w = do(h, "GET", "/api/auth/me", "garbage", nil); w.Code != 401 {
+ t.Fatal("me with bad token must 401")
+ }
+}
+
+// 伪造 XFF 换不了限流桶: peer(192.0.2.1)不在可信代理段 → ClientIP 取 peer,XFF 忽略
+func TestLoginRateLimitResistsXFFSpoof(t *testing.T) {
+ if os.Getenv("REDIS_URL") == "" {
+ t.Skip("REDIS_URL not set (no redis → IncrWindow always allows)")
+ }
+ _, _, h, _ := setupAPI(t) // setupAPI 已重置 loginrl:192.0.2.1
+ for i := 1; i <= 6; i++ {
+ req := httptest.NewRequest("POST", "/api/auth/login", bytes.NewBufferString(`{"username":"alice","password":"nope"}`))
+ req.Header.Set("X-Forwarded-For", fmt.Sprintf("203.0.113.%d", i))
+ w := httptest.NewRecorder()
+ h.ServeHTTP(w, req)
+ if i < 6 && w.Code != http.StatusUnauthorized {
+ t.Fatalf("attempt %d: want 401 got %d %s", i, w.Code, w.Body)
+ }
+ if i == 6 && w.Code != http.StatusTooManyRequests {
+ t.Fatalf("attempt 6: spoofed XFF escaped per-peer limit: want 429 got %d", w.Code)
+ }
+ }
+}
+
+func TestMemberCannotWriteUsers(t *testing.T) {
+ _, _, h, _ := setupAPI(t)
+ w := do(h, "POST", "/api/auth/login", "", map[string]string{"username": "bob", "password": testPW})
+ var v struct{ Token string }
+ json.Unmarshal(w.Body.Bytes(), &v)
+ tok := v.Token
+ w = do(h, "POST", "/api/users", tok, map[string]string{"username": "eve", "password": testPW, "role": "member"})
+ if w.Code != 403 {
+ t.Fatalf("member write users: want 403 got %d", w.Code)
+ }
+}
diff --git a/backend/internal/api/books.go b/backend/internal/api/books.go
new file mode 100644
index 0000000..00cc97a
--- /dev/null
+++ b/backend/internal/api/books.go
@@ -0,0 +1,152 @@
+package api
+
+import (
+ "errors"
+ "fmt"
+ "net/http"
+ "os"
+ "path/filepath"
+ "strconv"
+ "strings"
+ "time"
+
+ "github.com/gin-gonic/gin"
+ "github.com/jackc/pgx/v5"
+
+ "booklib/internal/bookfile"
+ "booklib/internal/store"
+)
+
+func (a *api) getBookRow(c *gin.Context, id int64) (store.Book, bool) {
+ b, e := a.st.GetBook(c, id)
+ if e != nil {
+ if errors.Is(e, pgx.ErrNoRows) {
+ err(c, http.StatusNotFound, "not_found", "no such book")
+ return store.Book{}, false
+ }
+ dbErr(c, e)
+ return store.Book{}, false
+ }
+ return b, true
+}
+
+func (a *api) bookFromParam(c *gin.Context) (store.Book, bool) {
+ id, e := strconv.ParseInt(c.Param("id"), 10, 64)
+ if e != nil {
+ err(c, http.StatusBadRequest, "bad_request", "bad id")
+ return store.Book{}, false
+ }
+ return a.getBookRow(c, id)
+}
+
+func (a *api) getLibRow(c *gin.Context, id int64) (store.Library, bool) {
+ l, e := a.st.GetLibrary(c, id)
+ if e != nil {
+ if errors.Is(e, pgx.ErrNoRows) {
+ err(c, http.StatusNotFound, "not_found", "no such library")
+ return store.Library{}, false
+ }
+ dbErr(c, e)
+ return store.Library{}, false
+ }
+ return l, true
+}
+
+// absBookPath: books.path 永远相对且不含 ..;拼接后二次前缀校验(纵深防御)
+func absBookPath(root string, b store.Book) (string, error) {
+ abs := filepath.Join(root, filepath.FromSlash(b.Path))
+ if filepath.Clean(abs) != abs || !hasPrefixDir(abs, root) {
+ return "", os.ErrPermission
+ }
+ return abs, nil
+}
+
+func hasPrefixDir(p, dir string) bool {
+ rel, err := filepath.Rel(filepath.Clean(dir), filepath.Clean(p))
+ return err == nil && rel != ".." && !strings.HasPrefix(rel, ".."+string(os.PathSeparator))
+}
+
+func bookJSON(b store.Book, percent float64, libraryName string) gin.H {
+ h := bookfile.Hash(b.FileSize, b.ModTS)
+ j := gin.H{
+ "id": b.ID, "library_id": b.LibraryID, "path": b.Path, "title": b.Title,
+ "format": b.Format, "size": b.FileSize, "mtime": b.ModTS, "pages": b.PageCount,
+ "state": b.State, "error": b.ErrMsg, "added_at": b.AddedAt.Format(time.RFC3339),
+ "percent": percent,
+ "cover_url": fmt.Sprintf("/api/books/%d/cover?v=%s", b.ID, h),
+ }
+ if b.Format == "cbz" {
+ j["pages_url"] = fmt.Sprintf("/api/books/%d/pages", b.ID)
+ j["page_url_fmt"] = fmt.Sprintf("/api/books/%d/pages/%%d?v=%s", b.ID, h)
+ } else {
+ j["file_url"] = fmt.Sprintf("/api/books/%d/file?v=%s", b.ID, h)
+ }
+ if libraryName != "" {
+ j["library"] = libraryName
+ }
+ return j
+}
+
+func (a *api) listBooks(c *gin.Context) {
+ libID, _ := strconv.ParseInt(c.Query("library"), 10, 64)
+ views, e := a.st.ListBooks(c, libID, c.Query("q"), c.Query("prefix"), uid(c))
+ if e != nil {
+ dbErr(c, e)
+ return
+ }
+ out := make([]gin.H, 0, len(views))
+ for _, v := range views {
+ out = append(out, bookJSON(v.Book, v.Percent, v.LibraryName))
+ }
+ c.JSON(http.StatusOK, out)
+}
+
+func (a *api) getBook(c *gin.Context) {
+ b, ok := a.bookFromParam(c)
+ if !ok {
+ return
+ }
+ p, e := a.st.GetProgress(c, uid(c), b.LibraryID, b.Path) // ErrNoRows → 零值 percent
+ if e != nil && !errors.Is(e, pgx.ErrNoRows) {
+ dbErr(c, e)
+ return
+ }
+ lib, e := a.st.GetLibrary(c, b.LibraryID)
+ if e != nil && !errors.Is(e, pgx.ErrNoRows) { // 库被并发删则留空 library 名,书仍可见
+ dbErr(c, e)
+ return
+ }
+ c.JSON(http.StatusOK, bookJSON(b, p.Percent, lib.Name))
+}
+
+func (a *api) deleteBook(c *gin.Context) {
+ b, ok := a.bookFromParam(c)
+ if !ok {
+ return
+ }
+ lib, ok := a.getLibRow(c, b.LibraryID)
+ if !ok {
+ return
+ }
+ root, ok := a.libRoot(c, lib)
+ if !ok {
+ return
+ }
+ abs, e := absBookPath(root, b)
+ if e != nil {
+ err(c, http.StatusForbidden, "forbidden", "unsafe path")
+ return
+ }
+ if e := os.Remove(abs); e != nil && !os.IsNotExist(e) {
+ err(c, http.StatusInternalServerError, "internal", "remove file")
+ return
+ }
+ key := bookfile.DirKey(b.ID, bookfile.Hash(b.FileSize, b.ModTS))
+ os.RemoveAll(bookfile.CoverDir(a.cfg.CacheDir, key))
+ os.RemoveAll(bookfile.PagesDir(a.cfg.CacheDir, key))
+ if e := a.st.DeleteBook(c, b.ID); e != nil {
+ dbErr(c, e)
+ return
+ }
+ c.Status(http.StatusNoContent)
+}
diff --git a/backend/internal/api/books_test.go b/backend/internal/api/books_test.go
new file mode 100644
index 0000000..042dbf0
--- /dev/null
+++ b/backend/internal/api/books_test.go
@@ -0,0 +1,174 @@
+package api
+
+import (
+ "archive/zip"
+ "bytes"
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "booklib/internal/scanner"
+ "booklib/internal/store"
+)
+
+func newLibrary(t *testing.T, st *store.Store, h http.Handler, tok, booksDir, name string) (store.Library, string) {
+ t.Helper()
+ root := filepath.Join(booksDir, name)
+ os.MkdirAll(filepath.Join(root, "series-a"), 0o755)
+ w := do(h, "POST", "/api/libraries", tok, map[string]string{"name": name, "root_path": root})
+ if w.Code != 201 {
+ t.Fatalf("create lib %d %s", w.Code, w.Body)
+ }
+ var v struct {
+ ID int64 `json:"id"`
+ }
+ json.Unmarshal(w.Body.Bytes(), &v)
+ lib, err := st.GetLibrary(context.Background(), v.ID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ return lib, root
+}
+
+func writeCBZ(t *testing.T, path string, pages int) {
+ t.Helper()
+ os.MkdirAll(filepath.Dir(path), 0o755)
+ buf := &bytes.Buffer{}
+ zw := zip.NewWriter(buf)
+ for i := 1; i <= pages; i++ {
+ w, _ := zw.Create(i2name(i))
+ w.Write(bytes.Repeat([]byte("IMG"), 64))
+ }
+ zw.Close()
+ os.WriteFile(path, buf.Bytes(), 0o644)
+}
+func i2name(i int) string { return fmt.Sprintf("%02d.jpg", i) }
+
+func scanNow(t *testing.T, sc *scanner.Scanner, lib store.Library) {
+ t.Helper()
+ sc.ScanLibrary(context.Background(), lib)
+}
+
+func TestBookListDetailDelete(t *testing.T) {
+ st, sc, h, booksDir := setupAPI(t)
+ atok := adminToken(t, h)
+
+ // member token + 进度前置数据
+ do(h, "POST", "/api/users", atok, map[string]string{"username": "m2", "password": testPW, "role": "member"})
+ mtok := loginAs(t, h, "m2", testPW)
+
+ lib, root := newLibrary(t, st, h, atok, booksDir, "comics")
+ writeCBZ(t, filepath.Join(root, "series-a", "vol_01.cbz"), 4)
+ os.WriteFile(filepath.Join(root, "readme.txt"), []byte("hello world"), 0o644)
+ scanNow(t, sc, lib)
+
+ w := do(h, "GET", "/api/books", mtok, nil)
+ var books []map[string]any
+ json.Unmarshal(w.Body.Bytes(), &books)
+ if w.Code != 200 || len(books) != 2 {
+ t.Fatalf("list %d %s", w.Code, w.Body)
+ }
+ var cbz map[string]any
+ for _, b := range books {
+ if b["format"] == "cbz" {
+ cbz = b
+ }
+ }
+ if cbz == nil {
+ t.Fatal("cbz missing")
+ }
+ if cbz["pages"].(float64) != 4 || cbz["library"] != "comics" || cbz["percent"].(float64) != 0 {
+ t.Fatalf("bad json %+v", cbz)
+ }
+ if !strings.HasPrefix(cbz["cover_url"].(string), "/api/books/") || !strings.Contains(cbz["cover_url"].(string), "?v=") {
+ t.Fatalf("cover_url %+v", cbz["cover_url"])
+ }
+ if cbz["page_url_fmt"] == nil {
+ t.Fatalf("cbz must have page_url_fmt: %+v", cbz)
+ }
+ id := itoa(cbz["id"])
+
+ // detail
+ w = do(h, "GET", "/api/books/"+id, mtok, nil)
+ if w.Code != 200 {
+ t.Fatalf("detail %d", w.Code)
+ }
+ // 进度联动的断言在 Task 12(progress 端点此任务还不存在)
+
+ // member 不能删
+ w = do(h, "DELETE", "/api/books/"+id, mtok, nil)
+ if w.Code != 403 {
+ t.Fatalf("member delete want 403 got %d", w.Code)
+ }
+ // admin 删:行、文件、缓存目录都没;txt 文件保留
+ w = do(h, "DELETE", "/api/books/"+id, atok, nil)
+ if w.Code != 204 {
+ t.Fatalf("admin delete %d %s", w.Code, w.Body)
+ }
+ if _, err := os.Stat(filepath.Join(root, "series-a", "vol_01.cbz")); !os.IsNotExist(err) {
+ t.Fatal("file not removed")
+ }
+ w = do(h, "GET", "/api/books", atok, nil)
+ json.Unmarshal(w.Body.Bytes(), &books)
+ if len(books) != 1 || books[0]["format"] != "txt" {
+ t.Fatalf("books after delete %+v", books)
+ }
+ // 过滤器
+ w = do(h, "GET", "/api/books?library="+itoa(lib.ID)+"&q=readme&prefix=series/", atok, nil)
+ json.Unmarshal(w.Body.Bytes(), &books)
+ if len(books) != 0 {
+ t.Fatalf("prefix+q filter broken %+v", books)
+ }
+ w = do(h, "GET", "/api/books?q=readme", atok, nil)
+ json.Unmarshal(w.Body.Bytes(), &books)
+ if len(books) != 1 {
+ t.Fatalf("q broken %+v", books)
+ }
+}
+
+func loginAs(t *testing.T, h http.Handler, user, pass string) string {
+ t.Helper()
+ w := do(h, "POST", "/api/auth/login", "", map[string]string{"username": user, "password": pass})
+ if w.Code != 200 {
+ t.Fatalf("login %s: %d", user, w.Code)
+ }
+ var v struct {
+ Token string
+ }
+ json.Unmarshal(w.Body.Bytes(), &v)
+ return v.Token
+}
+
+func TestDeleteUnsafePath403(t *testing.T) {
+ st, _, h, booksDir := setupAPI(t)
+ atok := adminToken(t, h)
+ lib, _ := newLibrary(t, st, h, atok, booksDir, "libs")
+ id, e := st.InsertBook(context.Background(), lib.ID, "../../x.cbz", "x", "cbz", 1, 1, 0)
+ if e != nil {
+ t.Fatal(e)
+ }
+ w := do(h, "DELETE", "/api/books/"+itoa(id), atok, nil)
+ if w.Code != 403 {
+ t.Fatalf("unsafe delete want 403 got %d %s", w.Code, w.Body)
+ }
+ if _, e := st.GetBook(context.Background(), id); e != nil { // 403 提前返回,行必须保留
+ t.Fatalf("row must survive: %v", e)
+ }
+}
+
+func TestAbsBookPathTraversalRejected(t *testing.T) {
+ root := "/data/books/lib" // 纯路径逻辑,不碰文件系统,无需 DB
+ for _, bad := range []string{"../../etc/passwd", "a/../../../etc/x", "../sibling"} {
+ if _, e := absBookPath(root, store.Book{Path: bad}); e == nil {
+ t.Fatalf("must reject %q", bad)
+ }
+ }
+ if p, e := absBookPath(root, store.Book{Path: "series-a/vol.cbz"}); e != nil || p != filepath.Join(root, "series-a", "vol.cbz") {
+ t.Fatalf("must accept relative path: %q %v", p, e)
+ }
+}
diff --git a/backend/internal/api/content.go b/backend/internal/api/content.go
new file mode 100644
index 0000000..ff26de0
--- /dev/null
+++ b/backend/internal/api/content.go
@@ -0,0 +1,226 @@
+package api
+
+import (
+ "fmt"
+ "net/http"
+ "os"
+ "path/filepath"
+ "strconv"
+ "strings"
+ "time"
+
+ "github.com/gin-gonic/gin"
+
+ "booklib/internal/bookfile"
+ "booklib/internal/store"
+)
+
+const defaultCover = ``
+
+func (a *api) bookRoot(c *gin.Context, b store.Book) (string, bool) {
+ lib, ok := a.getLibRow(c, b.LibraryID)
+ if !ok {
+ return "", false
+ }
+ return a.libRoot(c, lib)
+}
+
+func (a *api) immutable(c *gin.Context) {
+ c.Header("Cache-Control", "public, max-age=31536000, immutable")
+}
+
+func (a *api) serveCover(c *gin.Context) {
+ b, ok := a.bookFromParam(c)
+ if !ok {
+ return
+ }
+ a.immutable(c)
+ dir := bookfile.CoverDir(a.cfg.CacheDir, bookfile.DirKey(b.ID, bookfile.Hash(b.FileSize, b.ModTS)))
+ if entries, e := os.ReadDir(dir); e == nil {
+ for _, en := range entries { // 跳过写一半的 .tmp 落盘中间态
+ if !strings.Contains(en.Name(), ".tmp") {
+ http.ServeFile(c.Writer, c.Request, filepath.Join(dir, en.Name()))
+ return
+ }
+ }
+ }
+ if b.Format == "cbz" || b.Format == "epub" { // 自愈:缓存丢了就地抽封面(重启/卷漂移/扫描器还没跑到)
+ if root, ok := a.bookRoot(c, b); ok {
+ if f, size, ok := a.openBook(c, b, root); ok {
+ defer f.Close()
+ var img []byte
+ var ext string
+ var e error
+ if b.Format == "cbz" {
+ img, ext, e = bookfile.CBZCover(f, size)
+ } else {
+ img, ext, e = bookfile.EPUBCover(f, size)
+ }
+ if e == nil {
+ dst := filepath.Join(dir, "cover"+ext)
+ if e := os.MkdirAll(dir, 0o755); e == nil {
+ tmp := fmt.Sprintf("%s.tmp-%d", dst, time.Now().UnixNano()) // 并发幂等:唯一 tmp + rename 原子
+ if e := os.WriteFile(tmp, img, 0o644); e == nil {
+ if e := os.Rename(tmp, dst); e == nil {
+ http.ServeFile(c.Writer, c.Request, dst)
+ }
+ }
+ os.Remove(tmp)
+ }
+ }
+ }
+ }
+ }
+ if c.Writer.Written() { // openBook/bookRoot 已写 403/404/500,不再叠加占位图
+ return
+ }
+ c.Data(http.StatusOK, "image/svg+xml", []byte(defaultCover))
+}
+
+func (a *api) serveFile(c *gin.Context) {
+ b, ok := a.bookFromParam(c)
+ if !ok {
+ return
+ }
+ root, ok := a.bookRoot(c, b)
+ if !ok {
+ return
+ }
+ abs, perr := absBookPath(root, b)
+ if perr != nil {
+ err(c, http.StatusForbidden, "forbidden", "unsafe path")
+ return
+ }
+ c.Header("ETag", `"`+bookfile.Hash(b.FileSize, b.ModTS)+`"`)
+ c.Header("Cache-Control", "private, must-revalidate")
+ http.ServeFile(c.Writer, c.Request, abs)
+}
+
+func (a *api) openBook(c *gin.Context, b store.Book, root string) (*os.File, int64, bool) {
+ abs, perr := absBookPath(root, b)
+ if perr != nil {
+ err(c, http.StatusForbidden, "forbidden", "unsafe path")
+ return nil, 0, false
+ }
+ f, perr := os.Open(abs)
+ if perr != nil {
+ err(c, http.StatusNotFound, "not_found", "file missing on disk")
+ return nil, 0, false
+ }
+ st, perr := f.Stat()
+ if perr != nil {
+ f.Close()
+ err(c, http.StatusInternalServerError, "internal", "stat")
+ return nil, 0, false
+ }
+ return f, st.Size(), true
+}
+
+func (a *api) pageIndex(c *gin.Context, b store.Book, root string) ([]string, error) {
+ hash := bookfile.Hash(b.FileSize, b.ModTS)
+ key := fmt.Sprintf("pagesidx:%d:%s", b.ID, hash)
+ if v, ok := a.rdb.Get(c, key); ok && v != "" {
+ return strings.Split(v, "\n"), nil
+ }
+ f, size, ok := a.openBook(c, b, root)
+ if !ok {
+ return nil, os.ErrNotExist
+ }
+ defer f.Close()
+ idx, e := bookfile.PageIndex(f, size)
+ if e != nil {
+ return nil, e
+ }
+ if len(idx) > 0 { // 空索引不缓存,否则 warm 命中 "" 会 Split 出幽灵页
+ a.rdb.Set(c, key, strings.Join(idx, "\n"), 7*24*time.Hour)
+ }
+ return idx, nil
+}
+
+func (a *api) pagesCount(c *gin.Context) {
+ b, ok := a.bookFromParam(c)
+ if !ok {
+ return
+ }
+ if b.Format != "cbz" {
+ err(c, http.StatusBadRequest, "bad_request", "pages only for cbz")
+ return
+ }
+ root, ok := a.bookRoot(c, b)
+ if !ok {
+ return
+ }
+ idx, e := a.pageIndex(c, b, root)
+ if e != nil {
+ if c.Writer.Written() {
+ return // openBook 已写 403/404,不再叠加 422
+ }
+ err(c, http.StatusUnprocessableEntity, "broken", e.Error())
+ return
+ }
+ c.JSON(http.StatusOK, gin.H{"count": len(idx)})
+}
+
+func (a *api) page(c *gin.Context) {
+ b, ok := a.bookFromParam(c)
+ if !ok {
+ return
+ }
+ if b.Format != "cbz" {
+ err(c, http.StatusBadRequest, "bad_request", "pages only for cbz")
+ return
+ }
+ n, e := strconv.Atoi(c.Param("n"))
+ if e != nil || n < 0 {
+ err(c, http.StatusBadRequest, "bad_request", "bad page number")
+ return
+ }
+ root, ok := a.bookRoot(c, b)
+ if !ok {
+ return
+ }
+ idx, e := a.pageIndex(c, b, root)
+ if e != nil {
+ if c.Writer.Written() {
+ return // openBook 已写 403/404,不再叠加 422
+ }
+ err(c, http.StatusUnprocessableEntity, "broken", e.Error())
+ return
+ }
+ if n >= len(idx) {
+ err(c, http.StatusNotFound, "not_found", "no such page")
+ return
+ }
+ ext := strings.ToLower(filepath.Ext(idx[n]))
+ dir := bookfile.PagesDir(a.cfg.CacheDir, bookfile.DirKey(b.ID, bookfile.Hash(b.FileSize, b.ModTS)))
+ dst := filepath.Join(dir, strconv.Itoa(n)+ext)
+ if _, e := os.Stat(dst); e != nil { // miss → 解压落盘(并发重做同页幂等,唯一 tmp 名 + rename 原子)
+ f, size, ok := a.openBook(c, b, root)
+ if !ok {
+ return
+ }
+ defer f.Close()
+ data, e := bookfile.ReadEntry(f, size, idx[n])
+ if e != nil {
+ err(c, http.StatusInternalServerError, "internal", "extract page")
+ return
+ }
+ if e := os.MkdirAll(dir, 0o755); e != nil {
+ err(c, http.StatusInternalServerError, "internal", "cache dir")
+ return
+ }
+ tmp := fmt.Sprintf("%s.tmp-%d", dst, time.Now().UnixNano())
+ if e := os.WriteFile(tmp, data, 0o644); e != nil {
+ os.Remove(tmp)
+ err(c, http.StatusInternalServerError, "internal", "write cache")
+ return
+ }
+ if e := os.Rename(tmp, dst); e != nil {
+ os.Remove(tmp)
+ err(c, http.StatusInternalServerError, "internal", "rename cache")
+ return
+ }
+ }
+ a.immutable(c)
+ http.ServeFile(c.Writer, c.Request, dst)
+}
diff --git a/backend/internal/api/content_test.go b/backend/internal/api/content_test.go
new file mode 100644
index 0000000..f7c814e
--- /dev/null
+++ b/backend/internal/api/content_test.go
@@ -0,0 +1,150 @@
+package api
+
+import (
+ "context"
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+)
+
+func serveFixture(t *testing.T) (http.Handler, string, string) {
+ st, sc, h, booksDir := setupAPI(t)
+ atok := adminToken(t, h)
+ lib, root := newLibrary(t, st, h, atok, booksDir, "comics")
+ writeCBZ(t, filepath.Join(root, "s", "one.cbz"), 3)
+ os.WriteFile(filepath.Join(root, "two.txt"), []byte("plain text body"), 0o644)
+ scanNow(t, sc, lib)
+ w := do(h, "GET", "/api/books?q=one", atok, nil)
+ var bs []map[string]any
+ json.Unmarshal(w.Body.Bytes(), &bs)
+ cbzID := itoa(bs[0]["id"])
+ w = do(h, "GET", "/api/books?q=two", atok, nil) // 检索走 title(文件名去扩展名),不是 path
+ json.Unmarshal(w.Body.Bytes(), &bs)
+ txtID := itoa(bs[0]["id"])
+ return h, cbzID, txtID
+}
+
+func TestCoverCBZAndPlaceholder(t *testing.T) {
+ h, cbzID, txtID := serveFixture(t)
+ tok := adminToken(t, h)
+ w := do(h, "GET", "/api/books/"+cbzID+"/cover", tok, nil)
+ if w.Code != 200 || !strings.Contains(w.Header().Get("Content-Type"), "image/") {
+ t.Fatalf("cbz cover %d %s %q", w.Code, w.Body, w.Header().Get("Content-Type"))
+ }
+ if !strings.Contains(w.Header().Get("Cache-Control"), "immutable") {
+ t.Fatal("cover must be immutable")
+ }
+ w = do(h, "GET", "/api/books/"+txtID+"/cover", tok, nil)
+ if w.Code != 200 || w.Header().Get("Content-Type") != "image/svg+xml" {
+ t.Fatalf("placeholder cover %d %q", w.Code, w.Header().Get("Content-Type"))
+ }
+ w = do(h, "GET", "/api/books/999999/cover", tok, nil)
+ if w.Code != 404 {
+ t.Fatalf("missing book cover want 404 got %d", w.Code)
+ }
+}
+
+// 不经 scanner 直接入库(模拟缓存丢失/卷漂移)→ GET cover 就地抽图自愈并落盘(tmp+rename)
+func TestCoverSelfHeal(t *testing.T) {
+ st, _, h, booksDir := setupAPI(t)
+ atok := adminToken(t, h)
+ lib, root := newLibrary(t, st, h, atok, booksDir, "comics")
+ p := filepath.Join(root, "heal.cbz")
+ writeCBZ(t, p, 2)
+ fi, err := os.Stat(p)
+ if err != nil {
+ t.Fatal(err)
+ }
+ id, err := st.InsertBook(context.Background(), lib.ID, "heal.cbz", "heal", "cbz", fi.Size(), fi.ModTime().Unix(), 2)
+ if err != nil {
+ t.Fatal(err)
+ }
+ for i := 1; i <= 2; i++ { // 第二次走已缓存目录,均应 200 image/*
+ w := do(h, "GET", "/api/books/"+itoa(id)+"/cover", atok, nil)
+ if w.Code != 200 || !strings.Contains(w.Header().Get("Content-Type"), "image/") {
+ t.Fatalf("self-heal cover pass %d: %d %q", i, w.Code, w.Header().Get("Content-Type"))
+ }
+ }
+}
+
+func TestPages(t *testing.T) {
+ h, cbzID, txtID := serveFixture(t)
+ tok := adminToken(t, h)
+ w := do(h, "GET", "/api/books/"+cbzID+"/pages", tok, nil)
+ var v struct{ Count int }
+ json.Unmarshal(w.Body.Bytes(), &v)
+ if w.Code != 200 || v.Count != 3 {
+ t.Fatalf("pages %d %s", w.Code, w.Body)
+ }
+ w = do(h, "GET", "/api/books/"+cbzID+"/pages/1", tok, nil)
+ if w.Code != 200 || !strings.Contains(w.Body.String(), "IMG") {
+ t.Fatalf("page 1 %d", w.Code)
+ }
+ if !strings.Contains(w.Header().Get("Cache-Control"), "immutable") {
+ t.Fatal("page must be immutable")
+ }
+ for _, bad := range []string{"4", "-1", "abc"} {
+ if w = do(h, "GET", "/api/books/"+cbzID+"/pages/"+bad, tok, nil); w.Code != 404 && w.Code != 400 {
+ t.Fatalf("pages/%s want 404/400 got %d", bad, w.Code)
+ }
+ }
+ if w = do(h, "GET", "/api/books/"+txtID+"/pages", tok, nil); w.Code != 400 {
+ t.Fatalf("pages on txt want 400 got %d", w.Code)
+ }
+ // 二次命中磁盘缓存(服务仍 200,字节一致)
+ w2 := do(h, "GET", "/api/books/"+cbzID+"/pages/2", tok, nil)
+ w3 := do(h, "GET", "/api/books/"+cbzID+"/pages/2", tok, nil)
+ if w2.Code != 200 || w2.Body.String() != w3.Body.String() {
+ t.Fatal("page cache inconsistent")
+ }
+}
+
+func TestBrokenCBZ(t *testing.T) {
+ st, sc, h, booksDir := setupAPI(t)
+ atok := adminToken(t, h)
+ lib, root := newLibrary(t, st, h, atok, booksDir, "comics")
+ os.MkdirAll(filepath.Join(root, "b"), 0o755)
+ os.WriteFile(filepath.Join(root, "b", "bad.cbz"), []byte("not a zip at all"), 0o644)
+ scanNow(t, sc, lib)
+ w := do(h, "GET", "/api/books?q=bad", atok, nil)
+ var bs []map[string]any
+ json.Unmarshal(w.Body.Bytes(), &bs)
+ id := itoa(bs[0]["id"])
+ if w = do(h, "GET", "/api/books/"+id+"/pages", atok, nil); w.Code != 422 {
+ t.Fatalf("broken pages want 422 got %d", w.Code)
+ }
+ // 封面目录缺失(坏 cbz 抽不出封面)→ 占位 SVG 兜底
+ if w = do(h, "GET", "/api/books/"+id+"/cover", atok, nil); w.Code != 200 || w.Header().Get("Content-Type") != "image/svg+xml" {
+ t.Fatalf("broken cover want placeholder svg got %d %q", w.Code, w.Header().Get("Content-Type"))
+ }
+}
+
+func TestFileRangeETag(t *testing.T) {
+ h, _, txtID := serveFixture(t)
+ tok := adminToken(t, h)
+ w := do(h, "GET", "/api/books/"+txtID+"/file", tok, nil)
+ if w.Code != 200 || w.Body.String() != "plain text body" {
+ t.Fatalf("file %d %q", w.Code, w.Body)
+ }
+ if w.Header().Get("ETag") == "" {
+ t.Fatal("no etag")
+ }
+ if etag := w.Header().Get("ETag"); !strings.HasPrefix(etag, `"`) || !strings.HasSuffix(etag, `"`) {
+ t.Fatalf("etag must be quoted: %q", etag)
+ }
+ if w.Header().Get("Accept-Ranges") != "bytes" {
+ t.Fatal("no accept-ranges")
+ }
+ req := httptest.NewRequest("GET", "/api/books/"+txtID+"/file", nil)
+ req.Header.Set("Range", "bytes=0-4")
+ req.Header.Set("Authorization", "Bearer "+tok)
+ ww := httptest.NewRecorder()
+ h.ServeHTTP(ww, req)
+ if ww.Code != 206 || ww.Body.String() != "plain" {
+ t.Fatalf("range %d %q", ww.Code, ww.Body)
+ }
+}
diff --git a/backend/internal/api/libraries.go b/backend/internal/api/libraries.go
new file mode 100644
index 0000000..78e20cc
--- /dev/null
+++ b/backend/internal/api/libraries.go
@@ -0,0 +1,176 @@
+package api
+
+import (
+ "context"
+ "errors"
+ "io"
+ "net/http"
+ "os"
+ "path/filepath"
+ "strconv"
+ "strings"
+ "time"
+
+ "github.com/gin-gonic/gin"
+ "github.com/jackc/pgx/v5"
+
+ "booklib/internal/bookfile"
+ "booklib/internal/store"
+)
+
+// resolveLibRoot: root_path 必须绝对且落在 BooksDir 内(spec §7 前缀校验)
+func (a *api) libRoot(c *gin.Context, lib store.Library) (string, bool) {
+ root := filepath.Clean(lib.RootPath)
+ books := filepath.Clean(a.cfg.BooksDir)
+ if !filepath.IsAbs(root) || (root != books && !strings.HasPrefix(root, books+string(os.PathSeparator))) {
+ err(c, http.StatusForbidden, "forbidden", "library root outside books dir")
+ return "", false
+ }
+ return root, true
+}
+
+func (a *api) listLibraries(c *gin.Context) {
+ libs, e := a.st.ListLibraries(c)
+ if e != nil {
+ dbErr(c, e)
+ return
+ }
+ out := make([]gin.H, 0, len(libs))
+ for _, l := range libs {
+ out = append(out, gin.H{"id": l.ID, "name": l.Name, "root_path": l.RootPath,
+ "created_at": l.CreatedAt.Format(time.RFC3339)})
+ }
+ c.JSON(http.StatusOK, out)
+}
+
+func (a *api) createLibrary(c *gin.Context) {
+ var req struct {
+ Name string `json:"name"`
+ RootPath string `json:"root_path"`
+ }
+ if c.ShouldBindJSON(&req) != nil || req.Name == "" || req.RootPath == "" {
+ err(c, http.StatusBadRequest, "bad_request", "name and root_path required")
+ return
+ }
+ if !filepath.IsAbs(req.RootPath) {
+ err(c, http.StatusBadRequest, "bad_request", "root_path must be absolute")
+ return
+ }
+ id, e := a.st.CreateLibrary(c, req.Name, filepath.Clean(req.RootPath))
+ if e != nil {
+ if isUnique(e) {
+ err(c, http.StatusConflict, "exists", "root_path taken")
+ return
+ }
+ dbErr(c, e)
+ return
+ }
+ c.JSON(http.StatusCreated, gin.H{"id": id, "name": req.Name, "root_path": filepath.Clean(req.RootPath)})
+}
+
+func (a *api) getLibrary(c *gin.Context) (store.Library, bool) {
+ id, e := strconv.ParseInt(c.Param("id"), 10, 64)
+ if e != nil {
+ err(c, http.StatusBadRequest, "bad_request", "bad id")
+ return store.Library{}, false
+ }
+ lib, e := a.st.GetLibrary(c, id)
+ if e != nil {
+ if errors.Is(e, pgx.ErrNoRows) {
+ err(c, http.StatusNotFound, "not_found", "no such library")
+ return store.Library{}, false
+ }
+ dbErr(c, e)
+ return store.Library{}, false
+ }
+ return lib, true
+}
+
+func (a *api) scanLibrary(c *gin.Context) {
+ lib, ok := a.getLibrary(c)
+ if !ok {
+ return
+ }
+ if _, ok := a.libRoot(c, lib); !ok {
+ return
+ }
+ go a.sc.ScanLibraryByID(context.WithoutCancel(c), lib.ID)
+ c.JSON(http.StatusAccepted, gin.H{"accepted": true})
+}
+
+func (a *api) upload(c *gin.Context) {
+ lib, ok := a.getLibrary(c)
+ if !ok {
+ return
+ }
+ root, ok := a.libRoot(c, lib)
+ if !ok {
+ return
+ }
+ c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, a.cfg.UploadMaxMB<<20)
+ fh, e := c.FormFile("file")
+ if e != nil {
+ err(c, http.StatusBadRequest, "bad_request", "multipart field 'file' required")
+ return
+ }
+ name := bookfile.SafeName(fh.Filename)
+ if bookfile.FormatFromExt(name) == "" {
+ err(c, http.StatusBadRequest, "bad_format", "extension must be cbz/pdf/epub/txt/md")
+ return
+ }
+ dst, e := a.uniquePath(root, name)
+ if e != nil {
+ err(c, http.StatusForbidden, "forbidden", e.Error())
+ return
+ }
+ src, e := fh.Open()
+ if e != nil {
+ err(c, http.StatusInternalServerError, "internal", "open upload")
+ return
+ }
+ defer src.Close()
+ tmp := dst + ".upload-" + strconv.FormatInt(time.Now().UnixNano(), 36)
+ out, e := os.OpenFile(tmp, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644)
+ if e != nil {
+ err(c, http.StatusInternalServerError, "internal", "create tmp")
+ return
+ }
+ if _, e := io.Copy(out, src); e != nil {
+ out.Close()
+ os.Remove(tmp)
+ err(c, http.StatusRequestEntityTooLarge, "too_large", "upload failed")
+ return
+ }
+ out.Close()
+ if e := os.Rename(tmp, dst); e != nil { // 原子落盘,scanner 自动收编
+ os.Remove(tmp)
+ err(c, http.StatusInternalServerError, "internal", "rename")
+ return
+ }
+ c.JSON(http.StatusAccepted, gin.H{"accepted": true, "path": strings.TrimPrefix(dst, root+string(os.PathSeparator))})
+}
+
+// uniquePath 清洗后的 name 必须仍在 root 内;重名加 " (n)" 后缀
+func (a *api) uniquePath(root, name string) (string, error) {
+ ext := filepath.Ext(name)
+ base := strings.TrimSuffix(name, ext)
+ for i := 0; ; i++ {
+ cand := base + ext
+ if i > 0 {
+ cand = base + " (" + strconv.Itoa(i) + ")" + ext
+ }
+ p := filepath.Join(root, cand)
+ if filepath.Clean(p) != filepath.Join(root, filepath.Clean(cand)) ||
+ !strings.HasPrefix(filepath.Clean(p), root+string(os.PathSeparator)) {
+ return "", os.ErrInvalid
+ }
+ if _, e := os.Stat(p); os.IsNotExist(e) {
+ return p, nil
+ } else if e != nil {
+ return "", e
+ }
+ if i > 999 {
+ return "", os.ErrExist
+ }
+ }
+}
diff --git a/backend/internal/api/libraries_test.go b/backend/internal/api/libraries_test.go
new file mode 100644
index 0000000..67ce27c
--- /dev/null
+++ b/backend/internal/api/libraries_test.go
@@ -0,0 +1,78 @@
+package api
+
+import (
+ "bytes"
+ "encoding/json"
+ "mime/multipart"
+ "net/http/httptest"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+)
+
+func TestLibraryCreateListUpload(t *testing.T) {
+ _, _, h, booksDir := setupAPI(t)
+ tok := adminToken(t, h)
+ root := filepath.Join(booksDir, "lib1") // 必须落在解析过软链的 booksDir 内
+ os.MkdirAll(root, 0o755)
+ w := do(h, "POST", "/api/libraries", tok, map[string]string{"name": "comics", "root_path": root})
+ if w.Code != 201 {
+ t.Fatalf("create lib %d %s", w.Code, w.Body)
+ }
+ var lib map[string]any
+ json.Unmarshal(w.Body.Bytes(), &lib)
+ libID := itoa(lib["id"])
+ w = do(h, "GET", "/api/libraries", tok, nil)
+ if !strings.Contains(w.Body.String(), `"comics"`) {
+ t.Fatalf("list: %s", w.Body)
+ }
+ // 相对路径 root 必须 400(前缀校验的根)
+ w = do(h, "POST", "/api/libraries", tok, map[string]string{"name": "x", "root_path": "relative/path"})
+ if w.Code != 400 {
+ t.Fatalf("relative root want 400 got %d", w.Code)
+ }
+ // 上传:白名单 + 防穿越 + 原子落盘
+ body, mw := uploadBody("my 01.cbz", []byte("zipbytes"))
+ req := httptest.NewRequest("POST", "/api/libraries/"+libID+"/upload", body)
+ req.Header.Set("Content-Type", mw.FormDataContentType())
+ req.Header.Set("Authorization", "Bearer "+tok)
+ ww := httptest.NewRecorder()
+ h.ServeHTTP(ww, req)
+ if ww.Code != 202 {
+ t.Fatalf("upload %d %s", ww.Code, ww.Body)
+ }
+ if _, err := os.Stat(filepath.Join(root, "my 01.cbz")); err != nil {
+ t.Fatal("uploaded file missing:", err)
+ }
+ body, mw = uploadBody("../../evil.cbz", []byte("x"))
+ req = httptest.NewRequest("POST", "/api/libraries/"+libID+"/upload", body)
+ req.Header.Set("Content-Type", mw.FormDataContentType())
+ req.Header.Set("Authorization", "Bearer "+tok)
+ ww = httptest.NewRecorder()
+ h.ServeHTTP(ww, req)
+ if ww.Code != 202 { // 名字被清洗成 evil.cbz,落在 root 内
+ t.Fatalf("sanitize upload %d", ww.Code)
+ }
+ if _, err := os.Stat(filepath.Join(root, "evil.cbz")); err != nil {
+ t.Fatal("evil upload not sanitized")
+ }
+ body, mw = uploadBody("virus.exe", []byte("x"))
+ req = httptest.NewRequest("POST", "/api/libraries/"+libID+"/upload", body)
+ req.Header.Set("Content-Type", mw.FormDataContentType())
+ req.Header.Set("Authorization", "Bearer "+tok)
+ ww = httptest.NewRecorder()
+ h.ServeHTTP(ww, req)
+ if ww.Code != 400 {
+ t.Fatalf("bad ext want 400 got %d", ww.Code)
+ }
+}
+
+func uploadBody(filename string, content []byte) (*bytes.Buffer, *multipart.Writer) {
+ buf := &bytes.Buffer{}
+ mw := multipart.NewWriter(buf)
+ fw, _ := mw.CreateFormFile("file", filename)
+ fw.Write(content)
+ mw.Close()
+ return buf, mw
+}
diff --git a/backend/internal/api/progress.go b/backend/internal/api/progress.go
new file mode 100644
index 0000000..2252876
--- /dev/null
+++ b/backend/internal/api/progress.go
@@ -0,0 +1,57 @@
+package api
+
+import (
+ "encoding/json"
+ "net/http"
+ "time"
+
+ "github.com/gin-gonic/gin"
+)
+
+func (a *api) putProgress(c *gin.Context) {
+ b, ok := a.bookFromParam(c)
+ if !ok {
+ return
+ }
+ var req struct {
+ Locator json.RawMessage `json:"locator"`
+ Percent float64 `json:"percent"`
+ }
+ if e := c.ShouldBindJSON(&req); e != nil {
+ err(c, http.StatusBadRequest, "bad_request", "json body required")
+ return
+ }
+ if req.Percent < 0 || req.Percent > 1 {
+ err(c, http.StatusBadRequest, "bad_request", "percent must be in [0,1]")
+ return
+ }
+ if len(req.Locator) == 0 {
+ req.Locator = []byte("{}")
+ }
+ if !json.Valid(req.Locator) {
+ err(c, http.StatusBadRequest, "bad_request", "locator must be valid json")
+ return
+ }
+ if e := a.st.UpsertProgress(c, uid(c), b.LibraryID, b.Path, req.Locator, req.Percent); e != nil {
+ dbErr(c, e)
+ return
+ }
+ c.Status(http.StatusNoContent)
+}
+
+func (a *api) listProgress(c *gin.Context) {
+ rows, e := a.st.ListProgress(c, uid(c))
+ if e != nil {
+ dbErr(c, e)
+ return
+ }
+ out := make([]gin.H, 0, len(rows))
+ for _, p := range rows {
+ out = append(out, gin.H{
+ "library_id": p.LibraryID, "library": p.LibraryName, "path": p.BookPath,
+ "title": p.Title, "locator": json.RawMessage(p.Locator),
+ "percent": p.Percent, "updated_at": p.UpdatedAt.Format(time.RFC3339),
+ })
+ }
+ c.JSON(http.StatusOK, out)
+}
diff --git a/backend/internal/api/progress_test.go b/backend/internal/api/progress_test.go
new file mode 100644
index 0000000..f473ede
--- /dev/null
+++ b/backend/internal/api/progress_test.go
@@ -0,0 +1,75 @@
+package api
+
+import (
+ "encoding/json"
+ "os"
+ "path/filepath"
+ "testing"
+)
+
+func TestProgressRoundTrip(t *testing.T) {
+ st, sc, h, booksDir := setupAPI(t)
+ atok := adminToken(t, h)
+ do(h, "POST", "/api/users", atok, map[string]string{"username": "p1", "password": testPW, "role": "member"})
+ mtok := loginAs(t, h, "p1", testPW)
+
+ lib, root := newLibrary(t, st, h, atok, booksDir, "prog")
+ os.WriteFile(filepath.Join(root, "a.txt"), []byte("aaa"), 0o644)
+ scanNow(t, sc, lib)
+ w := do(h, "GET", "/api/books?q=a", atok, nil) // title="a"(文件名去 .txt)
+ var bs []map[string]any
+ json.Unmarshal(w.Body.Bytes(), &bs)
+ id := itoa(bs[0]["id"])
+
+ w = do(h, "PUT", "/api/books/"+id+"/progress", mtok,
+ map[string]any{"locator": map[string]int{"page": 12}, "percent": 0.42})
+ if w.Code != 204 {
+ t.Fatalf("put %d %s", w.Code, w.Body)
+ }
+ w = do(h, "GET", "/api/progress", mtok, nil)
+ var rows []map[string]any
+ json.Unmarshal(w.Body.Bytes(), &rows)
+ if len(rows) != 1 || rows[0]["percent"].(float64) != 0.42 || rows[0]["title"] != "a" {
+ t.Fatalf("get %s", w.Body)
+ }
+ if rows[0]["locator"].(map[string]any)["page"].(float64) != 12 {
+ t.Fatalf("locator %s", w.Body)
+ }
+ // upsert 覆盖
+ do(h, "PUT", "/api/books/"+id+"/progress", mtok, map[string]any{"locator": map[string]int{"page": 20}, "percent": 0.8})
+ w = do(h, "GET", "/api/progress", mtok, nil)
+ json.Unmarshal(w.Body.Bytes(), &rows)
+ if len(rows) != 1 || rows[0]["percent"].(float64) != 0.8 {
+ t.Fatalf("upsert %s", w.Body)
+ }
+ // 别人的进度不可见
+ w = do(h, "GET", "/api/progress", atok, nil)
+ json.Unmarshal(w.Body.Bytes(), &rows)
+ if len(rows) != 0 {
+ t.Fatalf("leak %s", w.Body)
+ }
+ // 校验
+ w = do(h, "PUT", "/api/books/"+id+"/progress", mtok, map[string]any{"percent": 1.5})
+ if w.Code != 400 {
+ t.Fatalf("bad percent want 400 got %d", w.Code)
+ }
+ w = do(h, "PUT", "/api/books/999999/progress", mtok, map[string]any{"percent": 0.5})
+ if w.Code != 404 {
+ t.Fatalf("bad book want 404 got %d", w.Code)
+ }
+ // 列表 percent 内嵌(Task 10 预告的断言在此兑现)
+ w = do(h, "GET", "/api/books", mtok, nil)
+ json.Unmarshal(w.Body.Bytes(), &bs)
+ if bs[0]["percent"].(float64) != 0.8 {
+ t.Fatalf("list percent %+v", bs[0])
+ }
+ // 删书,进度按 path 保留,重扫后 0.8 回来
+ do(h, "DELETE", "/api/books/"+id, atok, nil)
+ os.WriteFile(filepath.Join(root, "a.txt"), []byte("aaa"), 0o644)
+ scanNow(t, sc, lib)
+ w = do(h, "GET", "/api/books?q=a", mtok, nil)
+ json.Unmarshal(w.Body.Bytes(), &bs)
+ if bs[0]["percent"].(float64) != 0.8 {
+ t.Fatalf("progress did not survive delete+rescan: %+v", bs[0])
+ }
+}
diff --git a/backend/internal/api/router.go b/backend/internal/api/router.go
new file mode 100644
index 0000000..8a023a4
--- /dev/null
+++ b/backend/internal/api/router.go
@@ -0,0 +1,50 @@
+package api
+
+import (
+ "net/http"
+
+ "github.com/gin-gonic/gin"
+
+ "booklib/internal/config"
+ "booklib/internal/redispkg"
+ "booklib/internal/scanner"
+ "booklib/internal/store"
+)
+
+func NewRouter(cfg *config.Config, st *store.Store, rdb *redispkg.R, sc *scanner.Scanner) *gin.Engine {
+ gin.SetMode(gin.ReleaseMode)
+ a := &api{cfg: cfg, st: st, rdb: rdb, sc: sc}
+ r := gin.New()
+ if e := r.SetTrustedProxies(cfg.TrustedProxies); e != nil {
+ panic(e)
+ }
+ r.Use(gin.Recovery())
+ g := r.Group("/api")
+ g.GET("/healthz", func(c *gin.Context) { c.String(http.StatusOK, "ok") })
+ g.POST("/auth/login", a.login)
+
+ p := g.Group("", a.authMw())
+ p.GET("/auth/me", a.me)
+
+ users := p.Group("/users", a.adminOnly())
+ users.GET("", a.listUsers)
+ users.POST("", a.createUser)
+ users.DELETE("/:id", a.deleteUser)
+
+ libs := p.Group("/libraries")
+ libs.GET("", a.listLibraries)
+ libs.POST("", a.adminOnly(), a.createLibrary)
+ libs.POST("/:id/scan", a.adminOnly(), a.scanLibrary)
+ libs.POST("/:id/upload", a.adminOnly(), a.upload)
+
+ p.GET("/books", a.listBooks)
+ p.GET("/books/:id", a.getBook)
+ p.DELETE("/books/:id", a.adminOnly(), a.deleteBook)
+ p.GET("/books/:id/cover", a.serveCover)
+ p.GET("/books/:id/file", a.serveFile)
+ p.GET("/books/:id/pages", a.pagesCount)
+ p.GET("/books/:id/pages/:n", a.page)
+ p.PUT("/books/:id/progress", a.putProgress)
+ p.GET("/progress", a.listProgress)
+ return r
+}
diff --git a/backend/internal/api/router_test.go b/backend/internal/api/router_test.go
new file mode 100644
index 0000000..f36db66
--- /dev/null
+++ b/backend/internal/api/router_test.go
@@ -0,0 +1,53 @@
+package api
+
+import (
+ "errors"
+ "fmt"
+ "net/http"
+ "net/http/httptest"
+ "syscall"
+ "testing"
+ "time"
+
+ "github.com/gin-gonic/gin"
+ "github.com/jackc/pgx/v5/pgconn"
+ "github.com/jackc/puddle/v2"
+
+ "booklib/internal/config"
+ "booklib/internal/redispkg"
+)
+
+func testCfg() *config.Config {
+ return &config.Config{Addr: ":8080", JWTSecret: []byte("s3cret"), ScanInterval: time.Minute, UploadMaxMB: 200,
+ TrustedProxies: []string{"172.16.0.0/12"}} // 与 prod 默认一致: 只有 compose 网段内代理才可信
+}
+
+func TestHealthz(t *testing.T) {
+ r := NewRouter(testCfg(), nil, redispkg.New(""), nil)
+ req := httptest.NewRequest(http.MethodGet, "/api/healthz", nil)
+ w := httptest.NewRecorder()
+ r.ServeHTTP(w, req)
+ if w.Code != http.StatusOK {
+ t.Fatalf("healthz = %d, want 200", w.Code)
+ }
+}
+
+func TestDBErrStatus(t *testing.T) {
+ for _, tc := range []struct {
+ e error
+ want int
+ }{
+ {&pgconn.PgError{Code: "57P01"}, http.StatusServiceUnavailable}, // PG 停机:池内连接被服务端断
+ {&pgconn.PgError{Code: "08006"}, http.StatusServiceUnavailable},
+ {fmt.Errorf("dial: %w", syscall.ECONNREFUSED), http.StatusServiceUnavailable},
+ {puddle.ErrClosedPool, http.StatusServiceUnavailable},
+ {errors.New("boom"), http.StatusInternalServerError},
+ } {
+ w := httptest.NewRecorder()
+ c, _ := gin.CreateTestContext(w)
+ dbErr(c, tc.e)
+ if w.Code != tc.want {
+ t.Errorf("dbErr(%v) = %d, want %d", tc.e, w.Code, tc.want)
+ }
+ }
+}
diff --git a/backend/internal/api/users.go b/backend/internal/api/users.go
new file mode 100644
index 0000000..c589d57
--- /dev/null
+++ b/backend/internal/api/users.go
@@ -0,0 +1,97 @@
+package api
+
+import (
+ "errors"
+ "net/http"
+ "strconv"
+ "time"
+
+ "github.com/gin-gonic/gin"
+ "github.com/jackc/pgx/v5"
+ "github.com/jackc/pgx/v5/pgconn"
+
+ "booklib/internal/auth"
+)
+
+func isUnique(e error) bool {
+ var pgErr *pgconn.PgError
+ return errors.As(e, &pgErr) && pgErr.Code == "23505"
+}
+
+func (a *api) listUsers(c *gin.Context) {
+ users, e := a.st.ListUsers(c)
+ if e != nil {
+ dbErr(c, e)
+ return
+ }
+ out := make([]gin.H, 0, len(users))
+ for _, u := range users {
+ out = append(out, gin.H{"id": u.ID, "username": u.Username, "role": u.Role,
+ "created_at": u.CreatedAt.Format(time.RFC3339)})
+ }
+ c.JSON(http.StatusOK, out)
+}
+
+func (a *api) createUser(c *gin.Context) {
+ var req struct{ Username, Password, Role string }
+ if c.ShouldBindJSON(&req) != nil {
+ err(c, http.StatusBadRequest, "bad_request", "json body required")
+ return
+ }
+ if req.Role != "admin" && req.Role != "member" {
+ err(c, http.StatusBadRequest, "bad_request", "role must be admin|member")
+ return
+ }
+ if len(req.Password) < 8 {
+ err(c, http.StatusBadRequest, "bad_request", "password too short (min 8)")
+ return
+ }
+ h, e := auth.HashPassword(req.Password)
+ if e != nil {
+ err(c, http.StatusInternalServerError, "internal", "hash")
+ return
+ }
+ id, e := a.st.CreateUser(c, req.Username, h, req.Role)
+ if e != nil {
+ if isUnique(e) {
+ err(c, http.StatusConflict, "exists", "username taken")
+ return
+ }
+ dbErr(c, e)
+ return
+ }
+ c.JSON(http.StatusCreated, gin.H{"id": id, "username": req.Username, "role": req.Role})
+}
+
+func (a *api) deleteUser(c *gin.Context) {
+ id, e := strconv.ParseInt(c.Param("id"), 10, 64)
+ if e != nil {
+ err(c, http.StatusBadRequest, "bad_request", "bad id")
+ return
+ }
+ if id == uid(c) {
+ err(c, http.StatusBadRequest, "bad_request", "cannot delete yourself")
+ return
+ }
+ target, e := a.st.GetUserByID(c, id)
+ if e != nil {
+ if errors.Is(e, pgx.ErrNoRows) {
+ err(c, http.StatusNotFound, "not_found", "no such user")
+ return
+ }
+ dbErr(c, e)
+ return
+ }
+ if target.Role == "admin" {
+ n, _ := a.st.CountAdmins(c) // 防删光最后一个 admin
+ if n <= 1 {
+ err(c, http.StatusBadRequest, "bad_request", "cannot delete the last admin")
+ return
+ }
+ }
+ if e := a.st.DeleteUser(c, id); e != nil {
+ dbErr(c, e)
+ return
+ }
+ c.Status(http.StatusNoContent)
+}
diff --git a/backend/internal/api/users_test.go b/backend/internal/api/users_test.go
new file mode 100644
index 0000000..c15dafc
--- /dev/null
+++ b/backend/internal/api/users_test.go
@@ -0,0 +1,81 @@
+package api
+
+import (
+ "encoding/json"
+ "net/http"
+ "strconv"
+ "testing"
+)
+
+func adminToken(t *testing.T, h http.Handler) string {
+ t.Helper()
+ return loginAs(t, h, "alice", testPW)
+}
+
+func TestUserCRUD(t *testing.T) {
+ _, _, h, _ := setupAPI(t)
+ tok := adminToken(t, h)
+ w := do(h, "POST", "/api/users", tok, map[string]string{"username": "carol", "password": "pw123456", "role": "member"})
+ if w.Code != 201 {
+ t.Fatalf("create %d %s", w.Code, w.Body)
+ }
+ w = do(h, "GET", "/api/users", tok, nil)
+ var users []map[string]any
+ json.Unmarshal(w.Body.Bytes(), &users)
+ if len(users) != 3 {
+ t.Fatalf("list want 3 got %d: %s", len(users), w.Body)
+ }
+ carolID := findID(users, "carol")
+ // 重名 → 409
+ w = do(h, "POST", "/api/users", tok, map[string]string{"username": "carol", "password": "pw123456", "role": "member"})
+ if w.Code != 409 {
+ t.Fatalf("dup want 409 got %d", w.Code)
+ }
+ // 弱密码 → 400
+ w = do(h, "POST", "/api/users", tok, map[string]string{"username": "dave", "password": "1", "role": "member"})
+ if w.Code != 400 {
+ t.Fatalf("weak want 400 got %d", w.Code)
+ }
+ // 不能删自己:先 me 拿 id
+ w = do(h, "GET", "/api/auth/me", tok, nil)
+ var me map[string]any
+ json.Unmarshal(w.Body.Bytes(), &me)
+ w = do(h, "DELETE", "/api/users/"+itoa(me["id"]), tok, nil)
+ if w.Code != 400 {
+ t.Fatalf("self-delete want 400 got %d %s", w.Code, w.Body)
+ }
+ // 删 carol → 204,再删 → 404
+ w = do(h, "DELETE", "/api/users/"+itoa(carolID), tok, nil)
+ if w.Code != 204 {
+ t.Fatalf("delete %d %s", w.Code, w.Body)
+ }
+ w = do(h, "DELETE", "/api/users/"+itoa(carolID), tok, nil)
+ if w.Code != 404 {
+ t.Fatalf("redelete want 404 got %d", w.Code)
+ }
+}
+
+func TestBadRoleRejected(t *testing.T) {
+ _, _, h, _ := setupAPI(t)
+ tok := adminToken(t, h)
+ w := do(h, "POST", "/api/users", tok, map[string]string{"username": "e", "password": "pw123456", "role": "god"})
+ if w.Code != 400 {
+ t.Fatalf("bad role want 400 got %d", w.Code)
+ }
+}
+
+func findID(rows []map[string]any, name string) float64 {
+ for _, r := range rows {
+ if r["username"] == name {
+ return r["id"].(float64)
+ }
+ }
+ return 0
+}
+
+func itoa(v any) string {
+ if i, ok := v.(int64); ok {
+ return strconv.FormatInt(i, 10) // DB 行里的 id (如 store.Library.ID)
+ }
+ return strconv.FormatFloat(v.(float64), 'f', 0, 64) // json.Unmarshal 出来的数字
+}
diff --git a/backend/internal/auth/auth.go b/backend/internal/auth/auth.go
new file mode 100644
index 0000000..7fe8cec
--- /dev/null
+++ b/backend/internal/auth/auth.go
@@ -0,0 +1,54 @@
+package auth
+
+import (
+ "crypto/subtle"
+ "errors"
+ "time"
+
+ "github.com/golang-jwt/jwt/v5"
+ "golang.org/x/crypto/bcrypt"
+)
+
+var ErrToken = errors.New("invalid token")
+
+func HashPassword(plain string) (string, error) {
+ b, err := bcrypt.GenerateFromPassword([]byte(plain), 12)
+ return string(b), err
+}
+
+func CheckPassword(hash, plain string) bool {
+ return bcrypt.CompareHashAndPassword([]byte(hash), []byte(plain)) == nil
+}
+
+type Claims struct {
+ UID int64 `json:"uid"`
+ Role string `json:"role"`
+ jwt.RegisteredClaims
+}
+
+func Sign(secret []byte, uid int64, role string) (string, error) {
+ return SignWithTTL(secret, uid, role, 72*time.Hour)
+}
+
+func SignWithTTL(secret []byte, uid int64, role string, ttl time.Duration) (string, error) {
+ t := jwt.NewWithClaims(jwt.SigningMethodHS256, Claims{
+ UID: uid, Role: role,
+ RegisteredClaims: jwt.RegisteredClaims{ExpiresAt: jwt.NewNumericDate(time.Now().Add(ttl))},
+ })
+ return t.SignedString(secret)
+}
+
+func Parse(secret []byte, token string) (*Claims, error) {
+ c := &Claims{}
+ parsed, err := jwt.ParseWithClaims(token, c, func(t *jwt.Token) (any, error) {
+ m, ok := t.Method.(*jwt.SigningMethodHMAC)
+ if !ok || subtle.ConstantTimeCompare([]byte(m.Alg()), []byte("HS256")) != 1 {
+ return nil, ErrToken
+ }
+ return secret, nil
+ }, jwt.WithValidMethods([]string{"HS256"}))
+ if err != nil || !parsed.Valid {
+ return nil, ErrToken
+ }
+ return c, nil
+}
diff --git a/backend/internal/auth/auth_test.go b/backend/internal/auth/auth_test.go
new file mode 100644
index 0000000..f5a52b2
--- /dev/null
+++ b/backend/internal/auth/auth_test.go
@@ -0,0 +1,40 @@
+package auth
+
+import (
+ "strings"
+ "testing"
+ "time"
+)
+
+func TestHashCheck(t *testing.T) {
+ h, err := HashPassword("hunter2")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !strings.HasPrefix(h, "$2a$") || !CheckPassword(h, "hunter2") || CheckPassword(h, "wrong") {
+ t.Fatal("bcrypt wrong")
+ }
+}
+
+func TestSignParse(t *testing.T) {
+ secret := []byte("k")
+ tok, err := Sign(secret, 7, "admin")
+ if err != nil {
+ t.Fatal(err)
+ }
+ c, err := Parse(secret, tok)
+ if err != nil || c.UID != 7 || c.Role != "admin" {
+ t.Fatalf("parse: %v %+v", err, c)
+ }
+ if _, err := Parse([]byte("other"), tok); err == nil {
+ t.Fatal("must reject wrong secret")
+ }
+ expired, _ := SignWithTTL(secret, 1, "member", -time.Hour)
+ if _, err := Parse(secret, expired); err == nil {
+ t.Fatal("must reject expired")
+ }
+ // alg 混淆攻击:none 必须拒
+ if _, err := Parse(secret, "eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJ1aWQiOjF9."); err == nil {
+ t.Fatal("must reject alg=none")
+ }
+}
diff --git a/backend/internal/bookfile/bookfile.go b/backend/internal/bookfile/bookfile.go
new file mode 100644
index 0000000..fd7fae1
--- /dev/null
+++ b/backend/internal/bookfile/bookfile.go
@@ -0,0 +1,42 @@
+package bookfile
+
+import (
+ "path/filepath"
+ "strings"
+ "unicode"
+)
+
+func FormatFromExt(name string) string {
+ switch strings.ToLower(filepath.Ext(name)) {
+ case ".cbz", ".zip":
+ return "cbz"
+ case ".pdf":
+ return "pdf"
+ case ".epub":
+ return "epub"
+ case ".txt":
+ return "txt"
+ case ".md":
+ return "md"
+ }
+ return ""
+}
+
+func SafeName(s string) string {
+ s = strings.Map(func(r rune) rune {
+ if unicode.IsControl(r) {
+ return -1
+ }
+ return r
+ }, s)
+ s = strings.ReplaceAll(s, "\\", "/")
+ s = filepath.Base(filepath.ToSlash(s))
+ if s == "." || s == "/" {
+ return ""
+ }
+ s = strings.TrimSpace(s)
+ if len(s) > 200 {
+ s = strings.TrimSpace(s[:200])
+ }
+ return s
+}
diff --git a/backend/internal/bookfile/bookfile_test.go b/backend/internal/bookfile/bookfile_test.go
new file mode 100644
index 0000000..311eb20
--- /dev/null
+++ b/backend/internal/bookfile/bookfile_test.go
@@ -0,0 +1,32 @@
+package bookfile
+
+import "testing"
+
+func TestFormatFromExt(t *testing.T) {
+ cases := map[string]string{
+ "a.cbz": "cbz", "B.PDF": "pdf", "x.epub": "epub", "y.txt": "txt", "z.md": "md",
+ "w.rar": "", "noext": "", "cbr.cbZ": "cbz", "tar.gz": "",
+ }
+ for in, want := range cases {
+ if got := FormatFromExt(in); got != want {
+ t.Errorf("FormatFromExt(%q)=%q want %q", in, got, want)
+ }
+ }
+}
+
+func TestSafeName(t *testing.T) {
+ cases := map[string]string{
+ "my book.cbz": "my book.cbz",
+ "../../etc/passwd": "passwd",
+ "/abs/name.pdf": "name.pdf",
+ "a\\b\\c.epub": "c.epub",
+ " spaced .txt ": "spaced .txt",
+ "con\ntl.bin": "contl.bin",
+ "": "",
+ }
+ for in, want := range cases {
+ if got := SafeName(in); got != want {
+ t.Errorf("SafeName(%q)=%q want %q", in, got, want)
+ }
+ }
+}
diff --git a/backend/internal/bookfile/cache.go b/backend/internal/bookfile/cache.go
new file mode 100644
index 0000000..7667a4c
--- /dev/null
+++ b/backend/internal/bookfile/cache.go
@@ -0,0 +1,37 @@
+package bookfile
+
+import (
+ "errors"
+ "fmt"
+ "io/fs"
+ "os"
+ "path/filepath"
+)
+
+func DirKey(id int64, hash string) string { return fmt.Sprintf("%d-%s", id, hash) }
+
+func CoverDir(cacheDir, key string) string { return filepath.Join(cacheDir, "covers", key) }
+func PagesDir(cacheDir, key string) string { return filepath.Join(cacheDir, "pages", key) }
+
+func SweepStale(cacheDir string, live map[string]bool) (int, error) {
+ removed := 0
+ for _, kind := range []string{"covers", "pages"} {
+ entries, err := os.ReadDir(filepath.Join(cacheDir, kind))
+ if err != nil {
+ if errors.Is(err, fs.ErrNotExist) {
+ continue // 首次运行还没有该层目录,不算错
+ }
+ return removed, err
+ }
+ for _, e := range entries {
+ if !e.IsDir() || live[e.Name()] {
+ continue
+ }
+ if err := os.RemoveAll(filepath.Join(cacheDir, kind, e.Name())); err != nil {
+ return removed, err
+ }
+ removed++
+ }
+ }
+ return removed, nil
+}
diff --git a/backend/internal/bookfile/cache_test.go b/backend/internal/bookfile/cache_test.go
new file mode 100644
index 0000000..72b98bf
--- /dev/null
+++ b/backend/internal/bookfile/cache_test.go
@@ -0,0 +1,53 @@
+package bookfile
+
+import (
+ "os"
+ "path/filepath"
+ "testing"
+)
+
+func mk(t *testing.T, cacheDir, kind, key, file string) {
+ t.Helper()
+ d := filepath.Join(cacheDir, kind, key)
+ os.MkdirAll(d, 0o755)
+ os.WriteFile(filepath.Join(d, file), []byte("x"), 0o644)
+}
+
+func TestSweepStale(t *testing.T) {
+ dir := t.TempDir()
+ mk(t, dir, "covers", "1-aaa", "cover.jpg")
+ mk(t, dir, "covers", "2-bbb", "cover.png")
+ mk(t, dir, "pages", "1-aaa", "0.jpg")
+ mk(t, dir, "pages", "3-ccc", "0.jpg")
+ os.WriteFile(filepath.Join(dir, "covers", "stray.txt"), []byte("x"), 0o644) // 非目录,忽略
+
+ n, err := SweepStale(dir, map[string]bool{"1-aaa": true})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if n != 2 {
+ t.Fatalf("removed %d want 2", n)
+ }
+ if _, err := os.Stat(filepath.Join(dir, "covers", "1-aaa")); err != nil {
+ t.Fatal("live dir removed")
+ }
+ if _, err := os.Stat(filepath.Join(dir, "pages", "1-aaa")); err != nil {
+ t.Fatal("live pages removed")
+ }
+ if _, err := os.Stat(filepath.Join(dir, "covers", "2-bbb")); !os.IsNotExist(err) {
+ t.Fatal("stale cover remains")
+ }
+ // cacheDir 不存在 = 首次运行,不算错
+ if n, err := SweepStale(filepath.Join(dir, "nope"), nil); n != 0 || err != nil {
+ t.Fatalf("%d %v", n, err)
+ }
+}
+
+func TestPaths(t *testing.T) {
+ if DirKey(12, "ab") != "12-ab" {
+ t.Fatal("DirKey")
+ }
+ if CoverDir("/c", "12-ab") != filepath.Join("/c", "covers", "12-ab") {
+ t.Fatal("CoverDir")
+ }
+}
diff --git a/backend/internal/bookfile/cover.go b/backend/internal/bookfile/cover.go
new file mode 100644
index 0000000..2fa2f1a
--- /dev/null
+++ b/backend/internal/bookfile/cover.go
@@ -0,0 +1,125 @@
+package bookfile
+
+import (
+ "archive/zip"
+ "encoding/xml"
+ "errors"
+ "io"
+ "net/url"
+ "path"
+ "strings"
+)
+
+func CBZCover(f io.ReaderAt, size int64) ([]byte, string, error) {
+ idx, err := PageIndex(f, size)
+ if err != nil {
+ return nil, "", err
+ }
+ if len(idx) == 0 {
+ return nil, "", errors.New("no page images")
+ }
+ img, err := ReadEntry(f, size, idx[0])
+ return img, strings.ToLower(path.Ext(idx[0])), err
+}
+
+func readZipEntry(zr *zip.Reader, name string) ([]byte, error) {
+ for _, zf := range zr.File {
+ if zf.Name == name {
+ rc, err := zf.Open()
+ if err != nil {
+ return nil, err
+ }
+ defer rc.Close()
+ return io.ReadAll(io.LimitReader(rc, 8<<20))
+ }
+ }
+ return nil, errors.New("entry not found: " + name)
+}
+
+type opfDoc struct {
+ XMLName xml.Name `xml:"package"`
+ Metadata struct {
+ Meta []struct {
+ Name string `xml:"name,attr"`
+ Content string `xml:"content,attr"`
+ } `xml:"meta"`
+ } `xml:"metadata"`
+ Manifest struct {
+ Items []struct {
+ ID string `xml:"id,attr"`
+ Href string `xml:"href,attr"`
+ Properties string `xml:"properties,attr"`
+ MediaType string `xml:"media-type,attr"`
+ } `xml:"item"`
+ } `xml:"manifest"`
+}
+
+func EPUBCover(f io.ReaderAt, size int64) ([]byte, string, error) {
+ zr, err := zip.NewReader(f, size)
+ if err != nil {
+ return nil, "", ErrNotZip
+ }
+ cont, err := readZipEntry(zr, "META-INF/container.xml")
+ if err != nil {
+ return nil, "", err
+ }
+ var root struct {
+ XMLName xml.Name `xml:"container"`
+ RootFiles []struct {
+ FullPath string `xml:"full-path,attr"`
+ } `xml:"rootfiles>rootfile"`
+ }
+ if err := xml.Unmarshal(cont, &root); err != nil || len(root.RootFiles) == 0 {
+ return nil, "", errors.New("bad container.xml")
+ }
+ opfPath := root.RootFiles[0].FullPath
+ opfData, err := readZipEntry(zr, opfPath)
+ if err != nil {
+ return nil, "", err
+ }
+ var doc opfDoc
+ if err := xml.Unmarshal(opfData, &doc); err != nil {
+ return nil, "", errors.New("bad opf")
+ }
+ metaCoverID := ""
+ for _, m := range doc.Metadata.Meta {
+ if m.Name == "cover" {
+ metaCoverID = m.Content
+ }
+ }
+ href := ""
+ for _, it := range doc.Manifest.Items {
+ if strings.Contains(it.Properties, "cover-image") {
+ href = it.Href
+ break
+ }
+ if metaCoverID != "" && it.ID == metaCoverID {
+ href = it.Href
+ break
+ }
+ }
+ if href == "" {
+ for _, it := range doc.Manifest.Items {
+ if strings.HasPrefix(it.MediaType, "image/") && strings.Contains(strings.ToLower(it.ID), "cover") {
+ href = it.Href
+ break
+ }
+ }
+ }
+ if href == "" {
+ return nil, "", errors.New("no cover found")
+ }
+ unescaped, e := url.PathUnescape(href)
+ if e == nil {
+ href = unescaped
+ }
+ name := path.Join(path.Dir(opfPath), href)
+ if name == "." {
+ name = href
+ }
+ img, err := readZipEntry(zr, name)
+ if err != nil {
+ return nil, "", err
+ }
+ return img, strings.ToLower(path.Ext(name)), nil
+}
diff --git a/backend/internal/bookfile/cover_test.go b/backend/internal/bookfile/cover_test.go
new file mode 100644
index 0000000..097e60a
--- /dev/null
+++ b/backend/internal/bookfile/cover_test.go
@@ -0,0 +1,75 @@
+package bookfile
+
+import (
+ "archive/zip"
+ "bytes"
+ "testing"
+)
+
+func TestCBZCoverIsFirstPage(t *testing.T) {
+ buf := &bytes.Buffer{}
+ zw := zip.NewWriter(buf)
+ w, _ := zw.Create("02.jpg")
+ w.Write([]byte("second"))
+ w, _ = zw.Create("01.jpg")
+ w.Write([]byte("first"))
+ zw.Close()
+ r := bytes.NewReader(buf.Bytes())
+ img, ext, err := CBZCover(r, int64(r.Len()))
+ if err != nil || string(img) != "first" || ext != ".jpg" {
+ t.Fatalf("%q %q %v", img, ext, err)
+ }
+}
+
+func zipEntries(t *testing.T, kv map[string]string) *bytes.Reader {
+ t.Helper()
+ buf := &bytes.Buffer{}
+ zw := zip.NewWriter(buf)
+ for name, data := range kv {
+ w, _ := zw.Create(name)
+ w.Write([]byte(data))
+ }
+ zw.Close()
+ return bytes.NewReader(buf.Bytes())
+}
+
+const containerXML = `
backend up — frontend lands in Plan 2.
diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..3d043ea --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,34 @@ +services: + web: + build: { context: ., dockerfile: deploy/Dockerfile.web } + ports: ["8080:80"] + depends_on: [api] + api: + build: { context: ., dockerfile: deploy/Dockerfile.api } + environment: + DATABASE_URL: postgres://lib:lib@postgres:5432/lib?sslmode=disable + REDIS_URL: redis://redis:6379 + JWT_SECRET: ${JWT_SECRET} + ADMIN_USER: ${ADMIN_USER} + ADMIN_PASSWORD: ${ADMIN_PASSWORD} + BOOKS_DIR: /data/books + CACHE_DIR: /data/cache + SCAN_INTERVAL_SEC: ${SCAN_INTERVAL_SEC:-60} + volumes: + - ./library:/data/books + - cache:/data/cache + depends_on: + postgres: { condition: service_healthy } + redis: { condition: service_started } + postgres: + image: postgres:16-alpine + environment: { POSTGRES_USER: lib, POSTGRES_PASSWORD: lib, POSTGRES_DB: lib } + volumes: [pgdata:/var/lib/postgresql/data] + healthcheck: { test: ["CMD-SHELL", "pg_isready -U lib"], interval: 2s, timeout: 2s, retries: 30 } + redis: + image: redis:7-alpine + command: ["redis-server", "--maxmemory", "128mb", "--maxmemory-policy", "allkeys-lru"] + # 故意无 volume:redis 里全是可再生数据(spec §6.2) +volumes: + pgdata: + cache: diff --git a/docs/superpowers/plans/2026-09-04-backend.md b/docs/superpowers/plans/2026-09-04-backend.md index 4858719..e0d2bb7 100644 --- a/docs/superpowers/plans/2026-09-04-backend.md +++ b/docs/superpowers/plans/2026-09-04-backend.md @@ -604,7 +604,6 @@ func (s *Store) CountAdmins(ctx context.Context) (int, error) { return n, err } -```go // ---------- libraries ---------- func (s *Store) CreateLibrary(ctx context.Context, name, root string) (int64, error) { @@ -3156,7 +3155,7 @@ func (a *api) scanLibrary(c *gin.Context) { - [ ] **Step 5: 跑全部后端测试确认通过;Commit** -Run: `cd backend && DATABASE_URL=... REDIS_URL=... go test ./... -v` +Run: `cd backend && DATABASE_URL=... REDIS_URL=... go test -p 1 ./... -v` Expected: 全 PASS ```bash @@ -4167,12 +4166,12 @@ func dbErr(c *gin.Context, e error) { } ``` -把所有 handler 中 `"internal", "db error"` 的 500 分支替换为 `dbErr(c, <该处的 error 变量>)`:auth.go(login 1 处)、users.go(listUsers/createUser/deleteUser 各 1 处)、libraries.go(listLibraries)、books.go(listBooks/deleteBook)、content.go 无 DB 500 分支、progress.go(putProgress/listProgress)。替换后 `go test ./...` 全绿即可(既有测试不覆盖 PG 宕机路径)。 +把所有 handler 中 `"internal", "db error"` 的 500 分支替换为 `dbErr(c, <该处的 error 变量>)`:auth.go(login 1 处)、users.go(listUsers/createUser/deleteUser 各 1 处)、libraries.go(listLibraries)、books.go(listBooks/deleteBook)、content.go 无 DB 500 分支、progress.go(putProgress/listProgress)。替换后 `go test -p 1 ./...` 全绿即可(既有测试不覆盖 PG 宕机路径)。 - [ ] **Step 4: 全量回归 + 本地起服烟测** ```bash -cd backend && go vet ./... && DATABASE_URL=postgres://lib:lib@localhost:5433/lib?sslmode=disable REDIS_URL=redis://localhost:6380 go test ./... +cd backend && go vet ./... && DATABASE_URL=postgres://lib:lib@localhost:5433/lib?sslmode=disable REDIS_URL=redis://localhost:6380 go test -p 1 ./... mkdir -p /tmp/libtest/books/demo && echo hello > /tmp/libtest/books/demo/a.txt cd backend && JWT_SECRET=devsecret DATABASE_URL=postgres://lib:lib@localhost:5433/lib?sslmode=disable \ REDIS_URL=redis://localhost:6380 ADMIN_USER=admin ADMIN_PASSWORD=adminpw12 BOOKS_DIR=/tmp/libtest/books \ @@ -4212,7 +4211,7 @@ git add backend && git commit -m "feat(backend): main wiring, graceful shutdown, `deploy/Dockerfile.api`: ```dockerfile -FROM golang:1.23-alpine AS build +FROM golang:1.26-alpine AS build WORKDIR /src COPY backend/go.mod backend/go.sum ./ RUN go mod download diff --git a/scripts/smoke.sh b/scripts/smoke.sh new file mode 100755 index 0000000..9989064 --- /dev/null +++ b/scripts/smoke.sh @@ -0,0 +1,57 @@ +#!/usr/bin/env bash +set -euo pipefail +BASE=${BASE:-http://localhost:8080} +API=$BASE/api +J=(-H 'content-type: application/json') +[ -f .env ] && set -a && . ./.env && set +a +WORK=$(mktemp -d); trap 'rm -rf "$WORK"' EXIT + +say(){ echo "smoke: $1"; } +die(){ echo "SMOKE FAIL: $1"; exit 1; } +tokfor(){ curl -fsS "$API/auth/login" "${J[@]}" -d "{\"username\":\"$1\",\"password\":\"$2\"}" | sed -E 's/.*"token":"([^"]+)".*/\1/'; } + +say "healthz" +curl -fsS "$API/healthz" >/dev/null || die "healthz down" + +say "login" +TOK=$(tokfor "$ADMIN_USER" "$ADMIN_PASSWORD") +[ -n "$TOK" ] || die "no token" +AUTH="authorization: Bearer $TOK" + +say "member user + role enforcement" +curl -fsS "$API/users" "${J[@]}" -H "$AUTH" -d '{"username":"smoke","password":"smokepw123","role":"member"}' >/dev/null || die "create member" +MTOK=$(tokfor smoke smokepw123) +code=$(curl -s -o /dev/null -w '%{http_code}' -X POST "$API/users" "${J[@]}" -H "authorization: Bearer $MTOK" -d '{"username":"x","password":"xpw12345","role":"member"}') +[ "$code" = 403 ] || die "member write not blocked ($code)" + +say "library + bad-ext upload rejected + good upload + scan" +mkdir -p library/smoke-books +LID=$(curl -fsS "$API/libraries" "${J[@]}" -H "$AUTH" -d '{"name":"smoke","root_path":"/data/books/smoke-books"}' | sed -E 's/.*"id":([0-9]+).*/\1/') +printf 'x' > "$WORK/f" +curl -fsS -o /dev/null "$API/libraries/$LID/upload" -H "$AUTH" -F "file=@$WORK/f;filename=virus.exe" && die "bad ext upload must fail" || true +printf 'hello smoke book' > "$WORK/f" +curl -fsS -o /dev/null "$API/libraries/$LID/upload" -H "$AUTH" -F "file=@$WORK/f;filename=note.txt" || die "upload failed" +curl -fsS -o /dev/null -X POST "$API/libraries/$LID/scan" -H "$AUTH" || die "scan trigger" +found="" +for _ in $(seq 30); do + if curl -fsS "$API/books?library=$LID" -H "$AUTH" | grep -q '"path":"note.txt"'; then found=1; break; fi + sleep 1 +done +[ -n "$found" ] || die "book not indexed after 30s" + +say "read + progress roundtrip" +BID=$(curl -fsS "$API/books?library=$LID" -H "$AUTH" | sed -E 's/.*"id":([0-9]+).*/\1/') +curl -fsS "$API/books/$BID/file" -H "$AUTH" | grep -q "hello smoke book" || die "file body" +code=$(curl -s -o /dev/null -w '%{http_code}' -X PUT "$API/books/$BID/progress" "${J[@]}" -H "authorization: Bearer $MTOK" -d '{"locator":{"scroll":0.5},"percent":0.5}') +[ "$code" = 204 ] || die "progress put $code" +curl -fsS "$API/progress" -H "authorization: Bearer $MTOK" | grep -q '"percent":0.5' || die "progress read" + +say "immutable cache header" +COVER=$(curl -fsS "$API/books/$BID" -H "$AUTH" | sed -E 's/.*"cover_url":"([^"]+)".*/\1/') +curl -fsS -o /dev/null -D - "$BASE$COVER" -H "$AUTH" | grep -qi 'cache-control:.*immutable' || die "cover not immutable" + +say "delete book → file gone from host dir" +curl -fsS -o /dev/null -X DELETE "$API/books/$BID" -H "$AUTH" || die "delete" +[ ! -f library/smoke-books/note.txt ] || die "file survived delete" + +say "ALL SMOKE TESTS PASSED"