-
Notifications
You must be signed in to change notification settings - Fork 2
/
list.go
124 lines (97 loc) · 2.52 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package rclone
import (
"context"
"path/filepath"
"strings"
"time"
"code.cloudfoundry.org/bytefmt"
"github.com/darkhz/rclone-tui/rclone"
"github.com/mitchellh/mapstructure"
)
// List stores a list of directory entries.
type List struct {
Items []ListItem `mapstructure:"list"`
Path string
}
// ListItem stores information about a directory entry.
type ListItem struct {
ID string `mapstructure:"ID"`
IsDir bool `mapstructure:"IsDir"`
MimeType string `mapstructure:"MimeType"`
ModTime string `mapstructure:"ModTime"`
Name string `mapstructure:"Name"`
Path string `mapstructure:"Path"`
Size int64 `mapstructure:"Size"`
FS string
ISize string
ModifiedTime string
ModifiedTimeUnix int64
About bool
RefreshAddItem bool
}
// ListFS returns a list of directory entries from the provided remote and path.
func ListFS(id, fstype, path string) (List, error) {
var list List
var fs, desc string
if strings.Contains(fstype, ":") {
fs = fstype
if path != "" {
path = filepath.Clean(path)
}
} else {
fs = fstype
}
desc = fs + path
command := map[string]interface{}{
"fs": fs,
"remote": path,
}
job, err := rclone.SendCommandAsync("UI:Explorer:"+id, "Listing "+desc, command, "/operations/list")
if err != nil {
return List{}, err
}
jobInfo, err := rclone.GetJobReply(job)
if err != nil {
return List{}, err
}
err = mapstructure.Decode(jobInfo.Output, &list)
if err != nil {
return List{}, err
}
for j, item := range list.Items {
list.Items[j] = appendItemDetails(item, fs)
}
return list, err
}
func appendItemDetails(item ListItem, fs string) ListItem {
modtime, _ := time.Parse(time.RFC3339, item.ModTime)
item.ModTime = ""
item.ModifiedTimeUnix = modtime.Unix()
item.ModifiedTime = modtime.Format("Mon 01/02 15:04")
size := "Unknown"
if item.Size >= 0 {
size = bytefmt.ByteSize(uint64(item.Size))
}
item.ISize = size
item.FS = fs
return item
}
// ListRemotes lists the configured remotes.
func ListRemotes(ctx context.Context) ([]string, error) {
return GetDataSlice(ctx, "/config/listremotes", "remotes")
}
// GetListPath returns the joined path with the provided directory, or
// if cdback is true, returns the path's directory.
func GetListPath(path, dir string, cdback bool) (string, string) {
if filepath.Dir(path) == "." && cdback {
return "", ""
}
path = filepath.Clean(path)
if !cdback {
path = filepath.Join(path, dir)
} else {
path = filepath.Dir(path)
dir = filepath.Base(path)
}
return path, dir
}