-
Notifications
You must be signed in to change notification settings - Fork 9
/
job_move.go
94 lines (83 loc) · 2.04 KB
/
job_move.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
package putiosync
import (
"context"
"errors"
"fmt"
"net/url"
"os"
"path"
"path/filepath"
"strconv"
"strings"
)
type moveLocalFileJob struct {
localFile iLocalFile
toRelpath string
state stateType
}
func (j *moveLocalFileJob) String() string {
return fmt.Sprintf("Moving local file from %q to %q", j.state.relpath, j.toRelpath)
}
func (j *moveLocalFileJob) Run(ctx context.Context) error {
oldPath := j.localFile.FullPath()
newPath := filepath.Join(localPath, filepath.FromSlash(j.toRelpath))
exists, err := j.exists(newPath)
if err != nil {
return err
}
if exists {
return errors.New("file already exists at move target")
}
err = os.MkdirAll(filepath.Dir(newPath), 0777)
if err != nil {
return err
}
err = os.Rename(oldPath, newPath)
if err != nil {
return err
}
return j.state.Move(j.toRelpath)
}
func (j *moveLocalFileJob) exists(path string) (bool, error) {
_, err := os.Stat(path)
if os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
type moveRemoteFileJob struct {
remoteFile iRemoteFile
toRelpath string
state stateType
}
func (j *moveRemoteFileJob) String() string {
return fmt.Sprintf("Moving remote file from %q to %q", j.state.relpath, j.toRelpath)
}
func (j *moveRemoteFileJob) Run(ctx context.Context) error {
dir, name := path.Split(j.toRelpath)
parentID, err := dirCache.Mkdirp(ctx, dir)
if err != nil {
return err
}
err = moveRemoteFile(ctx, parentID, j.remoteFile.PutioFile().ID, name)
if err != nil {
return err
}
return j.state.Move(j.toRelpath)
}
func moveRemoteFile(ctx context.Context, parentID, fileID int64, name string) error {
params := url.Values{}
params.Set("file_id", strconv.FormatInt(fileID, 10))
params.Set("parent_id", strconv.FormatInt(parentID, 10))
params.Set("name", name)
req, err := client.NewRequest(ctx, "POST", "/v2/files/move", strings.NewReader(params.Encode()))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
_, err = client.Do(req, nil)
return err
}