-
Notifications
You must be signed in to change notification settings - Fork 0
/
lockbook.go
364 lines (311 loc) · 7.35 KB
/
lockbook.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package lockbook
import (
"fmt"
"sort"
"strconv"
"time"
"github.com/gofrs/uuid"
)
type Core interface {
WriteablePath() string
GetAccount() (Account, error)
CreateAccount(uname, apiURL string, welcome bool) (Account, error)
ImportAccount(acctStr string) (Account, error)
ExportAccount() (string, error)
FileByID(id FileID) (File, error)
FileByPath(lbPath string) (File, error)
GetRoot() (File, error)
GetChildren(id FileID) ([]File, error)
GetAndGetChildrenRecursively(id FileID) ([]File, error)
ListMetadatas() ([]File, error)
PathByID(id FileID) (string, error)
ReadDocument(id FileID) ([]byte, error)
WriteDocument(id FileID, data []byte) error
CreateFile(name string, parentID FileID, typ FileType) (File, error)
CreateFileAtPath(lbPath string) (File, error)
DeleteFile(id FileID) error
RenameFile(id FileID, newName string) error
MoveFile(srcID, destID FileID) error
ImportFile(src string, dest FileID, fn func(ImportFileInfo)) error
ExportFile(id FileID, dest string, fn func(ExportFileInfo)) error
ExportDrawing(id FileID, imgFmt ImageFormat) ([]byte, error)
ExportDrawingToDisk(id FileID, imgFmt ImageFormat, dest string) error
GetLastSynced() (time.Time, error)
GetLastSyncedHumanString() (string, error)
GetUsage() (UsageMetrics, error)
GetUncompressedUsage() (UsageItemMetric, error)
CalculateWork() (WorkCalculated, error)
SyncAll(fn func(SyncProgress)) error
ShareFile(id FileID, uname string, mode ShareMode) error
GetPendingShares() ([]File, error)
DeletePendingShare(id FileID) error
GetSubscriptionInfo() (SubscriptionInfo, error)
UpgradeViaStripe(card *CreditCard) error
CancelSubscription() error
Validate() ([]string, error)
}
func NewCore(fpath string) (Core, error) {
return initLbCoreFFI(fpath)
}
type ErrorCode uint32
const (
CodeSuccess ErrorCode = iota
CodeUnexpected
CodeAccountExists
CodeAccountNonexistent
CodeAccountStringCorrupted
CodeAlreadyCanceled
CodeAlreadyPremium
CodeAppStoreAccountAlreadyLinked
CodeCannotCancelSubscriptionForAppStore
CodeCardDecline
CodeCardExpired
CodeCardInsufficientFunds
CodeCardInvalidCvc
CodeCardInvalidExpMonth
CodeCardInvalidExpYear
CodeCardInvalidNumber
CodeCardNotSupported
CodeClientUpdateRequired
CodeCurrentUsageIsMoreThanNewTier
CodeDiskPathInvalid
CodeDiskPathTaken
CodeDrawingInvalid
CodeExistingRequestPending
CodeFileNameContainsSlash
CodeFileNameEmpty
CodeFileNonexistent
CodeFileNotDocument
CodeFileNotFolder
CodeFileParentNonexistent
CodeFolderMovedIntoSelf
CodeInsufficientPermission
CodeInvalidPurchaseToken
CodeInvalidAuthDetails
CodeLinkInSharedFolder
CodeLinkTargetIsOwned
CodeLinkTargetNonexistent
CodeMultipleLinksToSameFile
CodeNotPremium
CodeOldCardDoesNotExist
CodePathContainsEmptyFileName
CodePathTaken
CodeRootModificationInvalid
CodeRootNonexistent
CodeServerDisabled
CodeServerUnreachable
CodeShareAlreadyExists
CodeShareNonexistent
CodeTryAgain
CodeUsageIsOverFreeTierDataCap
CodeUsernameInvalid
CodeUsernameNotFound
CodeUsernamePublicKeyMismatch
CodeUsernameTaken
)
type Error struct {
Code ErrorCode
Msg string
Trace string
}
func (e *Error) Error() string {
return e.Msg
}
type Account struct {
Username string
APIURL string
}
type FileID = uuid.UUID
type File struct {
ID FileID
Parent FileID
Name string
Type FileType
Lastmod time.Time
LastmodBy string
Shares []Share
}
func (f *File) IsDir() bool {
_, ok := f.Type.(FileTypeFolder)
return ok
}
func (f *File) IsRoot() bool {
return f.ID == f.Parent
}
type (
FileType interface{ implsFileType() }
FileTypeDocument struct{}
FileTypeFolder struct{}
FileTypeLink struct{ Target FileID }
)
func (FileTypeDocument) implsFileType() {}
func (FileTypeFolder) implsFileType() {}
func (FileTypeLink) implsFileType() {}
func FileTypeString(t FileType) string {
switch t := t.(type) {
case FileTypeDocument:
return "Document"
case FileTypeFolder:
return "Folder"
case FileTypeLink:
return "Link('" + t.Target.String() + "')"
default:
return fmt.Sprintf("FileType(%v)", t)
}
}
type Share struct {
Mode ShareMode
SharedBy string
SharedWith string
}
type ShareMode int
const (
ShareModeRead ShareMode = iota
ShareModeWrite
)
func (s ShareMode) String() string {
switch s {
case ShareModeRead:
return "Read"
case ShareModeWrite:
return "Write"
default:
return "ShareMode(" + strconv.FormatInt(int64(s), 10) + ")"
}
}
func SortFiles(files []File) {
sort.SliceStable(files, func(i, j int) bool {
a, b := files[i], files[j]
if a.IsDir() == b.IsDir() {
return a.Name < b.Name
}
return a.IsDir()
})
}
type WorkCalculated struct {
LastServerUpdateAt uint64
WorkUnits []WorkUnit
}
type WorkUnit struct {
Type WorkUnitType
ID FileID
}
type WorkUnitType int
const (
WorkUnitTypeLocal WorkUnitType = iota
WorkUnitTypeServer
)
// SyncProgress is the data sent (via closure) at certain stages of sync.
type SyncProgress struct {
Total uint64
Progress uint64
Msg string
}
type UsageMetrics struct {
Usages []FileUsage
ServerUsage UsageItemMetric
DataCap UsageItemMetric
}
type UsageItemMetric struct {
Exact uint64
Readable string
}
type FileUsage struct {
FileID FileID
SizeBytes uint64
}
// ImportFileInfo is the data sent (via closure) at certain stages of file import. The
// stage and type of information is determined by the zero value of each field. A non-zero
// `Total` means a "total calculated" update. A non-empty `DiskPath` means a "file
// started" update. A non-nil `FileDone` means a "file finished" update.
type ImportFileInfo struct {
Total int
DiskPath string
FileDone *File
}
type ExportFileInfo struct {
DiskPath string
LbPath string
}
type ImageFormat int
const (
ImgFmtPNG ImageFormat = iota
ImgFmtJPEG
ImgFmtPNM
ImgFmtTGA
ImgFmtFarbfeld
ImgFmtBMP
)
type CreditCard struct {
Number string
ExpiryYear int
ExpiryMonth int
CVC string
}
type SubscriptionInfo struct {
StripeLast4 string
GooglePlay GooglePlayAccountState
AppStore AppStoreAccountState
PeriodEnd time.Time
}
type StripeInfo struct {
Last4 string
}
type GooglePlayAccountState int
const (
GooglePlayNone GooglePlayAccountState = iota
GooglePlayOk
GooglePlayCanceled
GooglePlayGracePeriod
GooglePlayOnHold
)
func (s GooglePlayAccountState) String() string {
switch s {
case GooglePlayNone:
return "None"
case GooglePlayOk:
return "Ok"
case GooglePlayCanceled:
return "Canceled"
case GooglePlayGracePeriod:
return "Grace Period"
case GooglePlayOnHold:
return "On Hold"
default:
return "GooglePlayAccountState(" + strconv.FormatInt(int64(s), 10) + ")"
}
}
type AppStoreAccountState int
const (
AppStoreNone AppStoreAccountState = iota
AppStoreOk
AppStoreGracePeriod
AppStoreFailedToRenew
AppStoreExpired
)
func (s AppStoreAccountState) String() string {
switch s {
case AppStoreNone:
return "None"
case AppStoreOk:
return "Ok"
case AppStoreGracePeriod:
return "Grace Period"
case AppStoreFailedToRenew:
return "Failed To Renew"
case AppStoreExpired:
return "Expired"
default:
return "AppStoreAccountState(" + strconv.FormatInt(int64(s), 10) + ")"
}
}
func MaybeFileByPath(core Core, p string) (File, bool, error) {
f, err := core.FileByPath(p)
if err != nil {
if err, ok := err.(*Error); ok && err.Code == CodeFileNonexistent {
return File{}, false, nil
}
return File{}, false, err
}
return f, true, nil
}