-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileUpload.go
393 lines (336 loc) · 10.2 KB
/
FileUpload.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package libdatamanager
import (
"crypto/aes"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"hash/crc32"
"io"
"net/http"
"net/url"
"os"
"strconv"
"github.com/JojiiOfficial/gaw"
gzip "github.com/klauspost/pgzip"
)
// NoProxyWriter use to fill proxyWriter arg in UpdloadFile
var NoProxyWriter WriterProxy = func(w io.Writer) io.Writer {
return w
}
// NoProxyReader use to fill proxyWriter arg in UpdloadFile
var NoProxyReader ReaderProxy = func(r io.Reader) io.Reader {
return r
}
var (
// ErrUnsupportedScheme error if url has an unsupported scheme
ErrUnsupportedScheme = errors.New("Unsupported scheme")
)
// UploadRequest a uploadrequest
type UploadRequest struct {
LibDM
Name string
Publicname string
Public bool
Attribute FileAttributes
ReplaceFileID uint
ReplaceEqualName bool
All bool
Encryption int8
EncryptionKey []byte
Buffersize int
fileSizeCallback FileSizeCallback
ProxyWriter WriterProxy
ProxyReader ReaderProxy
Archive bool
Compressed bool
}
// NewUploadRequest create a new uploadrequest
func (libdm LibDM) NewUploadRequest(name string, attributes FileAttributes) *UploadRequest {
if attributes.Namespace == "" {
attributes.Namespace = "default"
}
return &UploadRequest{
LibDM: libdm,
Name: name,
Attribute: attributes,
}
}
// Compress the uploaded file
func (uploadRequest *UploadRequest) Compress() *UploadRequest {
uploadRequest.Compressed = true
return uploadRequest
}
// SetFileSizeCallback sets the callback if the filesize is known
func (uploadRequest *UploadRequest) SetFileSizeCallback(cb FileSizeCallback) *UploadRequest {
uploadRequest.fileSizeCallback = cb
return uploadRequest
}
// GetReaderProxy returns proxyReader for uploadRequest
func (uploadRequest *UploadRequest) GetReaderProxy() ReaderProxy {
if uploadRequest.ProxyReader == nil {
return NoProxyReader
}
return uploadRequest.ProxyReader
}
// GetWriterProxy returns proxywriter for uploadRequest
func (uploadRequest *UploadRequest) GetWriterProxy() WriterProxy {
if uploadRequest.ProxyWriter == nil {
return NoProxyWriter
}
return uploadRequest.ProxyWriter
}
// GetBuffersize returns the buffersize
func (uploadRequest *UploadRequest) GetBuffersize() int {
if uploadRequest.Buffersize <= 0 {
return DefaultBuffersize
}
return uploadRequest.Buffersize
}
// MakePublic upload and publish a file. If publciName is empty a
// random public name will be created serverside
func (uploadRequest *UploadRequest) MakePublic(publicName string) *UploadRequest {
uploadRequest.Public = true
uploadRequest.Publicname = publicName
return uploadRequest
}
// ReplaceFileWithSameName replace files with same name
func (uploadRequest *UploadRequest) ReplaceFileWithSameName() *UploadRequest {
uploadRequest.ReplaceEqualName = true
return uploadRequest
}
// HandleAll applies action to all files
func (uploadRequest *UploadRequest) HandleAll() *UploadRequest {
uploadRequest.All = true
return uploadRequest
}
// ReplaceFileByID replace a file instead creating a new one
func (uploadRequest *UploadRequest) ReplaceFileByID(fileID uint) *UploadRequest {
uploadRequest.ReplaceFileID = fileID
return uploadRequest
}
// Encrypted Upload a file encrypted
func (uploadRequest *UploadRequest) Encrypted(encryptionMethod int8, key []byte) *UploadRequest {
uploadRequest.Encryption = encryptionMethod
uploadRequest.EncryptionKey = key
return uploadRequest
}
// BuildRequestStruct create a uploadRequset struct using Type
func (uploadRequest *UploadRequest) BuildRequestStruct(Type UploadType) *UploadRequestStruct {
return &UploadRequestStruct{
UploadType: Type,
Name: uploadRequest.Name,
Attributes: uploadRequest.Attribute,
Encryption: uploadRequest.Encryption,
Public: uploadRequest.Public,
PublicName: uploadRequest.Publicname,
ReplaceFileByID: uploadRequest.ReplaceFileID,
Archived: uploadRequest.Archive,
Compressed: uploadRequest.Compressed,
All: uploadRequest.All,
ReplaceEqualNames: uploadRequest.ReplaceEqualName,
}
}
// UploadURL make a get request and forward the responsebody to a datavault upload
func (uploadRequest UploadRequest) UploadURL(u *url.URL, uploadDone chan string, cancel chan bool) (*UploadResponse, error) {
if len(uploadRequest.Name) == 0 {
uploadRequest.Name = u.Hostname()
}
client := http.Client{}
// Build a new request
req, err := http.NewRequest("GET", u.String(), nil)
req.Header.Add("User-Agent", "curl")
if err != nil {
return nil, err
}
// Do the request
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Try to get the size to upload
size := int64(0)
length := resp.Header.Get("Content-Length")
if len(length) > 0 {
size, _ = strconv.ParseInt(length, 10, 64)
}
// Upload the responses body
return uploadRequest.UploadFromReader(resp.Body, size, uploadDone, cancel)
}
// UploadFromReader upload a file using r as data source
func (uploadRequest *UploadRequest) UploadFromReader(r io.Reader, size int64, uploadDone chan string, cancel chan bool) (*UploadResponse, error) {
// Build request and body
request := uploadRequest.BuildRequestStruct(FileUploadType)
body, contenttype, size := uploadRequest.UploadBodyBuilder(r, size, uploadDone, cancel)
if body == nil {
return nil, fmt.Errorf("body is nil")
}
// Run filesize callback if set
if uploadRequest.fileSizeCallback != nil {
uploadRequest.fileSizeCallback(size)
}
resp, err := uploadRequest.Do(body, request, ContentType(contenttype))
if err != nil {
return nil, err
}
return resp, nil
}
// UploadFile uploads the given file to the server
func (uploadRequest *UploadRequest) UploadFile(f *os.File, uploadDone chan string, cancel chan bool) (*UploadResponse, error) {
// Check if file exists and use
// its size to provide a relyable
// upload filesize
fi, err := f.Stat()
if err != nil {
return nil, err
}
// Upload from file using it's io.Reader
return uploadRequest.UploadFromReader(f, fi.Size(), uploadDone, cancel)
}
// UploadArchivedFolder uploads the given folder to the server
func (uploadRequest *UploadRequest) UploadArchivedFolder(uri string, uploadDone chan string, cancel chan bool) (*UploadResponse, error) {
uploadRequest.Archive = true
// Use size of all files in dir as
// full upload size
size, err := gaw.GetDirSize(uri)
if err != nil {
return nil, err
}
errChan := make(chan error, 1)
doneChan := make(chan struct{}, 1)
pr, pw := io.Pipe()
defer pr.Close()
go func() {
// Compress dir
if err := archive(uri, pw); err != nil {
errChan <- err
}
defer pw.Close()
}()
var resp *UploadResponse
go func() {
// Upload from compress reader
resp, err = uploadRequest.UploadFromReader(pr, size, uploadDone, cancel)
if err != nil {
errChan <- err
} else {
doneChan <- struct{}{}
}
}()
select {
case err := <-errChan:
go func() {
uploadDone <- ""
}()
return nil, err
case <-doneChan:
return resp, nil
}
}
// Do does the final upload http request and uploads the src
func (uploadRequest *UploadRequest) Do(body io.Reader, payload interface{}, contentType ContentType) (*UploadResponse, error) {
// Make json header content
rbody, err := json.Marshal(payload)
if err != nil {
return nil, &ResponseErr{
Err: err,
}
}
// Do request
var resStruct UploadResponse
response, err := uploadRequest.NewRequest(EPFileUpload, body).
WithMethod(PUT).
// Negate the 'Compressed' field, to tell the server
// not to decompress the stream
WithCompression(!uploadRequest.Compressed).
WithAuth(uploadRequest.Config.GetBearerAuth()).WithHeader(HeaderRequest, base64.StdEncoding.EncodeToString(rbody)).
WithRequestType(RawRequestType).
WithContentType(contentType).
Do(&resStruct)
if err != nil || response.Status == ResponseError {
return nil, NewErrorFromResponse(response, err)
}
return &resStruct, err
}
// UploadBodyBuilder build the body for the upload file request
func (uploadRequest *UploadRequest) UploadBodyBuilder(reader io.Reader, inpSize int64, doneChan chan string, cancel chan bool) (r *io.PipeReader, contentType string, size int64) {
// Apply readerproxy
reader = uploadRequest.GetReaderProxy()(reader)
var err error
// Don't calculate a size if inputsize
// is empty to prevent returning an inalid size
if inpSize > 0 {
// Set filesize
switch uploadRequest.Encryption {
case 1:
size = inpSize + aes.BlockSize
case 2:
size = inpSize
case 0:
size = inpSize
default:
return nil, "", -1
}
}
r, pW := io.Pipe()
go func() {
// Create hashobject and use a multiwriter to
// write to the part and the hash at thes
var writer io.Writer
var gzipw *gzip.Writer
hash := crc32.NewIEEE()
if uploadRequest.Compressed {
// Server uses gzip stream to build chehcksum
// Write zip stream into hash writer as well
gzipw = gzip.NewWriter(io.MultiWriter((uploadRequest.GetWriterProxy()(pW)), hash))
writer = gzipw
} else {
// Server uses raw stream to build chehcksum, so put
// the hash writer separate
gzipw = gzip.NewWriter(uploadRequest.GetWriterProxy()(pW))
writer = io.MultiWriter(gzipw, hash)
}
buf := make([]byte, uploadRequest.GetBuffersize())
// Copy from input reader to writer using
// to support encryption
switch uploadRequest.Encryption {
case 1:
err = EncryptAES(writer, reader, uploadRequest.EncryptionKey, buf, cancel)
case 2:
err = EncryptAGE(writer, reader, uploadRequest.EncryptionKey, buf, cancel)
case 0:
err = cancelledCopy(writer, reader, buf, cancel)
}
var hsh string
if uploadRequest.Compressed {
// No server-side decompression will be executed
// so append hash to the end after full gzip stream
gzipw.Close()
hsh = hex.EncodeToString(hash.Sum(nil))
pW.Write([]byte(hsh))
} else {
// Server wil decompress data
// so add hash to the end and gzip
// it as well
hsh = hex.EncodeToString(hash.Sum(nil))
writer.Write([]byte(hsh))
gzipw.Close()
}
// Close everything and write into doneChan
if err != nil {
if err != ErrCancelled {
pW.CloseWithError(err)
doneChan <- ""
} else {
pW.Close()
doneChan <- "cancelled"
}
} else {
pW.Close()
doneChan <- hsh
}
}()
return
}