-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
446 lines (363 loc) · 9.97 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
438
439
440
441
442
443
444
445
446
package main
import (
"flag"
"io"
"log"
"net/http"
"strings"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/cors"
"github.com/jinzhu/configor"
"github.com/rwcarlsen/goexif/exif"
"github.com/rwcarlsen/goexif/tiff"
"github.com/unrolled/render"
"github.com/xbsoftware/wfs"
local "github.com/xbsoftware/wfs-local"
)
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 features = FSFeatures{
Preview: map[string]bool{},
Meta: map[string]bool{},
}
type AppConfig struct {
DataFolder string
Port string
Preview string
UploadLimit int64
Readonly bool
}
var Config AppConfig
func main() {
flag.StringVar(&Config.Preview, "preview", "", "url of preview generation service")
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()
args := flag.Args()
if len(args) > 0 {
Config.DataFolder = args[0]
}
configor.New(&configor.Config{ENVPrefix: "APP", Silent: true}).Load(&Config, "config.yml")
// configure features
features.Meta["audio"] = true
features.Meta["image"] = true
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: true}
driveConfig.Operation = &wfs.OperationConfig{PreventNameCollision: true}
if Config.Readonly {
temp := wfs.Policy(&wfs.ReadOnlyPolicy{})
driveConfig.Policy = &temp
}
drive, err = local.NewLocalDrive(Config.DataFolder, &driveConfig)
if err != nil {
log.Fatal(err)
}
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)
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("/files", func(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
if id == "" {
id = "/"
}
search := r.URL.Query().Get("search")
var config *wfs.ListConfig
if search == "" {
config = &wfs.ListConfig{
Nested: true,
Exclude: func(name string) bool { return strings.HasPrefix(name, ".") },
}
} else {
search = strings.ToLower(search)
config = &wfs.ListConfig{
SubFolders: true,
Include: func(name string) bool { return strings.Contains(strings.ToLower(name), search) },
Exclude: func(name string) bool { return strings.HasPrefix(name, ".") },
}
}
data, err := drive.List(id, config)
if err != nil {
format.Text(w, 500, err.Error())
return
}
err = 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) {
// 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()
base := r.URL.Query().Get("id")
filename := r.Form.Get("upload_fullpath")
if filename == "" {
filename = handler.Filename
}
parts := strings.Split(filename, "/")
if len(parts) > 1 {
for _, p := range parts[:len(parts)-1] {
if !drive.Exists(base + "/" + p) {
id, err := drive.Make(base, p, true)
if err != nil {
format.JSON(w, 500, Response{Invalid: true, Error: err.Error()})
return
}
base = id
} else {
base = base + "/" + p
}
}
}
fileID, err := drive.Make(base, parts[len(parts)-1], 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 := drive.Info(fileID)
if err != nil {
format.Text(w, 500, "Access Denied")
return
}
format.JSON(w, 200, info)
})
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.Post("/delete", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
id := r.Form.Get("id")
if id == "" {
panic("path not provided")
}
err := drive.Remove(id)
if err != nil {
format.JSON(w, 500, Response{Invalid: true, Error: err.Error()})
return
}
format.JSON(w, 200, Response{})
})
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, _ := drive.Info(id)
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.Get("/info", getInfo)
r.Get("/meta", getMetaInfo)
log.Printf("Starting webserver at port " + Config.Port)
http.ListenAndServe(Config.Port, r)
}
type walkFunc func(exif.FieldName, *tiff.Tag) error
func (f walkFunc) Walk(name exif.FieldName, tag *tiff.Tag) error {
return f(name, tag)
}