-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
195 lines (162 loc) · 4 KB
/
data.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
/*
* Manage all "/data/" related routes.
* This is mostly a wrapper above the corresponding
* database functions (db-data.go)
*/
import (
"fmt"
"github.com/mbivert/auth"
"os"
"net/http"
"path/filepath"
)
// Simple wrappers to guard the access to dir (global)
func writeDataFile(fn, content string) error {
fpath := filepath.Join(dir, fn)
return writeFile(fpath, []byte(content))
}
func readDataFile(fn string) (string, error) {
fpath := filepath.Join(dir, fn)
x, err := os.ReadFile(fpath)
return string(x), err
}
// TODO: have per user size limits
func SetData(db *DB, in *SetDataIn, out *SetDataOut) error {
ok, uid, err := auth.CheckToken(in.Token)
if err != nil {
return err
} else if !ok {
return fmt.Errorf("Not connected!")
}
in.UserId = uid
// We're trying to update an already existing entry
if in.Id == -1 {
in.File = mkRandPath(uid)
return db.AddData(in)
}
return db.UpdateData(in)
}
func GetBooks(db *DB, in *GetBooksIn, out *GetBooksOut) error {
ok, uid, err := auth.CheckToken(in.Token)
if err != nil {
return err
}
if !ok {
return fmt.Errorf("Not connected!")
}
out.Books, err = db.GetBooks(uid)
return err
}
func GetAbout(db *DB, in *GetAboutIn, out *GetAboutOut) (err error) {
out.Datas, err = db.GetAbouts(C.ZmId)
return err
}
// NOTE: the meta datas are computed from user preferences: they
// are used to download the files which will be needed for inspection.
// This should work for anonymous users as well; when authenticated, in
// addition to public files, we'll want to also be able to access owned files.
func GetMetas(db *DB, in *GetMetasIn, out *GetMetasOut) (err error) {
ok, uid, err := auth.CheckToken(in.Token)
if err != nil {
return err
}
if !ok {
uid = -1
}
out.Metas, err = db.GetMetas(uid, in.Names)
return err
}
// "Special" request: this is a "GET /data/..." request; it's
// been kept as a GET because we want to involve the browser cache
// For more, see 'DONE.md:/^## medium @data-organisation/'
func GETData(db *DB, root string, w http.ResponseWriter, r *http.Request) {
var ok bool
var uid auth.UserId
tok, err := auth.GetCookie(w, r)
if err != nil {
goto Err
}
ok, uid, err = auth.CheckToken(tok)
if err != nil {
goto Err
}
// e.g. token expired
if !ok {
uid = -1
}
ok, err = db.CanGet(uid, r.URL.Path)
if !ok {
err = fmt.Errorf("Access forbidden")
}
if err != nil {
goto Err
}
http.ServeFile(w, r, filepath.Join(root, r.URL.Path))
return
Err:
fails(w, err)
}
func loadContents(ds []Data) (error) {
for i, d := range ds {
xs, err := readDataFile(d.File)
if err != nil {
return err
}
ds[i].Content = string(xs)
}
return nil
}
func GetMyData(db *DB, in *GetMyDataIn, out *GetMyDataOut) (err error) {
ok, uid, err := auth.CheckToken(in.Token)
if err != nil {
return err
}
if !ok {
return fmt.Errorf("Not connected!")
}
out.Datas, err = db.GetDataOf(uid)
if err != nil {
return err
}
return loadContents(out.Datas)
}
func GetLicenses(db *DB, in *GetLicensesIn, out *GetLicensesOut) (err error) {
out.Licenses, err = db.GetLicenses()
return err
}
func initData(db *DB) *http.ServeMux {
mux := http.NewServeMux()
// TODO: JS typing
// TODO: add an extra CLI parameter --dev or so, and make it so that
// the getCaptcha route returns the answer alongside it, so that we
// can test things in the front.
mux.HandleFunc(
"/set/data",
auth.Wrap[*DB, SetDataIn, SetDataOut](db, SetData),
)
mux.HandleFunc(
"/get/books",
auth.Wrap[*DB, GetBooksIn, GetBooksOut](db, GetBooks),
)
mux.HandleFunc(
"/get/about",
auth.Wrap[*DB, GetAboutIn, GetAboutOut](db, GetAbout),
)
mux.HandleFunc(
"/get/metas",
auth.Wrap[*DB, GetMetasIn, GetMetasOut](db, GetMetas),
)
mux.HandleFunc(
"/get/my/data",
auth.Wrap[*DB, GetMyDataIn, GetMyDataOut](db, GetMyData),
)
mux.HandleFunc(
"/get/licenses",
auth.Wrap[*DB, GetLicensesIn, GetLicensesOut](db, GetLicenses),
)
mux.HandleFunc("GET /data/", func(w http.ResponseWriter, r *http.Request) {
GETData(db, dir, w, r)
})
return mux
}