-
Notifications
You must be signed in to change notification settings - Fork 1
/
posts.go
115 lines (96 loc) · 2.18 KB
/
posts.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
package apiv1
import (
"log"
"net/http"
"strconv"
mm "github.com/kycklingar/MinMax"
DM "github.com/kycklingar/PBooru/DataManager"
BM "github.com/kycklingar/PBooru/benchmark"
)
type posts struct {
TotalPosts int
Generated float64
Posts []post
}
func postsHandler(w http.ResponseWriter, r *http.Request) {
tagStr := r.FormValue("tags")
orStr := r.FormValue("or")
filterStr := r.FormValue("filter")
unlessStr := r.FormValue("unless")
limitStr := r.FormValue("limit")
order := r.FormValue("order")
offsetStr := r.FormValue("offset")
offset, err := strconv.Atoi(offsetStr)
if err != nil {
offset = 0
}
mimeGroups := r.Form["mime-type"]
mimeIDs := DM.MimeIDsFromType(mimeGroups)
mimes := r.Form["mime"]
for _, mime := range mimes {
id, err := strconv.Atoi(mime)
if err != nil {
log.Println(err)
continue
}
contains := func(s []int, i int) bool {
for _, x := range s {
if x == i {
return true
}
}
return false
}
if !contains(mimeIDs, id) {
mimeIDs = append(mimeIDs, id)
}
}
var combineTags = len(r.FormValue("combTagNamespace")) > 0
var groupAlts = len(r.FormValue("alts")) > 0
bm := BM.Begin()
pc := DM.NewPostCollector()
err = pc.Get(
DM.SearchOptions{
And: tagStr,
Or: orStr,
Filter: filterStr,
Unless: unlessStr,
MimeIDs: mimeIDs,
AltCollect: groupAlts,
Order: order,
},
)
if err != nil {
log.Print(err)
apiError(w, err, http.StatusInternalServerError)
return
}
pc = DM.CachedPostCollector(pc)
limit, err := strconv.Atoi(limitStr)
if err != nil {
limit = 50
} else {
limit = mm.Max(10, mm.Min(100, limit))
}
var AP posts
result, err := pc.Search2(limit, limit*offset)
if err != nil {
log.Println(err)
http.Error(w, "FIXME", http.StatusInternalServerError)
return
}
AP.Posts = make([]post, len(result))
for i, set := range result {
APp, err := dmToAPIPost(set.Post, set.Tags, combineTags)
if err != nil {
log.Print(err)
http.Error(w, "FIXME", http.StatusInternalServerError)
return
}
//AP.Posts = append(AP.Posts, APp)
AP.Posts[i] = APp
}
AP.TotalPosts = pc.TotalPosts
AP.Generated = bm.End(false).Seconds()
writeJson(w, AP)
}