itd/api/fs.go

102 lines
1.6 KiB
Go
Raw Permalink Normal View History

2021-11-23 19:12:16 +00:00
package api
import (
2022-04-23 00:12:30 +00:00
"context"
"time"
2021-11-23 19:12:16 +00:00
)
2022-04-23 00:12:30 +00:00
func (c *Client) Remove(paths ...string) error {
return c.fsClient.Call(
context.Background(),
"Remove",
paths,
nil,
)
2021-11-23 19:12:16 +00:00
}
2022-04-23 00:12:30 +00:00
func (c *Client) Rename(old, new string) error {
return c.fsClient.Call(
context.Background(),
"Remove",
[2]string{old, new},
nil,
)
2021-11-23 19:12:16 +00:00
}
func (c *Client) Mkdir(paths ...string) error {
2022-04-23 00:12:30 +00:00
return c.fsClient.Call(
context.Background(),
"Mkdir",
paths,
nil,
)
2021-11-23 19:12:16 +00:00
}
2022-04-23 00:12:30 +00:00
func (c *Client) ReadDir(dir string) (out []FileInfo, err error) {
err = c.fsClient.Call(
context.Background(),
"ReadDir",
dir,
&out,
)
return
2021-11-23 19:12:16 +00:00
}
2022-04-23 00:12:30 +00:00
func (c *Client) Upload(dst, src string) (chan FSTransferProgress, error) {
var id string
err := c.fsClient.Call(
context.Background(),
"Upload",
[2]string{dst, src},
&id,
)
2021-11-23 19:12:16 +00:00
if err != nil {
2021-12-13 01:08:48 +00:00
return nil, err
2021-11-23 19:12:16 +00:00
}
2021-12-13 01:08:48 +00:00
2022-04-23 00:12:30 +00:00
progressCh := make(chan FSTransferProgress, 5)
go func() {
srvValCh, ok := c.srvVals[id]
for !ok {
time.Sleep(100 * time.Millisecond)
srvValCh, ok = c.srvVals[id]
}
for val := range srvValCh {
progressCh <- val.(FSTransferProgress)
}
close(progressCh)
}()
2021-11-23 19:12:16 +00:00
2022-04-23 00:12:30 +00:00
return progressCh, nil
}
2021-12-13 01:08:48 +00:00
2022-04-23 00:12:30 +00:00
func (c *Client) Download(dst, src string) (chan FSTransferProgress, error) {
var id string
err := c.fsClient.Call(
context.Background(),
"Download",
[2]string{dst, src},
&id,
)
2021-11-23 19:12:16 +00:00
if err != nil {
2021-12-13 01:08:48 +00:00
return nil, err
2021-11-23 19:12:16 +00:00
}
2021-12-13 01:08:48 +00:00
2022-04-23 00:12:30 +00:00
progressCh := make(chan FSTransferProgress, 5)
go func() {
srvValCh, ok := c.srvVals[id]
for !ok {
time.Sleep(100 * time.Millisecond)
srvValCh, ok = c.srvVals[id]
}
for val := range srvValCh {
progressCh <- val.(FSTransferProgress)
}
close(progressCh)
}()
return progressCh, nil
2021-11-23 19:12:16 +00:00
}