-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
171 lines (155 loc) · 4.58 KB
/
util.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
package main
import (
"fmt"
"html"
"html/template"
"os"
"path/filepath"
"sort"
"strings"
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/logrusorgru/aurora"
)
// CleanPath makes a path safe for use with filepath.Join. This is done by not
// only cleaning the path, but also (if the path is relative) adding a leading
// '/' and cleaning it (then removing the leading '/'). This ensures that a
// path resulting from prepending another path will always resolve to lexically
// be a subdirectory of the prefixed path. This is all done lexically, so paths
// that include symlinks won't be safe as a result of using CleanPath.
func CleanPath(path string) string {
// Deal with empty strings nicely.
if path == "" {
return ""
}
// Ensure that all paths are cleaned (especially problematic ones like
// "/../../../../../" which can cause lots of issues).
path = filepath.Clean(path)
// If the path isn't absolute, we need to do more processing to fix paths
// such as "../../../../<etc>/some/path". We also shouldn't convert absolute
// paths to relative ones.
if !filepath.IsAbs(path) {
path = filepath.Clean(string(os.PathSeparator) + path)
// This can't fail, as (by definition) all paths are relative to root.
path, _ = filepath.Rel(string(os.PathSeparator), path)
}
// Clean the path again for good measure.
return filepath.Clean(path)
}
func sendError(c *gin.Context, status int, message string) {
sendWithFormat(c, status, map[string]interface{}{
"status": status,
"message": message,
})
}
func dataToHtml(data interface{}) string {
switch v := data.(type) {
case map[string]interface{}:
out := "<ul>"
for k, val := range v {
out += fmt.Sprintf("<li><strong>%v</strong>:%v</li>", html.EscapeString(k), dataToHtml(val))
}
out += "</ul>"
return out
case []interface{}:
out := "<ul>"
for _, val := range v {
out += fmt.Sprintf("<li>%v</li>", dataToHtml(val))
}
out += "</ul>"
return out
default:
return fmt.Sprintf("<pre>%v</pre>", html.EscapeString(fmt.Sprintf("%v", v)))
}
}
func sendWithFormat(c *gin.Context, status int, data map[string]interface{}, extraTemplateData ...map[string]interface{}) {
accept := strings.Split(strings.ToLower(c.GetHeader("Accept")), ",")[0]
if _, forceJson := c.GetQuery("json"); forceJson {
accept = "application/json"
}
switch accept {
case "application/json":
fallthrough
case "text/json":
fallthrough
case "json":
c.JSON(status, data)
return
case "text/html":
fallthrough
case "application/xhtml+xml":
tplData := map[string]interface{}{
"Data": data,
"DataHTML": template.HTML(dataToHtml(data)),
"Status": status,
"PageType": "unknown",
}
for _, e := range extraTemplateData {
for k, v := range e {
tplData[k] = v
}
}
c.HTML(status, "with-format.html", tplData)
return
}
if strings.HasPrefix(c.GetHeader("User-Agent"), "curl/") || strings.HasPrefix(strings.ToLower(c.GetHeader("User-Agent")), "wget/") {
colored := ""
keys := make([]string, 0)
data = collapseSizes(data)
for k, _ := range data {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := data[k]
colored += aurora.Yellow(k).String()
colored += aurora.Gray(10, ": ").String()
switch typed := v.(type) {
case string:
colored += aurora.Green(typed).String()
case byte:
colored += aurora.Cyan(fmt.Sprintf("%v", typed)).String()
case int:
colored += aurora.Cyan(fmt.Sprintf("%v", typed)).String()
case int64:
colored += aurora.Cyan(fmt.Sprintf("%v", typed)).String()
case uint:
colored += aurora.Cyan(fmt.Sprintf("%v", typed)).String()
case uint64:
colored += aurora.Cyan(fmt.Sprintf("%v", typed)).String()
case float32:
colored += aurora.Cyan(fmt.Sprintf("%v", typed)).String()
case float64:
colored += aurora.Cyan(fmt.Sprintf("%v", typed)).String()
default:
marsh, _ := json.Marshal(v)
colored += aurora.Reset(string(marsh)).String()
}
colored += "\n"
}
c.String(status, colored)
return
}
c.JSON(status, data)
}
func addCommandsToResponse(data map[string]interface{}, cleanedPath string) (out map[string]interface{}) {
basename := filepath.Base(cleanedPath)
out = map[string]interface{}{
"cmdDownload": fmt.Sprintf("curl %v -o %v", data["url"], basename),
}
for k, v := range data {
out[k] = v
}
return
}
func collapseSizes(data map[string]interface{}) (out map[string]interface{}) {
out = map[string]interface{}{}
for k, v := range data {
out[k] = v
}
if _, ok := out["size"]; ok {
out["size"] = fmt.Sprintf("%v (%v B)", out["size"], out["sizeExact"])
delete(out, "sizeExact")
}
return
}