fix(backend): single error body on pages paths, reject newline entry names, empty-index cache skip

This commit is contained in:
2026-09-05 20:56:05 +08:00
parent 0946f6be5a
commit 8206c7a4ed
3 changed files with 13 additions and 5 deletions
+11 -3
View File
@@ -36,7 +36,7 @@ func (a *api) serveCover(c *gin.Context) {
}
a.immutable(c)
dir := bookfile.CoverDir(a.cfg.CacheDir, bookfile.DirKey(b.ID, bookfile.Hash(b.FileSize, b.ModTS)))
if entries, err := os.ReadDir(dir); err == nil && len(entries) > 0 {
if entries, e := os.ReadDir(dir); e == nil && len(entries) > 0 {
http.ServeFile(c.Writer, c.Request, filepath.Join(dir, entries[0].Name()))
return
}
@@ -85,7 +85,7 @@ func (a *api) openBook(c *gin.Context, b store.Book, root string) (*os.File, int
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 {
if v, ok := a.rdb.Get(c, key); ok && v != "" {
return strings.Split(v, "\n"), nil
}
f, size, ok := a.openBook(c, b, root)
@@ -97,7 +97,9 @@ func (a *api) pageIndex(c *gin.Context, b store.Book, root string) ([]string, er
if e != nil {
return nil, e
}
a.rdb.Set(c, key, strings.Join(idx, "\n"), 7*24*time.Hour)
if len(idx) > 0 { // 空索引不缓存,否则 warm 命中 "" 会 Split 出幽灵页
a.rdb.Set(c, key, strings.Join(idx, "\n"), 7*24*time.Hour)
}
return idx, nil
}
@@ -116,6 +118,9 @@ func (a *api) pagesCount(c *gin.Context) {
}
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
}
@@ -142,6 +147,9 @@ func (a *api) page(c *gin.Context) {
}
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
}