-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathshare.go
39 lines (34 loc) · 968 Bytes
/
share.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
package main
import (
"crypto/sha256"
"encoding/base64"
"fmt"
"net/http"
)
func stringToHash(code string) string {
hash := sha256.Sum256([]byte(code))
b := base64.URLEncoding.EncodeToString(hash[0:])
// Web sites don’t always linkify a trailing underscore, making it seem like
// the link is broken. If there is an underscore at the end of the substring,
// extend it until there is not.
hashLen := 11
for hashLen <= len(b) && b[hashLen-1] == '_' {
hashLen++
}
return string(b)[:hashLen]
}
func HandleShare(w http.ResponseWriter, r *http.Request) {
// Use MB of memory before paging out to disk
if err := r.ParseMultipartForm(1000000); err != nil {
fmt.Fprintf(w, "Unable to parse form: %v", err)
return
}
code := r.FormValue("jsonnet")
id := stringToHash(code)
if err := store.Store(id, code); err != nil {
fmt.Fprintf(w, "Unable to store code: %v", err)
return
}
fid := fmt.Sprintf("%s/j/%s", r.Host, id)
fmt.Fprintf(w, fid)
}