forked from bepasty/bepasty-client-cli
/
bepasty_cli.py
76 lines (59 loc) · 2.24 KB
/
bepasty_cli.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
# Copyright: 2014 Dennis Schmalacker <github@progde.de>
# License: BSD 2-clause, see LICENSE for details.
"""
bepasty-server commandline interface
"""
import os, sys, base64, pprint
from mimetypes import guess_type
import requests
import click
@click.command()
@click.option('-f', '--file', 'fileobj', help='File to be uploaded to a bepasty-server. If this is omitted stdin is read.')
@click.option('-n', '--name', 'fname', help='Filename for piped input.')
@click.option('-t', '--type', 'ftype', help='Filetype for piped input. Specified as file extension. E.g. png, txt, mp3...'
+ ' If omitted, filetype will be destinguised by filename')
def main(fileobj, fname, ftype):
pretty = pprint.PrettyPrinter()
if fileobj:
fileobj = open(fileobj, 'rb')
filesize = os.path.getsize(os.path.abspath(fileobj.name))
if not fname:
fname = fileobj.name
stdin = False
else:
fileobj = click.get_binary_stream('stdin')
if not fname:
fname = ''
stdin = True
if not ftype:
ftype, enc = guess_type(fname)
if not ftype:
ftype = 'application/octet-stream'
offset = 0
trans_id = ''
while True:
read_size = 1 * 1024 * 1024
raw_data = fileobj.read(read_size)
raw_data_size = len(raw_data)
payload = base64.b64encode(raw_data)
if stdin:
if raw_data_size < read_size:
filesize = offset + raw_data_size
else:
filesize = offset + raw_data_size + 1
headers = {
'content-range': ('bytes %d-%d/%d' % (offset, offset+raw_data_size-1, filesize)),
'content-type': ftype,
'content-filename': fname,
}
headers['Content-Length'] = filesize
if not trans_id == '':
headers['Transaction-ID'] = trans_id
response = requests.post('http://localhost:5000/api/v1/items', data=payload, headers=headers, auth=('user','foo'))
offset = offset + raw_data_size
if response.headers['Transaction-ID']:
trans_id = response.headers['Transaction-ID']
if raw_data_size < read_size:
break
if __name__ == '__main__':
main()