-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.py
85 lines (73 loc) · 3.09 KB
/
client.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
# flake8: noqa
import uuid
import datetime
import logging
import json
from flask import Flask, flash, request, redirect, url_for, render_template_string
import grpc
import ImageService_pb2 as service
import ImageService_pb2_grpc as rpc
from google.protobuf.json_format import MessageToJson
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', '.gif'}
CHUNK_SIZE = 1024 * 1024 # decrease the value here to evaluate memory usage
app = Flask(__name__)
logging.basicConfig(level=logging.INFO)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.template_filter('json_loads')
def json_loads_filter(s):
return json.loads(s) if s else None
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
if 'selected_files' not in request.files:
flash("No 'selected_files' part")
return redirect(request.url)
selected_files = request.files.getlist("selected_files")
results = []
for i, file in enumerate(selected_files, 1):
if file.filename == '':
flash('You must select at least one file')
return redirect(request.url)
if file and allowed_file(file.filename):
fileid = f'{datetime.datetime.utcnow().isoformat()}-{uuid.uuid4()}'
def upload_request_generator(): # this generates our grpc `stream ImageUploadRequest`
i = 1
while True:
b = file.read(CHUNK_SIZE)
if b:
result = service.ImageUploadRequest(Content=b, Id=fileid, StatusCode=service.ImageUploadStatusCode.InProgress) # noqa
else:
result = service.ImageUploadRequest(Id=fileid, StatusCode=service.ImageUploadStatusCode.Ok) # noqa
yield result
if not b:
break
result = stub.Upload(upload_request_generator())
logging.info(f'file {i} {file.name} was upload successfully')
results.append(MessageToJson(result))
return redirect(url_for('upload_file', json=json.dumps(results))) # we need a safe string to pass as url param
return render_template_string('''
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form method=post enctype=multipart/form-data>
<input type=file name=selected_files multiple>
<input type=submit value=Upload>
</form>
{% if json_response %}
<h1>Last upload</h1>
<ol>
{% for item in (json_response|json_loads) %}
<li>
<a href="{{ ((item|json_loads)['Message']|json_loads)['data']['link'] }}">{{ (item|json_loads)['Id'] }}</a><br>
<pre style="overflow: auto;">{{ item }}</pre>
</li>
{% endfor %}
</ol>
{% endif %}
''', json_response=request.args.get('json'))
if __name__ == "__main__":
channel = grpc.insecure_channel('grpc_service:22222')
stub = rpc.ImageServiceStub(channel)
app.run(host='0.0.0.0')