-
Notifications
You must be signed in to change notification settings - Fork 0
/
snippet.go
77 lines (66 loc) · 1.66 KB
/
snippet.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
package replay
import (
"bytes"
"crypto/sha1"
"encoding/base64"
"io"
"net/http"
"strings"
"appengine"
"appengine/datastore"
)
const hello = `let myVar = "Hello";
print_endline myVar;`
const salt = "[replace this with something unique]"
const maxSnippetSize = 64 * 1024
type snippet struct {
Body []byte
}
func (s *snippet) ID() string {
h := sha1.New()
io.WriteString(h, salt)
h.Write(s.Body)
sum := h.Sum(nil)
b := make([]byte, base64.URLEncoding.EncodedLen(len(sum)))
base64.URLEncoding.Encode(b, sum)
return string(b)[:10]
}
func getSnippet(r *http.Request) *snippet {
snip := &snippet{Body: []byte(hello)}
if !strings.HasPrefix(r.URL.Path, "/p/") {
return snip
}
c := appengine.NewContext(r)
id := r.URL.Path[3:]
key := datastore.NewKey(c, "Snippet", id, 0, nil)
err := datastore.Get(c, key, snip)
if err != nil {
if err != datastore.ErrNoSuchEntity {
c.Errorf("loading Snippet: %v", err)
}
return nil
}
return snip
}
func saveSnippet(r *http.Request) (string, error) {
c := appengine.NewContext(r)
var body bytes.Buffer
_, err := io.Copy(&body, io.LimitReader(r.Body, maxSnippetSize+1))
r.Body.Close()
if err != nil {
c.Errorf("reading Body: %v", err)
return "", httpError{"Server Error", http.StatusInternalServerError}
}
if body.Len() > maxSnippetSize {
return "", httpError{"Snippet is too large", http.StatusRequestEntityTooLarge}
}
snip := &snippet{Body: body.Bytes()}
id := snip.ID()
key := datastore.NewKey(c, "Snippet", id, 0, nil)
_, err = datastore.Put(c, key, snip)
if err != nil {
c.Errorf("putting Snippet: %v", err)
return "", httpError{"Server Error", http.StatusInternalServerError}
}
return id, nil
}