-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathjob_state.go
79 lines (68 loc) · 1.7 KB
/
job_state.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
package putiosync
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/cenkalti/log"
"github.com/putdotio/putio-sync/v2/internal/inode"
)
type deleteStateJob struct {
state stateType
}
func (j *deleteStateJob) String() string {
return fmt.Sprintf("Deleting state %q", j.state.relpath)
}
func (j *deleteStateJob) Run(ctx context.Context) error {
if j.state.DownloadTempName != "" {
err := os.Remove(filepath.Join(tempDirPath, j.state.DownloadTempName))
if err != nil {
log.Errorln("cannot remove temp download file:", err.Error())
}
}
if j.state.UploadURL != "" {
err := client.Upload.TerminateUpload(ctx, j.state.UploadURL)
if err != nil {
log.Errorln("cannot remove upload:", err.Error())
}
}
return j.state.Delete()
}
type writeFileStateJob struct {
localFile iLocalFile
remoteFile iRemoteFile
}
func (j *writeFileStateJob) String() string {
return fmt.Sprintf("Saving file state %q", j.localFile.RelPath())
}
func (j *writeFileStateJob) Run(ctx context.Context) error {
in, err := inode.Get(j.localFile.FullPath(), j.localFile.Info())
if err != nil {
return err
}
s := stateType{
Status: statusSynced,
LocalInode: in,
RemoteID: j.remoteFile.PutioFile().ID,
Size: j.remoteFile.PutioFile().Size,
CRC32: j.remoteFile.PutioFile().CRC32,
relpath: j.remoteFile.RelPath(),
}
return s.Write()
}
type writeDirStateJob struct {
remoteID int64
relpath string
}
func (j *writeDirStateJob) String() string {
return fmt.Sprintf("Saving folder state %q", j.relpath)
}
func (j *writeDirStateJob) Run(ctx context.Context) error {
s := stateType{
Status: statusSynced,
IsDir: true,
RemoteID: j.remoteID,
relpath: j.relpath,
}
return s.Write()
}