-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
63 lines (51 loc) · 1.09 KB
/
utils.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
package main
import (
"crypto/rand"
"encoding/json"
"fmt"
"mime"
"net/http"
"net/url"
"path"
)
func contains(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func validateFileType(fileBytes *[]byte) (string, error) {
// check file type, detectcontenttype only needs the first 512 bytes
filetype := http.DetectContentType(*fileBytes)
fileEndings, err := mime.ExtensionsByType(filetype)
if err != nil {
return "", &fileTypeReadError
}
return fileEndings[0], nil
}
func randomToken(len int) string {
b := make([]byte, len)
_, err := rand.Read(b)
if err != nil {
return ""
}
return fmt.Sprintf("%x", b)
}
// createResponse returns a JSON object for ShareX upload requests.
func createResponse(prefix string, file string) []byte {
u, _ := url.Parse("http://" + config.Host)
u.Path = path.Join(u.Path, prefix, file)
res := response{
Status: http.StatusOK,
Result: result{
Name: file,
URL: u.String(),
// doesnt do anything yet.
//DeleteKey: randomToken(6)},
},
}
j, _ := json.Marshal(&res)
return j
}