forked from espebra/filebin2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_admin.go
228 lines (203 loc) · 5.82 KB
/
http_admin.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package main
import (
"crypto/sha256"
"database/sql"
"encoding/json"
"fmt"
"github.com/dustin/go-humanize"
"github.com/gorilla/mux"
"io"
"net/http"
"strings"
"github.com/espebra/filebin2/ds"
"github.com/espebra/filebin2/s3"
)
func (h *HTTP) ViewAdminDashboard(w http.ResponseWriter, r *http.Request) {
type Data struct {
//Bins Bins `json:"bins"`
//Files []ds.File `json:"files"`
BucketInfo s3.BucketInfo `json:"bucketinfo"`
Page string `json:"page"`
DBInfo ds.Info `json:"db_info"`
DBStats sql.DBStats `json:"db_stats"`
Config ds.Config `json:"-"`
}
var data Data
data.Config = *h.config
data.Page = "about"
data.BucketInfo = h.s3.GetBucketInfo()
info, err := h.dao.Info().GetInfo()
if err != nil {
fmt.Printf("Unable to GetInfo(): %s\n", err.Error())
http.Error(w, "Errno 326", http.StatusInternalServerError)
return
}
data.DBInfo = info
freeBytes := int64(h.config.LimitStorageBytes) - info.CurrentBytes
if freeBytes < 0 {
freeBytes = 0
}
data.DBInfo.FreeBytes = freeBytes
data.DBInfo.FreeBytesReadable = humanize.Bytes(uint64(freeBytes))
data.DBStats = h.dao.Stats()
//binsAvailable, err := h.dao.Bin().GetAll()
//if err != nil {
// fmt.Printf("Unable to GetAll(): %s\n", err.Error())
// http.Error(w, "Errno 200", http.StatusInternalServerError)
// return
//}
//var bins Bins
//bins.Available = binsAvailable
//data.Bins = bins
if r.Header.Get("accept") == "application/json" {
w.Header().Set("Content-Type", "application/json")
out, err := json.MarshalIndent(data, "", " ")
if err != nil {
fmt.Printf("Failed to parse json: %s\n", err.Error())
http.Error(w, "Errno 201", http.StatusInternalServerError)
return
}
w.WriteHeader(200)
io.WriteString(w, string(out))
} else {
if err := h.templates.ExecuteTemplate(w, "admin_dashboard", data); err != nil {
fmt.Printf("Failed to execute template: %s\n", err.Error())
http.Error(w, "Errno 203", http.StatusInternalServerError)
return
}
}
}
func (h *HTTP) ViewAdminBins(w http.ResponseWriter, r *http.Request) {
type Bins struct {
Available []ds.Bin `json:"available"`
}
type Data struct {
Bins Bins `json:"bins"`
//Files []ds.File `json:"files"`
BucketInfo s3.BucketInfo `json:"bucketinfo"`
Page string `json:"page"`
DBInfo ds.Info `json:"db_info"`
}
var data Data
//data.Page = "about"
//data.BucketInfo = h.s3.GetBucketInfo()
//info, err := h.dao.Info().GetInfo()
//if err != nil {
// fmt.Printf("Unable to GetInfo(): %s\n", err.Error())
// http.Error(w, "Errno 326", http.StatusInternalServerError)
// return
//}
//data.DBInfo = info
binsAvailable, err := h.dao.Bin().GetAll()
if err != nil {
fmt.Printf("Unable to GetAll(): %s\n", err.Error())
http.Error(w, "Errno 200", http.StatusInternalServerError)
return
}
var bins Bins
bins.Available = binsAvailable
data.Bins = bins
if r.Header.Get("accept") == "application/json" {
w.Header().Set("Content-Type", "application/json")
out, err := json.MarshalIndent(data, "", " ")
if err != nil {
fmt.Printf("Failed to parse json: %s\n", err.Error())
http.Error(w, "Errno 201", http.StatusInternalServerError)
return
}
w.WriteHeader(200)
io.WriteString(w, string(out))
} else {
if err := h.templates.ExecuteTemplate(w, "admin_bins", data); err != nil {
fmt.Printf("Failed to execute template: %s\n", err.Error())
http.Error(w, "Errno 203", http.StatusInternalServerError)
return
}
}
}
func (h *HTTP) ViewAdminLog(w http.ResponseWriter, r *http.Request) {
type Data struct {
Transactions []ds.Transaction `json:"transactions"`
}
var data Data
params := mux.Vars(r)
inputCategory := params["category"]
inputFilter := params["filter"]
if inputCategory == "bin" {
bin := inputFilter
trs, err := h.dao.Transaction().GetByBin(bin)
if err != nil {
http.Error(w, "Errno 361", http.StatusInternalServerError)
return
}
data.Transactions = trs
} else if inputCategory == "ip" {
ip := inputFilter
trs, err := h.dao.Transaction().GetByIP(ip)
if err != nil {
http.Error(w, "Errno 361", http.StatusInternalServerError)
return
}
data.Transactions = trs
} else if inputCategory == "cid" {
cid := inputFilter
trs, err := h.dao.Transaction().GetByClientId(cid)
if err != nil {
http.Error(w, "Errno 361", http.StatusInternalServerError)
return
}
data.Transactions = trs
}
if err := h.templates.ExecuteTemplate(w, "log", data); err != nil {
fmt.Printf("Failed to execute template: %s\n", err.Error())
http.Error(w, "Errno 203", http.StatusInternalServerError)
return
}
}
func (h *HTTP) ViewAdminCleanup(w http.ResponseWriter, r *http.Request) {
type Data struct {
Objects []string `json:"objects"`
////Files []ds.File `json:"files"`
//BucketInfo s3.BucketInfo `json:"bucketinfo"`
//Page string `json:"page"`
Bins []ds.Bin `json:"bins"`
}
objects, err := h.s3.ListObjects()
if err != nil {
http.Error(w, "Errno 262", http.StatusInternalServerError)
return
}
bins, err := h.dao.Bin().GetAll()
if err != nil {
http.Error(w, "Errno 361", http.StatusInternalServerError)
return
}
var allbins []string
for _, bin := range bins {
b := sha256.New()
b.Write([]byte(bin.Id))
hash := fmt.Sprintf("%x", b.Sum(nil))
allbins = append(allbins, hash)
}
for _, object := range objects {
splits := strings.Split(object, "/")
if len(splits) == 2 {
hash := splits[0]
if inStringSlice(hash, allbins) {
fmt.Printf("Match\n")
} else {
fmt.Printf("No match\n")
}
} else {
fmt.Printf("No match. Weird length: %d\n", len(splits))
}
}
var data Data
data.Objects = objects
data.Bins = bins
if err := h.templates.ExecuteTemplate(w, "cleanup", data); err != nil {
fmt.Printf("Failed to execute template: %s\n", err.Error())
http.Error(w, "Errno 203", http.StatusInternalServerError)
return
}
}