-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.py
139 lines (113 loc) · 4.58 KB
/
application.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Main script to run the degree management web-app.
Usage:
application.py <port> <private-key-path> <path-water-mark-template>
Options:
-h --help Show this screen.
<port> Port to run the server example 8080
<private-key-path> Path to the private key
<path-water-mark-template> Path to the water mark template generate by water_mark_generator.py
"""
from __future__ import absolute_import
import logging.handlers
import os
from docopt import docopt
from flask import Flask, render_template, send_file, request
from flask_bootstrap import Bootstrap
from waitress import serve
from werkzeug.utils import secure_filename
from degree import save_image, read_image, sign_degree_generator, verify_degree, DeepWaterMark, \
sign_deep_degree_generator, verify_deep_degree
PYTHON_LOGGER = logging.getLogger(__name__)
if not os.path.exists("log"):
os.mkdir("log")
HDLR = logging.handlers.TimedRotatingFileHandler("log/application.log",
when="midnight", backupCount=60)
STREAM_HDLR = logging.StreamHandler()
FORMATTER = logging.Formatter("%(asctime)s %(filename)s [%(levelname)s] %(message)s")
HDLR.setFormatter(FORMATTER)
STREAM_HDLR.setFormatter(FORMATTER)
PYTHON_LOGGER.addHandler(HDLR)
PYTHON_LOGGER.addHandler(STREAM_HDLR)
PYTHON_LOGGER.setLevel(logging.DEBUG)
# Absolute path to the folder location of this python file
FOLDER_ABSOLUTE_PATH = os.path.normpath(os.path.dirname(os.path.abspath(__file__)))
app = Flask(__name__)
app.secret_key = "zefzarega5zerg+6e5rzeafz"
Bootstrap(app)
app.config['BOOTSTRAP_SERVE_LOCAL'] = True
PRIVATE_KEY_PATH = None
WATER_MARK_PATH = None
DEEP_WATER_MARK = None
@app.route('/', methods=["GET", "POST"])
def home():
"""
Display the home page.
"""
return render_template("index.html")
@app.route("/degree_creator")
def degree_creator():
"""
Display the degree creation page.
"""
return render_template("degree_creator.html")
@app.route("/degree_validator")
def degree_validator():
"""
Display the degree verification page.
"""
return render_template("degree_validator.html")
@app.route("/download", methods=['GET', 'POST'])
def download():
"""
Download a degree.
"""
if request.method == 'POST':
player_name = request.form["player_name"]
player_score = request.form["player_score"]
if "classic" in request.form:
output_image = sign_degree_generator(PRIVATE_KEY_PATH, player_name, int(player_score))
save_image("generated_degree.png", output_image)
# Neuronal method
elif "neuronal" in request.form:
output_image = sign_deep_degree_generator(PRIVATE_KEY_PATH,
player_name,
int(player_score),
DEEP_WATER_MARK)
save_image("generated_degree.png", output_image)
return send_file("generated_degree.png", as_attachment=True)
@app.route("/upload", methods=['GET', 'POST'])
def upload():
"""
Save a file and check if it is a valid degree.
"""
if request.method == 'POST':
try:
uploaded_file = request.files['uploaded_file']
file_name = secure_filename(uploaded_file.filename)
uploaded_file.save(file_name)
img = read_image(file_name)
if "classic" in request.form:
verify, score = verify_degree(PRIVATE_KEY_PATH, img)
# Neuronal method
else:
verify, score = verify_deep_degree(PRIVATE_KEY_PATH, DEEP_WATER_MARK, img)
verification_message = "authentic" if verify else "not authentic"
return "The input degree {} is {}. The score his {}".format(os.path.basename(file_name),
verification_message,
score)
except Exception as e:
PYTHON_LOGGER.error("error in download: {}".format(e))
return "Verification failed. Please check your input file."
if __name__ == "__main__":
args = docopt(__doc__)
try:
port = int(args["<port>"])
except ValueError:
port = 8080
PRIVATE_KEY_PATH = args["<private-key-path>"]
DEEP_WATER_MARK = DeepWaterMark(args["<path-water-mark-template>"])
PYTHON_LOGGER.info("Start server to the port {}".format(port))
serve(app, host="0.0.0.0", port=port)