forked from juruen/rmapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathporcelain.go
309 lines (262 loc) · 8.3 KB
/
porcelain.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package cloud
import (
"bytes"
"fmt"
"io"
"net/http"
"sort"
"time"
"github.com/google/uuid"
"github.com/pkg/errors"
)
const (
// DirectoryType is used as directory type.
DirectoryType = "CollectionType"
// DocumentType is used a regular document type.
DocumentType = "DocumentType"
)
// Document represents a human readable format of a Remarkable document.
// It is transformed into a more technical object and used as a
// payload or response during http calls with the Remarkable API.
type Document struct {
ID string
Version int
Type string
Name string
CurrentPage int
Bookmarked bool
Parent string
}
// String dictates how to print a Document object.
func (d Document) String() string {
var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintf("%s (%s)\n", d.Name, d.ID))
buffer.WriteString(fmt.Sprintf("\tVersion: %d\n", d.Version))
buffer.WriteString(fmt.Sprintf("\tType: %s\n", d.Type))
buffer.WriteString(fmt.Sprintf("\tCurrent Page: %d\n", d.CurrentPage))
buffer.WriteString(fmt.Sprintf("\tBookmarked: %t\n", d.Bookmarked))
buffer.WriteString(fmt.Sprintf("\tParent: %s", d.Parent))
return buffer.String()
}
// toRawDocument returns a technical rawDocument created from a public Document
func (d Document) toRawDocument() rawDocument {
return rawDocument{
ID: d.ID,
Version: d.Version,
Type: d.Type,
VissibleName: d.Name,
CurrentPage: d.CurrentPage,
Bookmarked: d.Bookmarked,
Parent: d.Parent,
}
}
// Get is a first class method used to fetch information of a document.
//
// It takes a uuid as parameter and a Document is returned to the end user.
func (c *Client) Get(uuid string) (Document, error) {
rdoc, err := c.getDoc(uuid)
if err != nil {
return Document{}, errors.Wrap(err, "can't get document")
}
return rdoc.toDocument(), nil
}
// List is a first class method used to fetch the list of all documents on a device.
//
// It returns a list of Documents.
func (c *Client) List() ([]Document, error) {
// use empty uuid to have them all
rdocs, err := c.getDocs("")
if err != nil {
return nil, errors.Wrap(err, "can't get documents")
}
var docs []Document
for _, rdoc := range rdocs {
docs = append(docs, rdoc.toDocument())
}
// sort by name
sort.Slice(docs, func(i, j int) bool {
return docs[i].Name < docs[j].Name
})
return docs, nil
}
// Download is a first class method used to download the actual content of a document.
//
// It takes a uuid as parameter and an io.Writer that will be used to write the content.
// By using an io.Writer, the content can be committed to a file
// but also in response to an http call for example.
//
// The content received will be a zip file containing all the document files.
// To make use of it, you can have a look to the archive package.
func (c *Client) Download(uuid string, w io.Writer) error {
rdoc, err := c.getDoc(uuid)
if err != nil {
return errors.Wrap(err, "can't get document")
}
// direct call to the http.Client.Get because different domain
resp, err := c.httpClient.Get(rdoc.BlobURLGet)
if err != nil {
return errors.Wrap(err, "download failed")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return errors.Errorf("wrong http return code: %d", resp.StatusCode)
}
_, err = io.Copy(w, resp.Body)
if err != nil {
return errors.Wrap(err, "can't write file")
}
return nil
}
// Upload is a first class method used to upload content to a document.
//
// The Document given as parameter will identify which real document to
// target. If the Document uuid already exists, the content will be
// updated. If not, a new document will be created.
// For creating a new document however, you may want to use the Upload method instead.
//
// An io.Reader is expected as parameter to provide the content of the
// document. This way, we can upload a content not only from a file
// but as well from other sources.
//
// The content should be shaped as a zip file as expected by the Remarkable.
// You can have a look to the archive package to help easily creating
// a correctly formatted file.
func (c *Client) UploadDocument(doc Document, r io.Reader) error {
if doc.ID == "" {
return errors.New("undefined document id")
}
url, err := c.uploadRequest(doc.toRawDocument())
if err != nil {
return errors.Wrap(err, "can't create upload request")
}
// direct upload to url as endpoint is different
req, err := http.NewRequest("PUT", url, r)
if err != nil {
return errors.Wrap(err, "can't initiate upload")
}
resp, err := c.httpClient.Do(req)
if err != nil {
return errors.Wrap(err, "can't upload document")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return errors.Errorf("wrong http return code: %d", resp.StatusCode)
}
if err := c.Metadata(doc); err != nil {
return errors.Wrap(err, "can't update metadata")
}
return nil
}
// Upload is a first class method used to upload a new document.
//
// A newly generated uuid should be provided and a name should be given.
//
// An io.Reader is expected as parameter to provide the content of the
// document. This way, we can upload a content not only from a file
// but as well from other sources.
//
// The content should be shaped as a zip file as expected by the Remarkable.
// You can have a look to the archive package to help easily creating
// a correctly formatted file.
func (c *Client) Upload(uuid string, name string, r io.Reader) error {
doc := Document{
ID: uuid,
Type: DocumentType,
Name: name,
Version: 1,
}
return c.UploadDocument(doc, r)
}
// CreateFolder is a first class method used to create a new folder.
//
// It takes a folder name as parameter and an optional parent UUID to
// create the folder in a subdirectory. If an empty string is provided,
// the folder will be created at the root.
//
// The UUID of the created folder is returned.
func (c *Client) CreateFolder(name string, parent string) (string, error) {
id := uuid.New().String()
doc := Document{
ID: id,
Parent: parent,
Type: DirectoryType,
Version: 1,
Name: name,
}
if err := c.Metadata(doc); err != nil {
return "", errors.Wrap(err, "can't create folder")
}
return id, nil
}
// Metadata is a first class method used to update metadata of
// an existing document.
//
// It takes a Document as input containing the metadata changes
// that will be sent to the API.
//
// This method can be used to make a document visible, to move,
// rename, bookmark a document.
//
// Calling this method will reset the ModifiedClient time to now.
// If the document Version is not explicitly defined, it will be
// defined by fetching the current document version and adding one.
func (c *Client) Metadata(doc Document) error {
rdoc := doc.toRawDocument()
if rdoc.ID == "" {
return errors.New("undefined document id")
}
// set modified to now
rdoc.ModifiedClient = time.Now().UTC().Format(time.RFC3339Nano)
// set Version to current version +1 if not defined
if rdoc.Version == 0 {
cur, err := c.getCurrentVersion(doc.ID)
if err != nil {
return errors.Wrap(err, "can't get current version of document")
}
rdoc.Version = cur + 1
}
payload := []rawDocument{rdoc}
req, err := c.newRequest("PUT", "document-storage/json/2/upload/update-status", payload)
if err != nil {
return err
}
var rdocs []rawDocument
resp, err := c.do(req, &rdocs)
if err != nil {
return errors.Wrap(err, "request failed")
}
if resp.StatusCode != http.StatusOK {
return errors.Errorf("wrong http return code: %d", resp.StatusCode)
}
if len(rdocs) == 0 {
return errors.Wrap(err, "empty document list received")
}
if !rdocs[0].Success {
return errors.Errorf("success false received: %s", rdocs[0].Message)
}
return nil
}
// Delete is a first class method used to delete a document or folder.
func (c *Client) Delete(uuid string) error {
cur, err := c.getCurrentVersion(uuid)
if err != nil {
return errors.Wrap(err, "can't get current version of document")
}
doc := Document{
ID: uuid,
Version: cur,
}
payload := []rawDocument{doc.toRawDocument()}
req, err := c.newRequest("PUT", "/document-storage/json/2/delete", payload)
if err != nil {
return err
}
resp, err := c.do(req, nil)
if err != nil {
return errors.Wrap(err, "request failed")
}
if resp.StatusCode != http.StatusOK {
return errors.Errorf("wrong http return code: %d", resp.StatusCode)
}
return nil
}