forked from couchbaselabs/cbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
108 lines (96 loc) · 2.51 KB
/
list.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
package main
import (
"encoding/json"
"log"
"strings"
)
type fileListing struct {
Files map[string]interface{} `json:"files"`
Dirs map[string]interface{} `json:"dirs"`
Path string `json:"path"`
}
func toStringJoin(in []interface{}, sep string) string {
s := []string{}
for _, a := range in {
s = append(s, a.(string))
}
return strings.Join(s, sep)
}
func listFiles(path string, includeMeta bool,
depth int) (fileListing, error) {
viewRes := struct {
Rows []struct {
Key []interface{}
Value map[string]interface{}
}
}{}
// use the requested path to build our view query parameters
startKey := []interface{}{}
if path != "" {
for _, k := range strings.Split(path, "/") {
startKey = append(startKey, k)
}
}
endKey := make([]interface{}, len(startKey)+1, len(startKey)+1)
copy(endKey, startKey)
endMarker := json.RawMessage([]byte{'{', '}'})
endKey[len(startKey)] = &endMarker
groupLevel := len(startKey) + depth
// query the view
err := couchbase.ViewCustom("cbfs", "file_browse",
map[string]interface{}{
"group_level": groupLevel,
"start_key": startKey,
"end_key": endKey,
}, &viewRes)
if err != nil {
return fileListing{}, err
}
// use the view result to build a list of keys
keys := make([]string, len(viewRes.Rows), len(viewRes.Rows))
for i, r := range viewRes.Rows {
keys[i] = toStringJoin(r.Key, "/")
}
// do a multi-get on the all the keys returned
bulkResult := couchbase.GetBulk(keys)
// divide items up into files and directories
files := map[string]interface{}{}
dirs := map[string]interface{}{}
for _, r := range viewRes.Rows {
key := toStringJoin(r.Key, "/")
subkey := r.Key
if len(r.Key) > depth {
subkey = r.Key[len(r.Key)-depth:]
}
name := toStringJoin(subkey, "/")
res, ok := bulkResult[key]
if ok == true {
// this means we have a file
rv := map[string]interface{}{}
err := json.Unmarshal(res.Body, &rv)
if err != nil {
log.Printf("Error deserializing json, ignoring: %v", err)
} else {
if includeMeta {
files[name] = rv
} else {
files[name] = map[string]interface{}{}
}
}
} else {
// no record in the multi-get metans this is a directory
dirs[name] = map[string]interface{}{
"descendants": r.Value["count"],
"size": int64(r.Value["sum"].(float64)),
"smallest": int64(r.Value["min"].(float64)),
"largest": int64(r.Value["max"].(float64)),
}
}
}
rv := fileListing{
Path: "/" + path,
Dirs: dirs,
Files: files,
}
return rv, nil
}