feat(cbz): adapt reader to real archive structure — PageIndex skips macOS junk (__MACOSX/ and ._ AppleDouble were polluting pages with 163-byte blacks), pages endpoint derives chapters[{title,start}] from folder layout, imageless zip now lands state=error instead of an empty reader, CbzReader gains 目录 sidebar + prev/next chapter (TextReader pattern, rd-* classes); pagesidx cache key bumped for one-time invalidation; e2e on 311MB 調教開關:第二季.zip: 15048→7524 real pages, 52 chapters, all JPEGs
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -118,7 +119,7 @@ func (h *H) openBook(c *gin.Context, b store.Book, root string) (*os.File, int64
|
||||
|
||||
func (h *H) 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)
|
||||
key := fmt.Sprintf("pagesidx2:%d:%s", b.ID, hash) // 前缀换代 = 索引逻辑变更时一次性作废旧缓存
|
||||
if v, ok := h.rdb.Get(c, key); ok && v != "" {
|
||||
return strings.Split(v, "\n"), nil
|
||||
}
|
||||
@@ -158,7 +159,48 @@ func (h *H) PagesCount(c *gin.Context) {
|
||||
err(c, http.StatusUnprocessableEntity, "broken", e.Error())
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"count": len(idx)})
|
||||
c.JSON(http.StatusOK, gin.H{"count": len(idx), "chapters": chaptersOf(idx)})
|
||||
}
|
||||
|
||||
type cbzChapter struct {
|
||||
Title string `json:"title"`
|
||||
Start int `json:"start"`
|
||||
}
|
||||
|
||||
// chaptersOf 页索引已自然序排列,按父目录分组:每个新目录开一章,标题取目录名;扁平包或只有一组返回 nil
|
||||
func chaptersOf(idx []string) []cbzChapter {
|
||||
type grp struct {
|
||||
dir string
|
||||
start int
|
||||
}
|
||||
var grps []grp
|
||||
last := "\x00"
|
||||
for i, n := range idx {
|
||||
d := path.Dir(n)
|
||||
if d == last {
|
||||
continue
|
||||
}
|
||||
last = d
|
||||
if d == "." { // 根目录散页不开章
|
||||
continue
|
||||
}
|
||||
grps = append(grps, grp{d, i})
|
||||
}
|
||||
if len(grps) < 2 {
|
||||
return nil
|
||||
}
|
||||
titles := make(map[string]int)
|
||||
out := make([]cbzChapter, len(grps))
|
||||
for i, g := range grps {
|
||||
out[i] = cbzChapter{Title: path.Base(g.dir), Start: g.start}
|
||||
titles[out[i].Title]++
|
||||
}
|
||||
for i, g := range grps { // 同名目录(不同父级)撞车 → 用全路径消歧
|
||||
if titles[out[i].Title] > 1 {
|
||||
out[i].Title = g.dir
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (h *H) Page(c *gin.Context) {
|
||||
|
||||
Reference in New Issue
Block a user