-
Notifications
You must be signed in to change notification settings - Fork 5
/
flask_app.py
87 lines (64 loc) · 2.5 KB
/
flask_app.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
from flask import Flask, flash, redirect, url_for, \
request, render_template, send_file
from os import path, urandom
from io import BytesIO
from zipfile import ZipFile
import converter
app = Flask(__name__)
app.secret_key = urandom(24)
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
app.debug = False
@app.route('/')
def default():
return render_template('index.html',
max_file_size=app.config['MAX_CONTENT_LENGTH'])
@app.route('/convert', methods=['POST'])
def convert():
xml_source = request.files['xmlFile']
msg_source = request.files['msgFile']
if xml_source.filename == '' or msg_source.filename == '':
flash('Please upload valid Xml and Msg files')
return redirect(url_for('default'))
msg_result = BytesIO()
hsh_result = BytesIO()
try:
converter.convert(xml_source.stream, msg_source.stream, msg_result)
msg_result.seek(0)
hsh_result.write(converter.generate_integrity_hash(msg_result))
msg_result.seek(0)
hsh_result.seek(0)
zip_stream = generate_zipfile(msg_result.getvalue(),
hsh_result.getvalue(),
'result')
return send_file(zip_stream,
attachment_filename='result.zip',
as_attachment=True)
except Exception as ex:
flash(type(ex).__name__ + ' - ' + str(ex.args))
return redirect(url_for('default'))
@app.route('/createchecksum', methods=['POST'])
def createchecksum():
msg_source = request.files['msgFile']
if msg_source.filename == '':
flash('Please upload valid Msg file')
return redirect(url_for('default'))
hsh_result = BytesIO()
try:
hsh_result.write(converter.generate_integrity_hash(msg_source.stream))
hsh_result.seek(0)
name, _ = path.splitext(msg_source.filename)
return send_file(hsh_result,
attachment_filename=name + '.hsh',
as_attachment=True)
except Exception as ex:
flash(type(ex).__name__ + ' - ' + str(ex.args))
return redirect(url_for('default'))
def generate_zipfile(msg_content, hsh_content, filename):
zip_stream = BytesIO()
with ZipFile(zip_stream, 'w') as zip_file:
zip_file.writestr(filename + '.msg', msg_content)
zip_file.writestr(filename + '.hsh', hsh_content)
zip_stream.seek(0)
return zip_stream
if __name__ == '__main__':
app.run()