-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
437 lines (353 loc) · 10.2 KB
/
server.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package main
import (
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"time"
"wfs-ls/demodata"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/cors"
"github.com/jinzhu/configor"
"github.com/unrolled/render"
"github.com/xbsoftware/wfs"
db "github.com/xbsoftware/wfs-db"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
var format = render.New()
type Response struct {
Invalid bool `json:"invalid"`
Error string `json:"error"`
ID string `json:"id"`
}
type FSFeatures struct {
Preview map[string]bool `json:"preview"`
Meta map[string]bool `json:"meta"`
}
var drive wfs.Drive
var conn *sqlx.DB
var features = FSFeatures{
Preview: map[string]bool{},
Meta: map[string]bool{},
}
type AppConfig struct {
DataFolder string `default:"/tmp/docs"`
UserFolder string `default:"./demodata/avatars"`
Port string
Preview string
UploadLimit int64
Readonly bool
ResetOnStart bool
DB DBConfig
}
type DBConfig struct {
Host string `default:"localhost"`
Port string `default:"3306"`
User string `default:"root"`
Password string `default:"1"`
Database string `default:"files"`
}
var Config AppConfig
func main() {
flag.StringVar(&Config.DataFolder, "data", "", "location of data folder")
flag.StringVar(&Config.Preview, "preview", "", "url of preview generation service")
flag.BoolVar(&Config.ResetOnStart, "reset", false, "reset data in DB")
flag.BoolVar(&Config.Readonly, "readonly", false, "readonly mode")
flag.Int64Var(&Config.UploadLimit, "limit", 10_000_000, "max file size to upload")
flag.StringVar(&Config.Port, "port", ":3200", "port for web server")
flag.Parse()
configor.New(&configor.Config{ENVPrefix: "APP", Silent: true}).Load(&Config, "config.yml")
// configure features
features.Meta["audio"] = true
features.Meta["image"] = true
if Config.Preview != "none" {
features.Preview["image"] = true
if Config.Preview != "" {
features.Preview["document"] = true
features.Preview["code"] = true
}
}
// common drive access
var err error
driveConfig := wfs.DriveConfig{Verbose: false}
driveConfig.Operation = &wfs.OperationConfig{PreventNameCollision: true}
if Config.Readonly {
temp := wfs.Policy(&wfs.ReadOnlyPolicy{})
driveConfig.Policy = &temp
}
connStr := fmt.Sprintf("%s:%s@(%s:%s)/%s?multiStatements=true&parseTime=true",
Config.DB.User, Config.DB.Password, Config.DB.Host, Config.DB.Port, Config.DB.Database)
conn, err = sqlx.Connect("mysql", connStr)
if err != nil {
log.Fatal(err)
}
migration(conn)
os.Mkdir(Config.DataFolder, 0777)
drive, err = db.NewDBDrive(conn, Config.DataFolder, "entity", User.Root, &driveConfig)
if err != nil {
log.Fatal(err)
}
if Config.ResetOnStart {
demodata.ResetDemoData(drive, conn)
}
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
cors := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
AllowCredentials: true,
MaxAge: 300,
})
r.Use(cors.Handler)
addExtrasRoutes(r)
addFilesRoutes(r)
addTrashRoutes(r)
r.Get("/icons/{size}/{type}/{name}", func(w http.ResponseWriter, r *http.Request) {
size := chi.URLParam(r, "size")
name := chi.URLParam(r, "name")
ftype := chi.URLParam(r, "type")
http.ServeFile(w, r, getIconURL(size, ftype, name, ""))
})
r.Get("/icons/{skin}/{size}/{type}/{name}", func(w http.ResponseWriter, r *http.Request) {
skin := chi.URLParam(r, "skin")
size := chi.URLParam(r, "size")
name := chi.URLParam(r, "name")
ftype := chi.URLParam(r, "type")
http.ServeFile(w, r, getIconURL(size, ftype, name, skin))
})
r.Get("/preview", getFilePreview)
r.Get("/search", func(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
search := r.URL.Query().Get("search")
data, err := drive.Search(id, search)
if err != nil {
format.Text(w, 500, err.Error())
return
}
format.JSON(w, 200, data)
})
r.Get("/folders", func(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
if id == "" {
id = "/"
}
data, err := drive.List(id, &wfs.ListConfig{
Nested: true,
SubFolders: true,
SkipFiles: true,
Exclude: func(name string) bool { return strings.HasPrefix(name, ".") },
})
if err != nil {
format.Text(w, 500, err.Error())
return
}
err = format.JSON(w, 200, data)
})
r.Post("/copy", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
id := r.Form.Get("id")
to := r.Form.Get("to")
if id == "" || to == "" {
panic("both, 'id' and 'to' parameters must be provided")
}
id, err := drive.Copy(id, to, "")
if err != nil {
format.JSON(w, 500, Response{Invalid: true, Error: err.Error()})
return
}
info, err := drive.Info(id)
if err != nil {
format.JSON(w, 500, Response{Invalid: true, Error: err.Error()})
return
}
format.JSON(w, 200, info)
})
r.Post("/move", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
id := r.Form.Get("id")
to := r.Form.Get("to")
if id == "" || to == "" {
panic("both, 'id' and 'to' parameters must be provided")
}
id, err := drive.Move(id, to, "")
if err != nil {
format.JSON(w, 500, Response{Invalid: true, Error: err.Error()})
return
}
info, err := drive.Info(id)
if err != nil {
format.JSON(w, 500, Response{Invalid: true, Error: err.Error()})
return
}
format.JSON(w, 200, info)
})
r.Post("/rename", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
id := r.Form.Get("id")
name := r.Form.Get("name")
if id == "" || name == "" {
panic("both, 'id' and 'name' parameters must be provided")
}
id, err := drive.Move(id, "", name)
if err != nil {
format.JSON(w, 500, Response{Invalid: true, Error: err.Error()})
return
}
format.JSON(w, 200, Response{ID: id})
})
r.Post("/upload", func(w http.ResponseWriter, r *http.Request) {
handleUpload(w, r, true)
})
r.Post("/makefile", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
id := r.Form.Get("id")
name := r.Form.Get("name")
if id == "" || name == "" {
panic("both, 'id' and 'name' parameters must be provided")
}
id, err := drive.Make(id, name, false)
if err != nil {
format.JSON(w, 500, Response{Invalid: true, Error: err.Error()})
return
}
info, err := drive.Info(id)
if err != nil {
format.JSON(w, 500, Response{Invalid: true, Error: err.Error()})
return
}
format.JSON(w, 200, info)
})
r.Post("/makedir", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
id := r.Form.Get("id")
name := r.Form.Get("name")
if id == "" || name == "" {
panic("both, 'id' and 'name' parameters must be provided")
}
id, err := drive.Make(id, name, true)
if err != nil {
format.JSON(w, 500, Response{Invalid: true, Error: err.Error()})
return
}
info, err := drive.Info(id)
if err != nil {
format.JSON(w, 500, Response{Invalid: true, Error: err.Error()})
return
}
format.JSON(w, 200, info)
})
r.Get("/text", func(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
if id == "" {
panic("id not provided")
}
data, err := drive.Read(id)
if err != nil {
panic(err)
}
w.Header().Add("Content-type", "text/plain")
io.Copy(w, data)
})
r.Post("/text", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
id := r.Form.Get("id")
content := r.Form.Get("content")
if id == "" {
panic("id not provided")
}
err = drive.Write(id, strings.NewReader(content))
if err != nil {
panic(err)
}
info, _ := saveVersion(id, nil)
format.JSON(w, 200, info)
})
r.Get("/direct", func(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
if id == "" {
panic("id not provided")
}
info, err := drive.Info(id)
if err != nil {
format.Text(w, 500, "Access denied")
return
}
data, err := drive.Read(id)
if err != nil {
format.Text(w, 500, "Access denied")
return
}
disposition := "inline"
_, ok := r.URL.Query()["download"]
if ok {
disposition = "attachment"
}
w.Header().Set("Content-Disposition", disposition+"; filename=\""+info.Name+"\"")
http.ServeContent(w, r, "", time.Now(), data)
})
r.Post("/direct", func(w http.ResponseWriter, r *http.Request) {
handleUpload(w, r, false)
})
r.Get("/info", getInfo)
r.Get("/meta", getMetaInfo)
log.Printf("Starting webserver at port " + Config.Port)
err = http.ListenAndServe(Config.Port, r)
if err != nil {
log.Println(err.Error())
}
}
func handleUpload(w http.ResponseWriter, r *http.Request, makeNew bool) {
// buffer for file parsing, this is NOT the max upload size
var limit = int64(32 << 20) // default is 32MB
if Config.UploadLimit < limit {
limit = Config.UploadLimit
}
// this one limit max upload size
r.Body = http.MaxBytesReader(w, r.Body, Config.UploadLimit)
r.ParseMultipartForm(limit)
file, handler, err := r.FormFile("upload")
if err != nil {
panic("Error Retrieving the File")
}
defer file.Close()
fileID := r.URL.Query().Get("id")
if makeNew {
fileID, err = drive.Make(fileID, handler.Filename, false)
if err != nil {
format.Text(w, 500, "Access Denied")
return
}
}
err = drive.Write(fileID, file)
if err != nil {
format.Text(w, 500, "Access Denied")
return
}
info, err := saveVersion(fileID, nil)
format.JSON(w, 200, info)
}
func saveVersion(id string, restore *time.Time) (*wfs.File, error) {
var data db.DBFile
err := conn.Get(&data, "select entity.* from entity where path = ?", id)
if err != nil {
return nil, err
}
out := &wfs.File{ID: data.Path, Name: data.FileName, Date: data.LastModTime.Unix(), Size: data.FileSize, Type: wfs.GetType(data.FileName, data.IsDir())}
// get previous version
var older db.DBFile
err = conn.Get(&older, "select content from entity_edit where entity_id = ? order by modified desc limit 1;", data.ID)
// write new edit version
_, err = conn.Exec("INSERT INTO entity_edit(entity_id, content, modified, user_id, previous, origin) VALUES(?, ?, ?, ?, ?, ?)", data.ID, data.Content, data.LastModTime, User.Root, older.Content, restore)
if err != nil {
log.Println(err)
return out, err
}
return out, nil
}