forked from jessfraz/morningpaper2remarkable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremarkable.go
94 lines (74 loc) · 2.25 KB
/
remarkable.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 main
import (
"errors"
"fmt"
"path/filepath"
"github.com/juruen/rmapi/util"
"github.com/sirupsen/logrus"
)
func syncFileWithRemarkableCloud(file, title string) error {
if len(file) < 1 || len(title) < 1 {
return errors.New("file and title cannot be empty")
}
// Get the node for the directory.
dir, err := rmCtx.Filetree.NodeByPath(filepath.Dir(file), rmCtx.Filetree.Root())
if err != nil || dir.IsFile() {
return fmt.Errorf("%s is a file not a directory", filepath.Dir(file))
}
logrus.WithFields(logrus.Fields{
"file": file,
"title": title,
}).Debug("uploading file to remarkable cloud")
if _, err := rmCtx.Filetree.NodeByPath(title, dir); err == nil {
// File already exists.
return nil
}
dstDir := dir.Id()
document, err := rmCtx.UploadDocument(dstDir, file)
if err != nil {
return fmt.Errorf("uploading file %s failed: %v", file, err)
}
logrus.WithFields(logrus.Fields{
"file": file,
"title": title,
}).Info("successfully uploaded file to remarkable cloud")
rmCtx.Filetree.AddDocument(*document)
// Move the file to the title.
docName := util.DocPathToName(filepath.Base(file))
node, err := rmCtx.Filetree.NodeByPath(docName, dir)
if err != nil {
return fmt.Errorf("getting file %s failed: %v", file, err)
}
if _, err := rmCtx.MoveEntry(node, dir, title); err != nil {
return fmt.Errorf("moving the file to a new name failed: %v", err)
}
return nil
}
func remarkableMkdir(dir string) error {
if len(dir) == 0 {
return errors.New("directory cannot be empty")
}
if _, err := rmCtx.Filetree.NodeByPath(dir, rmCtx.Filetree.Root()); err == nil {
// Directory already exists
return nil
}
parentDir := filepath.Dir(dir)
newDir := filepath.Base(dir)
if newDir == "/" || newDir == "." {
return fmt.Errorf("%s is an invalid directory name", newDir)
}
parentNode, err := rmCtx.Filetree.NodeByPath(parentDir, rmCtx.Filetree.Root())
if err != nil || parentNode.IsFile() {
return fmt.Errorf("%s is a file not a directory", parentNode.Name())
}
parentID := parentNode.Id()
if parentNode.IsRoot() {
parentID = ""
}
document, err := rmCtx.CreateDir(parentID, newDir)
if err != nil {
return fmt.Errorf("creating directory %s failed: %v", newDir, err)
}
rmCtx.Filetree.AddDocument(document)
return nil
}