-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrm.go
58 lines (45 loc) · 1.21 KB
/
rm.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
package main
import (
"fmt"
"io"
"path/filepath"
"github.com/juruen/rmapi/archive"
)
// uploadToRm sends the file to the Remarkable device.
func (s server) uploadToRm(id string, file []byte, name string) error {
zip := archive.NewZip()
zip.UUID = id
fname, ext := splitExt(name)
switch ext {
case ".epub":
zip.Epub = file
zip.Content.FileType = "epub"
case ".pdf":
zip.Pdf = file
zip.Content.FileType = "pdf"
default:
return fmt.Errorf("file does not have a supported extension")
}
// We use an io.Pipe to connect the io.Writer of the archive
// package to the io.Reader needed by the api package.
pr, pw := io.Pipe()
// Write the zip file to the pipe.
// Writing without a reader will deadlock so write in a goroutine.
go func() {
pw.CloseWithError(zip.Write(pw))
}()
// Upload to the Remarkable from the pipe.
if err := s.cli.Upload(id, fname, pr); err != nil {
return err
}
return nil
}
// deleteFromRm removes the file from the Remarkable device.
func (s server) deleteFromRm(id string) error {
return s.cli.Delete(id)
}
// splitExt splits the extension from a filename
func splitExt(name string) (string, string) {
ext := filepath.Ext(name)
return name[0 : len(name)-len(ext)], ext
}