-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
file_download.go
181 lines (150 loc) · 4.61 KB
/
file_download.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
package proton_api_bridge
import (
"bytes"
"context"
"io"
"log"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/henrybear327/go-proton-api"
)
type FileDownloadReader struct {
protonDrive *ProtonDrive
ctx context.Context
link *proton.Link
data *bytes.Buffer
nodeKR *crypto.KeyRing
sessionKey *crypto.SessionKey
revision *proton.Revision
nextRevision int
isEOF bool
// TODO: integrity check if the entire file is read
}
func (r *FileDownloadReader) Read(p []byte) (int, error) {
if r.data.Len() == 0 {
// TODO: do we have memory sharing bug?
// to avoid sharing the underlying buffer array across re-population
r.data = bytes.NewBuffer(nil)
// we download and decrypt more content
err := r.populateBufferOnRead()
if err != nil {
return 0, err
}
if r.isEOF {
// if the file has been downloaded entirely, we return EOF
return 0, io.EOF
}
}
return r.data.Read(p)
}
func (r *FileDownloadReader) Close() error {
r.protonDrive = nil
return nil
}
func (reader *FileDownloadReader) populateBufferOnRead() error {
if len(reader.revision.Blocks) == 0 || len(reader.revision.Blocks) == reader.nextRevision {
reader.isEOF = true
return nil
}
offset := reader.nextRevision
for i := offset; i-offset < DOWNLOAD_BATCH_BLOCK_SIZE && i < len(reader.revision.Blocks); i++ {
// TODO: parallel download
blockReader, err := reader.protonDrive.c.GetBlock(reader.ctx, reader.revision.Blocks[i].BareURL, reader.revision.Blocks[i].Token)
if err != nil {
return err
}
defer blockReader.Close()
signatureVerificationKR, err := reader.protonDrive.getSignatureVerificationKeyring([]string{reader.link.SignatureEmail}, reader.nodeKR)
if err != nil {
return err
}
err = decryptBlockIntoBuffer(reader.sessionKey, signatureVerificationKR, reader.nodeKR, reader.revision.Blocks[i].Hash, reader.revision.Blocks[i].EncSignature, reader.data, blockReader)
if err != nil {
return err
}
reader.nextRevision = i + 1
}
return nil
}
func (protonDrive *ProtonDrive) DownloadFileByID(ctx context.Context, linkID string, offset int64) (io.ReadCloser, int64, *FileSystemAttrs, error) {
/* It's like event system, we need to get the latest information before creating the move request! */
protonDrive.removeLinkIDFromCache(linkID, false)
link, err := protonDrive.getLink(ctx, linkID)
if err != nil {
return nil, 0, nil, err
}
return protonDrive.DownloadFile(ctx, link, offset)
}
func (protonDrive *ProtonDrive) DownloadFile(ctx context.Context, link *proton.Link, offset int64) (io.ReadCloser, int64, *FileSystemAttrs, error) {
if link.Type != proton.LinkTypeFile {
return nil, 0, nil, ErrLinkTypeMustToBeFileType
}
parentNodeKR, err := protonDrive.getLinkKRByID(ctx, link.ParentLinkID)
if err != nil {
return nil, 0, nil, err
}
signatureVerificationKR, err := protonDrive.getSignatureVerificationKeyring([]string{link.SignatureEmail})
if err != nil {
return nil, 0, nil, err
}
nodeKR, err := link.GetKeyRing(parentNodeKR, signatureVerificationKR)
if err != nil {
return nil, 0, nil, err
}
sessionKey, err := link.GetSessionKey(nodeKR)
if err != nil {
return nil, 0, nil, err
}
revision, fileSystemAttrs, err := protonDrive.GetActiveRevisionWithAttrs(ctx, link)
if err != nil {
return nil, 0, nil, err
}
reader := &FileDownloadReader{
protonDrive: protonDrive,
ctx: ctx,
link: link,
data: bytes.NewBuffer(nil),
nodeKR: nodeKR,
sessionKey: sessionKey,
revision: revision,
nextRevision: 0,
isEOF: false,
}
useFallbackDownload := false
if fileSystemAttrs != nil {
// based on offset, infer the nextRevision (0-based)
if fileSystemAttrs.BlockSizes == nil {
useFallbackDownload = true
} else {
// infer nextRevision
totalBytes := int64(0)
for i := 0; i < len(fileSystemAttrs.BlockSizes); i++ {
prevTotalBytes := totalBytes
totalBytes += fileSystemAttrs.BlockSizes[i]
if offset <= totalBytes {
offset = offset - prevTotalBytes
reader.nextRevision = i
break
}
}
// download will start from the specified block
n, err := io.CopyN(io.Discard, reader, offset)
if err != nil {
return nil, 0, nil, err
}
if int64(n) != offset {
return nil, 0, nil, ErrSeekOffsetAfterSkippingBlocks
}
}
}
if useFallbackDownload {
log.Println("Performing inefficient seek as metadata of encrypted file is missing")
n, err := io.CopyN(io.Discard, reader, offset)
if err != nil {
return nil, 0, nil, err
}
if int64(n) != offset {
return nil, 0, nil, ErrSeekOffsetAfterSkippingBlocks
}
}
return reader, link.Size, fileSystemAttrs, nil
}