-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrclone_backend.go
More file actions
137 lines (108 loc) · 2.45 KB
/
Copy pathrclone_backend.go
File metadata and controls
137 lines (108 loc) · 2.45 KB
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
package gemdrive
import (
"encoding/json"
"fmt"
"io"
"os/exec"
"strings"
)
type RcloneBackend struct {
}
type rcloneItem struct {
Name string
Size int64
ModTime string
IsDir bool
}
func NewRcloneBackend() *RcloneBackend {
return &RcloneBackend{}
}
func (b *RcloneBackend) List(reqPath string, maxDepth int) (*Item, error) {
if reqPath == "/" {
return b.listRemotes()
}
rcloneItems, err := b.rcloneLs(reqPath)
if err != nil {
return nil, err
}
parentItem := &Item{
Children: make(map[string]*Item),
}
for _, item := range rcloneItems {
child := &Item{
Size: item.Size,
ModTime: item.ModTime,
}
if item.IsDir {
parentItem.Children[item.Name+"/"] = child
} else {
parentItem.Children[item.Name] = child
}
}
return parentItem, nil
}
func (b *RcloneBackend) Read(reqPath string, offset, length int64) (*Item, io.ReadCloser, error) {
rcloneItems, err := b.rcloneLs(reqPath)
if err != nil {
return nil, nil, err
}
item := &Item{
Size: rcloneItems[0].Size,
ModTime: rcloneItems[0].ModTime,
}
args := []string{"cat"}
if offset != 0 {
args = append(args, "--offset", fmt.Sprintf("%d", offset))
}
if length != 0 {
args = append(args, "--count", fmt.Sprintf("%d", length))
}
parts := strings.Split(reqPath, "/")
rclonePath := parts[1] + ":" + strings.Join(parts[2:], "/")
args = append(args, rclonePath)
cmd := exec.Command("rclone", args...)
data, err := cmd.StdoutPipe()
if err != nil {
return nil, nil, err
}
err = cmd.Start()
if err != nil {
return nil, nil, err
}
return item, data, nil
}
func (b *RcloneBackend) listRemotes() (*Item, error) {
cmd := exec.Command("rclone", "listremotes")
stdout, err := cmd.Output()
if err != nil {
return nil, err
}
lines := strings.Split(string(stdout), "\n")
rootItem := &Item{
Children: make(map[string]*Item),
}
for _, line := range lines {
if len(line) == 0 {
continue
}
child := &Item{}
remoteName := line[:len(line)-1] + "/"
rootItem.Children[remoteName] = child
}
return rootItem, nil
}
func (b *RcloneBackend) rcloneLs(reqPath string) ([]rcloneItem, error) {
parts := strings.Split(reqPath, "/")
rclonePath := parts[1] + ":" + strings.Join(parts[2:], "/")
cmd := exec.Command("rclone", "lsjson", rclonePath)
stdout, err := cmd.Output()
if err != nil {
return nil, err
}
var rcloneItems []rcloneItem
err = json.Unmarshal(stdout, &rcloneItems)
if err != nil {
return nil, err
}
return rcloneItems, nil
}