forked from ptt/pttweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcached_ops.go
148 lines (123 loc) · 2.92 KB
/
cached_ops.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"fmt"
"github.com/ptt/pttweb/article"
"github.com/ptt/pttweb/cache"
"github.com/ptt/pttweb/pttbbs"
)
const (
EntryPerPage = 20
)
type BbsIndexRequest struct {
Brd pttbbs.Board
Page int
}
func (r *BbsIndexRequest) String() string {
return fmt.Sprintf("pttweb:bbsindex/%v/%v", r.Brd.BrdName, r.Page)
}
func generateBbsIndex(key cache.Key) (cache.Cacheable, error) {
r := key.(*BbsIndexRequest)
page := r.Page
bbsindex := &BbsIndex{
Board: r.Brd,
IsValid: true,
}
count, err := ptt.GetArticleCount(r.Brd.Bid)
if err != nil {
return nil, err
}
// Handle paging
paging := NewPaging(EntryPerPage, count)
if page == 0 {
page = paging.LastPageNo()
paging.SetPageNo(page)
} else if err = paging.SetPageNo(page); err != nil {
return nil, err
}
bbsindex.TotalPage = paging.LastPageNo()
// Fetch article list
bbsindex.Articles, err = ptt.GetArticleList(r.Brd.Bid, paging.Cursor())
if err != nil {
return nil, err
}
// Page links
if page > 1 {
bbsindex.HasPrevPage = true
bbsindex.PrevPage = page - 1
}
if page < paging.LastPageNo() {
bbsindex.HasNextPage = true
bbsindex.NextPage = page + 1
}
return bbsindex, nil
}
const (
TruncateSize = 1048576
TruncateMaxScan = 1024
HeadSize = 100 * 1024
TailSize = 50 * 1024
)
type ArticleRequest struct {
Brd pttbbs.Board
Filename string
}
func (r *ArticleRequest) String() string {
return fmt.Sprintf("pttweb:bbs/%v/%v", r.Brd.BrdName, r.Filename)
}
func generateArticle(key cache.Key) (cache.Cacheable, error) {
r := key.(*ArticleRequest)
p, err := ptt.GetArticleSelect(r.Brd.Bid, pttbbs.SelectHead, r.Filename, "", 0, HeadSize)
if err != nil {
return nil, err
}
// We don't want head and tail have duplicate content
if p.FileSize <= HeadSize+TailSize {
p, err = ptt.GetArticleSelect(r.Brd.Bid, pttbbs.SelectPart, r.Filename, "", 0, p.FileSize)
if err != nil {
return nil, err
}
}
if len(p.Content) == 0 {
return nil, pttbbs.ErrNotFound
}
a := new(Article)
a.IsPartial = p.Length < p.FileSize
a.IsTruncated = a.IsPartial
if a.IsPartial {
// Get and render tail
ptail, err := ptt.GetArticleSelect(r.Brd.Bid, pttbbs.SelectTail, r.Filename, "", -TailSize, TailSize)
if err != nil {
return nil, err
}
if len(ptail.Content) > 0 {
ar := article.NewRenderer()
ar.DisableArticleHeader = true
buf, err := ar.Render(ptail.Content)
if err != nil {
return nil, err
}
a.ContentTailHtml = buf.Bytes()
}
}
ar := article.NewRenderer()
buf, err := ar.Render(p.Content)
if err != nil {
return nil, err
}
a.ParsedTitle = ar.ParsedTitle()
a.PreviewContent = ar.PreviewContent()
a.ContentHtml = buf.Bytes()
a.IsValid = true
return a, nil
}
func truncateLargeContent(content []byte, size, maxScan int) []byte {
if len(content) <= size {
return content
}
for i := size - 1; i >= size-maxScan && i >= 0; i-- {
if content[i] == '\n' {
return content[:i+1]
}
}
return content[:size]
}