-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilesender_sagc.py
executable file
·571 lines (468 loc) · 17.4 KB
/
filesender_sagc.py
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
#!/usr/bin/env python3
#
# FileSender www.filesender.org
#
# Copyright (c) 2009-2019, AARNet, Belnet, HEAnet, SURFnet, UNINETT
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of AARNet, Belnet, HEAnet, SURFnet and UNINETT nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import argparse
try:
import requests
import time
from collections.abc import Iterable
from collections.abc import MutableMapping
import hmac
import hashlib
import urllib3
import os
import json
import configparser
from os.path import expanduser
from multiprocessing import Pool
from functools import partial
from string import Template
except Exception as e:
print(type(e))
print(e.args)
print(e)
print('')
print('ERROR: A required dependency is not installed, please check your')
print('distribution packages or run something like the following')
print('')
print('pip3 install requests urllib3 ')
exit(1)
##########################################################################
def flatten(d, parent_key=''):
items = []
for k, v in d.items():
new_key = parent_key + '[' + k + ']' if parent_key else k
if isinstance(v, MutableMapping):
items.extend(flatten(v, new_key).items())
else:
items.append(new_key+'='+v)
items.sort()
return items
def call(method, path, data, content=None, rawContent=None, options={}):
data['remote_user'] = username
data['timestamp'] = str(round(time.time()))
flatdata = flatten(data)
signed = bytes(method+'&'+base_url.replace('https://', '',
1).replace('http://', '', 1)+path+'?'+('&'.join(flatten(data))), 'ascii')
content_type = options['Content-Type'] if 'Content-Type' in options else 'application/json'
inputcontent = None
if content is not None and content_type == 'application/json':
inputcontent = json.dumps(content, separators=(',', ':'))
signed += bytes('&'+inputcontent, 'ascii')
elif rawContent is not None:
inputcontent = rawContent
signed += bytes('&', 'ascii')
signed += inputcontent
# print(signed)
bkey = bytearray()
bkey.extend(map(ord, apikey))
data['signature'] = hmac.new(bkey, signed, hashlib.sha1).hexdigest()
url = base_url+path+'?'+('&'.join(flatten(data)))
headers = {
"Accept": "application/json",
"Content-Type": content_type
}
response = None
if method == "get":
response = requests.get(url, verify=not insecure, headers=headers)
elif method == "post":
response = requests.post(
url, data=inputcontent, verify=not insecure, headers=headers)
elif method == "put":
response = requests.put(url, data=inputcontent,
verify=not insecure, headers=headers)
elif method == "delete":
response = requests.delete(url, verify=not insecure, headers=headers)
if response is None:
raise Exception('Client error')
code = response.status_code
# print(url)
# print(inputcontent)
# print(code)
# print(response.text)
if code != 200:
if method != 'post' or code != 201:
raise Exception('Http error '+str(code)+' '+response.text)
if response.text == "":
raise Exception('Http error '+str(code)+' Empty response')
if method != 'post':
return response.json()
r = {}
r['location'] = response.headers['Location']
r['created'] = response.json()
return r
def postTransfer(user_id, files, recipients, subject=None, message=None, expires=None, options=[]):
if expires is None:
expires = round(time.time()) + (default_transfer_days_valid*24*3600)
print(expires)
to = [x.strip() for x in recipients.split(',')]
return call(
'post',
'/transfer',
{},
{
'from': user_id,
'files': files,
'recipients': to,
'subject': subject,
'message': message,
'expires': expires,
'aup_checked': 1,
'options': options
},
None,
{}
)
def putChunk(t, f, chunk, offset):
return call(
'put',
'/file/'+str(f['id'])+'/chunk/'+str(offset),
{'key': f['uid'], 'roundtriptoken': t['roundtriptoken']},
None,
chunk,
{'Content-Type': 'application/octet-stream'}
)
def fileComplete(t, f):
return call(
'put',
'/file/'+str(f['id']),
{'key': f['uid'], 'roundtriptoken': t['roundtriptoken']},
{'complete': True},
None,
{}
)
def transferComplete(transfer):
return call(
'put',
'/transfer/'+str(transfer['id']),
{'key': transfer['files'][0]['uid']},
{'complete': True},
None,
{}
)
def deleteTransfer(transfer):
return call(
'delete',
'/transfer/'+str(transfer['id']),
{'key': transfer['files'][0]['uid']},
None,
None,
{}
)
##########################################################################
def upload_file( fileobject, transferData, filesData, upload_chunk_size, debug):
"""This is the mp worker that replaces the last "try" block in the original script
"""
fname = fileobject["name"]
fsize = fileobject["size"]
fstring = f"{fname}:{fsize}"
fpath = filesData[fstring]["path"]
try:
# putChunks
if debug:
print('putChunks: '+fpath)
with open(fpath, mode='rb', buffering=0) as fin:
chunk_count = 0
for offset in range(0, fsize, upload_chunk_size):
if progress:
print('Uploading: '+fpath+' '+str(offset)+'-'+str(min(offset +
upload_chunk_size, fsize))+' '+str(round(offset/fsize*100))+'%')
data = fin.read(upload_chunk_size)
# print(data)
putChunk(transferData, fileobject, data, offset)
if debug:
chunk_count += 1
print(f"uploaded {chunk_count} chunks")
# file complete
if debug:
print('fileComplete: '+fpath)
fileComplete(transferData, fileobject)
if progress:
print('Uploading: '+fpath+' '+str(size)+' 100%')
except Exception as e:
raise(e)
def transfer_data_to_text(tdata):
total_size = 0
for f in tdata["files"]:
total_size += f["size"]
size_unit = "Bytes"
if total_size > 1024**3:
size_unit = "GB"
total_size = total_size/(1024**3)
elif total_size > 1024**2:
size_unit = "MB"
total_size = total_size/(1024**2)
elif total_size > 1024:
size_unit = "kB"
total_size = total_size/(1024)
if size_unit == "Bytes":
size_str = f"{total_size} {size_unit}"
else:
size_str = f"{total_size:.2f} {size_unit}"
url = tdata["recipients"][0]["download_url"]
report_txt = f"""FileSender upload
transfer ID: {tdata["id"]}
uploaded by: {tdata["user_email"]}
total size: {size_str}
recipient: {tdata["recipients"][0]["email"]}
D/L link: {url}
Files uploaded:
"""
for fidx, f in enumerate(tdata["files"]):
url = f"https://filesender.aarnet.edu.au/download.php?token={tdata['recipients'][0]['token']}&files_ids={f['id']}"
ct_str = f"{fidx}".rjust( len(str(len(tdata["files"]))) )
report_txt += f"""
{ct_str}: "{f["name"]}"
{" "*len(ct_str)} {f["size"]:,} bytes
{" "*len(ct_str)} {url}
"""
return report_txt
# -------------------------------------------------------------------------------
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# settings
base_url = 'https://filesender.aarnet.edu.au/rest.php'
default_transfer_days_valid = 21
username = None
apikey = None
homepath = expanduser("~")
recipients = None
config = configparser.ConfigParser()
config.read(homepath + '/.filesender/filesender.py.ini')
if 'system' in config:
base_url = config['system'].get(
'base_url', 'https://filesender.aarnet.edu.au/rest.php')
default_transfer_days_valid = int(
config['system'].get('default_transfer_days_valid', 10))
if 'user' in config:
username = config['user'].get('username')
apikey = config['user'].get('apikey')
if 'recipients' in config:
recipients = config['recipients'].get('recipients')
# argv
parser = argparse.ArgumentParser()
parser.add_argument("files", help="path to file(s) to send", nargs='+')
parser.add_argument("-v", "--verbose", action="store_true")
parser.add_argument("-i", "--insecure", action="store_true")
parser.add_argument("-p", "--progress", action="store_true")
parser.add_argument("-s", "--subject", default="", type=str)
parser.add_argument("-m", "--message", default="", type=str)
parser.add_argument("-k", "--skip-email", action="store_true", default=False, help="Don't send email to recipient")
parser.add_argument("-n", "--n_procs", default=1, type=int, help="number of parallel uploads")
# if we have found these in the config file they become optional arguments
requiredNamed = parser.add_argument_group('required named arguments')
if username is None:
requiredNamed.add_argument("-u", "--username", required=True)
else:
parser.add_argument("-u", "--username")
if apikey is None:
requiredNamed.add_argument("-a", "--apikey", required=True)
else:
parser.add_argument("-a", "--apikey")
if recipients is None:
requiredNamed.add_argument("-r", "--recipients", required=True)
else:
parser.add_argument("-r", "--recipients")
# final reporting logs
parser.add_argument("--report", "-R", type=str, help="filepath for transfer report")
# reporttype = parser.add_mutually_exclusive_group(required=True)
parser.add_argument("--report-json", "-j", action="store_true", help="Output transfer report in JSON")
parser.add_argument("--report-text", "-t", action="store_true", help="Output transfer report in text (default)")
# parser.add_argument("--report-both", "-b", action="store_true", help="Report both JSON and txt formats")
parser.add_argument("--quiet", "-q", action="store_true", help="Quiet mode. No report.")
args = parser.parse_args()
debug = args.verbose
progress = args.progress
insecure = args.insecure
n_procs = args.n_procs
skip_email = args.skip_email
if args.username is not None:
username = args.username
if args.apikey is not None:
apikey = args.apikey
if args.recipients is not None:
recipients = args.recipients
# -------------------------------------------------------------------------------
# -------------------------------------------------------------------------------
# test API, get info
# configs
try:
response = requests.get(base_url+'/info', verify=True)
except requests.exceptions.SSLError as exc:
if not insecure:
print('Error: the SSL certificate of the server you are connecting to cannot be verified:')
print(exc)
print('For more information, please refer to https://www.digicert.com/ssl/. If you are absolutely certain of the identity of the server you are connecting to, you can use the --insecure flag to bypass this warning. Exiting...')
sys.exit(1)
elif insecure:
print('Warning: Error: the SSL certificate of the server you are connecting to cannot be verified:')
print(exc)
print('Running with --insecure flag, ignoring warning...')
response = requests.get(base_url+'/info', verify=False)
upload_chunk_size = response.json()['upload_chunk_size']
# -------------------------------------------------------------------------------
# test local file output
write_to_stdout = True
outprefix = None
outdir = None
if args.report:
outprefix = os.path.abspath(args.report)
outdir = os.path.dirname(outprefix)
try:
os.makedirs(outdir, exist_ok=True)
fout = open(outprefix+".test", "w")
fout.write("")
fout.close()
os.remove(outprefix + ".test")
write_to_stdout = False
except Exception as e:
print("""
###########
# WARNING #
###########
Unable to write output to specified location. Printing to stdout instead
""")
outprefix = None
write_to_stdout = True
WRITE_JSON = args.report_json
WRITE_TEXT = args.report_text
QUIET = args.quiet
if not WRITE_JSON and not WRITE_TEXT:
WRITE_TEXT = True
# -------------------------------------------------------------------------------
file_list_short_string = ",".join(args.files)
if len(args.files) > 6:
file_list_short_string = ",".join( args.files[:3] ) + "..." + ",".join( args.files[-3:] )
if debug:
print('base_url : '+base_url)
print('username : '+username)
print('apikey : '+apikey)
print('upload_chunk_size : '+str(upload_chunk_size)+' bytes')
print('recipients : '+recipients)
print('files : '+','.join(args.files))
print('insecure : '+str(insecure))
##########################################################################
# -------------------------------------------------------------------------------
# Start upload process
SPLIT_LIMIT = 1000
MAX_PER_SPLIT = int(0.95*SPLIT_LIMIT)
# postTransfer
if debug:
print('postTransfer')
n_sets = 1
if len(args.files) < SPLIT_LIMIT:
input_file_list = [args.files]
else:
# split into sets of 0.95*SPLIT_LIMIT files
input_file_list = []
n_sets = int(len(args.files) / MAX_PER_SPLIT )
if n_sets * 950 < len(args.files):
n_sets += 1
for n in range(n_sets):
input_file_list.append( args.files[n*MAX_PER_SPLIT:(n+1)*MAX_PER_SPLIT] )
# ------------------------
# now iterate through sets of input_file_list
Responses = []
for file_set in range(n_sets):
# get input file list
files = {}
filesTransfer = []
for f in input_file_list[file_set]:
fn_abs = os.path.abspath(f)
fn = os.path.basename(fn_abs)
size = os.path.getsize(fn_abs)
files[fn+':'+str(size)] = {
'name': fn,
'size': size,
'path': fn_abs
}
filesTransfer.append({'name': fn, 'size': size})
troptions = {'get_a_link': skip_email}
# sort by decreasing file size
filesTransfer = sorted(filesTransfer, key=lambda x: x["size"], reverse=True)
transfer = postTransfer(username,
filesTransfer,
recipients,
subject=args.subject,
message=args.message,
expires=None,
options=troptions)['created']
# ----------------------------------------------------------------------
# transferring data
n_procs = min(n_procs, len(filesTransfer))
task = partial(upload_file,
transferData=transfer,
filesData=files,
upload_chunk_size=upload_chunk_size,
debug=debug)
pool = Pool(n_procs)
pool.map(task, transfer['files'])
pool.close()
# transferComplete
if debug:
print('transferComplete')
finalResponse = transferComplete(transfer)
if progress:
print('Upload Complete')
Responses.append(finalResponse)
# --------------------------------------------------
if QUIET:
exit()
# --------------------------------------------------
if WRITE_TEXT:
if n_sets == 1:
report_text = transfer_data_to_text(Responses[0])
elif n_sets > 1:
report_text = f"There were more than 1,000 files uploaded, so upload data were split into {n_sets} transfers\n"
for n in range(n_sets):
report_text += f"""
Set {n+1}:
{transfer_data_to_text(Responses[n])}
-------------------------------
"""
if outprefix:
fout = open(outprefix + ".txt", "w")
fout.write(report_text)
fout.close()
else:
print(f"""
#-----------------------------
# Text Report
{report_text}
""")
if WRITE_JSON:
if outprefix:
fout = open( outprefix + ".json", "w" )
fout.write( json.dumps(finalResponse) )
fout.close()
else:
print(f"""
#-----------------------------
# JSON Report
{json.dumps(finalResponse, indent=1)}
""")