-
Notifications
You must be signed in to change notification settings - Fork 157
/
guest_links.go
151 lines (126 loc) · 4.19 KB
/
guest_links.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
package handlers
import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/mtlynch/picoshare/v2/handlers/parse"
"github.com/mtlynch/picoshare/v2/picoshare"
"github.com/mtlynch/picoshare/v2/random"
)
const (
GuestLinkIDLength = 16
GuestLinkByteLimitMinimum = 1024 * 1024
)
type GuestLinkPostResponse struct {
ID string `json:"id"`
}
// Omit visually similar characters (I,l,1), (0,O)
var guestLinkIDCharacters = []rune("abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789")
func (s Server) guestLinksPost() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
gl, err := guestLinkFromRequest(r)
if err != nil {
http.Error(w, fmt.Sprintf("Invalid request: %v", err), http.StatusBadRequest)
return
}
gl.ID = generateGuestLinkID()
gl.Created = time.Now()
if err := s.getDB(r).InsertGuestLink(gl); err != nil {
log.Printf("failed to save guest link: %v", err)
http.Error(w, fmt.Sprintf("Failed to save guest link: %v", err), http.StatusInternalServerError)
return
}
respondJSON(w, GuestLinkPostResponse{ID: string(gl.ID)})
}
}
func (s Server) guestLinksDelete() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id, err := parseGuestLinkID(mux.Vars(r)["id"])
if err != nil {
log.Printf("failed to parse guest link ID %s: %v", mux.Vars(r)["id"], err)
http.Error(w, fmt.Sprintf("Invalid guest link ID: %v", err), http.StatusBadRequest)
return
}
if err := s.getDB(r).DeleteGuestLink(id); err != nil {
log.Printf("failed to delete guest link: %v", err)
http.Error(w, fmt.Sprintf("Failed to delete guest link: %v", err), http.StatusInternalServerError)
return
}
}
}
func guestLinkFromRequest(r *http.Request) (picoshare.GuestLink, error) {
var payload struct {
Label string `json:"label"`
Expiration string `json:"expirationTime"`
MaxFileBytes *uint64 `json:"maxFileBytes"`
MaxFileUploads *int `json:"maxFileUploads"`
}
err := json.NewDecoder(r.Body).Decode(&payload)
if err != nil {
log.Printf("failed to decode JSON request: %v", err)
return picoshare.GuestLink{}, err
}
label, err := parse.GuestLinkLabel(payload.Label)
if err != nil {
return picoshare.GuestLink{}, err
}
expiration, err := parse.Expiration(payload.Expiration)
if err != nil {
return picoshare.GuestLink{}, err
}
maxFileBytes, err := parseMaxFileBytes(payload.MaxFileBytes)
if err != nil {
return picoshare.GuestLink{}, err
}
maxFileUploads, err := parseUploadCountLimit(payload.MaxFileUploads)
if err != nil {
return picoshare.GuestLink{}, err
}
return picoshare.GuestLink{
Label: label,
Expires: expiration,
MaxFileBytes: maxFileBytes,
MaxFileUploads: maxFileUploads,
}, nil
}
func parseMaxFileBytes(limitRaw *uint64) (picoshare.GuestUploadMaxFileBytes, error) {
if limitRaw == nil {
return picoshare.GuestUploadUnlimitedFileSize, nil
}
if *limitRaw < GuestLinkByteLimitMinimum {
return nil, fmt.Errorf("guest upload size limit must be at at least %d bytes", GuestLinkByteLimitMinimum)
}
return picoshare.GuestUploadMaxFileBytes(limitRaw), nil
}
func parseUploadCountLimit(limitRaw *int) (picoshare.GuestUploadCountLimit, error) {
if limitRaw == nil {
return picoshare.GuestUploadUnlimitedFileUploads, nil
}
if *limitRaw <= 0 {
return nil, errors.New("guest upload count limit must be a positive number")
}
return picoshare.GuestUploadCountLimit(limitRaw), nil
}
func generateGuestLinkID() picoshare.GuestLinkID {
return picoshare.GuestLinkID(random.String(GuestLinkIDLength, guestLinkIDCharacters))
}
func parseGuestLinkID(s string) (picoshare.GuestLinkID, error) {
if len(s) != GuestLinkIDLength {
return picoshare.GuestLinkID(""), fmt.Errorf("guest link ID (%v) has invalid length: got %d, want %d", s, len(s), GuestLinkIDLength)
}
// We could do this outside the function and store the result.
idCharsHash := map[rune]bool{}
for _, c := range guestLinkIDCharacters {
idCharsHash[c] = true
}
for _, c := range s {
if _, ok := idCharsHash[c]; !ok {
return picoshare.GuestLinkID(""), fmt.Errorf("entry ID (%s) contains invalid character: %s", s, string(c))
}
}
return picoshare.GuestLinkID(s), nil
}