-
Notifications
You must be signed in to change notification settings - Fork 9
/
post_processing.go
190 lines (183 loc) · 5.25 KB
/
post_processing.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"bytes"
"fmt"
"html/template"
"log"
"net/url"
"path"
"sync/atomic"
"golang.org/x/net/html"
)
func postProcessing(done <-chan struct{}, pc *PageChannel, t *TemplateField) (bool, error) {
select {
case <-done:
return false, nil
default:
}
if config.HighResImage {
var searchImageURL func(done <-chan struct{}, pc *PageChannel, t *TemplateField, root *html.Node) bool
searchImageURL = func(done <-chan struct{}, pc *PageChannel, t *TemplateField, root *html.Node) bool {
updated := false
for c := root.FirstChild; c != nil; c = c.NextSibling {
select {
case <-done:
return false
default:
}
if c.Type == html.ElementNode && c.Data == "img" {
for i, a := range c.Attr {
if a.Key == "src" {
// log.Printf("img: %s", a.Val)
localSrc, ud := cvtHighResImageURL(done, pc, t, a.Val, t.FileName())
updated = updated || ud
c.Attr[i].Val = localSrc
}
}
}
ud := searchImageURL(done, pc, t, c)
updated = updated || ud
}
return updated
}
if ret, err := processTemplateContent(done, pc, t, searchImageURL); err != nil {
return false, err
} else if ret {
return true, nil
}
}
if config.StoreExternalResource {
var searchExternalResource func(done <-chan struct{}, pc *PageChannel, t *TemplateField, root *html.Node) bool
searchExternalResource = func(done <-chan struct{}, pc *PageChannel, t *TemplateField, root *html.Node) bool {
updated := false
for c := root.FirstChild; c != nil; c = c.NextSibling {
select {
case <-done:
return false
default:
}
if c.Type == html.ElementNode && c.Data == "img" {
for i, a := range c.Attr {
if a.Key == "src" {
localSrc, ud := cvtLocalURL(done, pc, t, a.Val, t.FileName())
updated = updated || ud
c.Attr[i].Val = localSrc
}
}
}
ud := searchExternalResource(done, pc, t, c)
updated = updated || ud
}
return updated
}
if ret, err := processTemplateContent(done, pc, t, searchExternalResource); err != nil {
return false, err
} else if ret {
return true, nil
}
}
return false, nil
}
func processTemplateContent(done <-chan struct{}, pc *PageChannel, t *TemplateField, callback func(done <-chan struct{}, pc *PageChannel, t *TemplateField, root *html.Node) bool) (bool, error) {
t.mutex.Lock()
t.Lzls.lock.Lock()
defer t.mutex.Unlock()
defer t.Lzls.lock.Unlock()
updated := false
traverse := func(tpStr string, data interface{}) (template.HTML, bool, error) {
executor := template.New("comment")
executor.Parse(tpStr)
var buf bytes.Buffer
executor.Execute(&buf, data)
node, err := html.Parse(&buf)
if err != nil {
return "", false, fmt.Errorf("failed to parse html node: %v", err)
}
buf.Reset()
ud := callback(done, pc, t, node)
if err := html.Render(&buf, node); err != nil {
return "", false, fmt.Errorf("failed to render html template: %v", err)
}
return template.HTML(buf.String()), ud, nil
}
for k := range t.Comments {
select {
case <-done:
return false, nil
default:
}
var ud bool
var err error
// log.Printf("content (before): %s", t.Comments[k].Content)
t.Comments[k].Content, ud, err = traverse(`{{.Content}}`, &t.Comments[k])
updated = updated || ud
// if err != nil {
// return false, fmt.Errorf("[parseExternalResource] failed to parse comment (PostID: %s): %v", t.Comments[k].PostID, err)
// }
if err != nil {
log.Printf("[parseExternalResource] failed to parse comment (ThreadID: %d, PostID: %d): %v", t.ThreadID, k, err)
}
// log.Printf("content (after): %s", t.Comments[k].Content)
}
for k := range t.Lzls.Map {
select {
case <-done:
return false, nil
default:
}
var ud bool
var err error
for i := range t.Lzls.Map[k].Info {
// log.Printf("lzl content (before): %s", t.Lzls.Map[k].Info[i].Content)
t.Lzls.Map[k].Info[i].Content, ud, err = traverse(`{{.Content}}`, &t.Lzls.Map[k].Info[i])
// log.Printf("lzl content (after): %s", t.Lzls.Map[k].Info[i].Content)
updated = updated || ud
if err != nil {
log.Printf("[parseExternalResource] failed to parse lzlComment (ThreadID: %d, PostID: %d, Index: %d): %v", t.ThreadID, k, t.Lzls.Map[k].Info[i].Index, err)
}
}
}
return updated, nil
}
func cvtLocalURL(done <-chan struct{}, pc *PageChannel, t *TemplateField, src, prefix string) (string, bool) {
u, err := url.Parse(src)
if err != nil {
return src, false
}
uOrig := *u
// url already converted
if u.Scheme == "" {
return src, false
}
u.Scheme = ""
u.Path = fmt.Sprintf("%s%s", u.Host, u.Path)
dst := fmt.Sprintf("res_%s/%s", prefix, u.Path)
newSrc := dst
if t.resMap.Put(newSrc) {
atomic.AddInt64(&t.resLeft, 1)
pc.Add(1)
go func() {
select {
case pc.send <- &HTMLPage{URL: &uOrig, Type: HTMLExternalResource, Path: dst, ThreadID: t.ThreadID}:
case <-done:
return
}
}()
}
return newSrc, true
}
func cvtHighResImageURL(done <-chan struct{}, pc *PageChannel, t *TemplateField, src, prefix string) (string, bool) {
u, err := url.Parse(src)
if err != nil {
return src, false
}
if u.Host != "c.hiphotos.baidu.com" {
return src, false
}
imageName := path.Base(u.Path)
u.Host = "imgsrc.baidu.com"
u.Path = fmt.Sprintf("/forum/pic/item/%s", imageName)
dst := u.String()
newSrc := dst
return newSrc, true
}