-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathapp.py
296 lines (217 loc) · 8.24 KB
/
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
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
from dotenv import load_dotenv
from ServersCollection import ServersCollection
load_dotenv()
from flask import Flask, jsonify, make_response, render_template, request
from flask_socketio import SocketIO
from MosaicWorker import MosaicWorker
import os
from urllib.parse import quote_plus
app = Flask(__name__)
app.jinja_env.filters['quote_plus'] = lambda u: quote_plus(u)
socketio = SocketIO(app)
useDB = False
if os.getenv("ADMIN_PASSCODE"):
useDB = True
servers = ServersCollection(useDB)
os.makedirs("mosaics", exist_ok=True)
from concurrent.futures import ThreadPoolExecutor
threadPool = ThreadPoolExecutor(max_workers=30)
@app.route("/", methods=["GET"])
def GET_index():
"""Route for "/" (frontend)"""
disableInterface = False
if os.getenv("ADMIN_PASSCODE"):
disableInterface = True
if "admin" in request.cookies and request.cookies.get("admin") == os.getenv("ADMIN_PASSCODE"):
disableInterface = False
return render_template("index.html", data={"disableInterface": disableInterface})
@app.route("/addMMG", methods=["PUT"])
def PUT_addMMG():
"""Add a mosaic microservice generator"""
# Check for required fields:
for requiredField in ["name", "url", "author", "tileImageCount"]:
if requiredField not in request.form:
error = f"Required field {requiredField} is not present in /addMMG request."
print(f"❌ REJECTED /addMMG: {error}")
return jsonify({"error": error})
# Add the MMG:
result = servers.addMMG(
name = request.form["name"],
url = request.form["url"],
author = request.form["author"],
tiles = int(request.form["tileImageCount"]),
)
return jsonify(result["id"]), 200
@app.route("/registerReducer", methods=["PUT"])
def PUT_registerReducer():
"""Registers a reducer"""
# Check for required fields:
for requiredField in ["url", "author"]:
if requiredField not in request.form:
error = f"Required field {requiredField} is not present in /registerReducer request."
print(f"❌ REJECTED /registerReducer: {error}")
return jsonify({"error": error})
result = servers.addReducer(
url = request.form["url"],
author = request.form["author"],
)
return jsonify(result["id"]), 200
@app.route("/makeMosaic", methods=["POST"])
async def POST_makeMosaic():
"""Route to generate mosaic"""
if os.getenv("ADMIN_PASSCODE") and ("admin" not in request.cookies or request.cookies.get("admin") != os.getenv("ADMIN_PASSCODE")):
return jsonify({"error": "This server is currently in admin-only mode. You are unable to add an image."}), 400
try:
input_file = request.files["image"]
baseImage = input_file.read()
worker = MosaicWorker(
baseImage = baseImage,
tilesAcross = int(request.form["tilesAcross"]),
renderedTileSize = int(request.form["renderedTileSize"]),
fileFormat = request.form["fileFormat"],
servers = servers,
socketio = socketio,
threadPool = threadPool,
)
# MMGs and Filtering
filterQuery = request.form["filter"]
if filterQuery == "":
filterQuery = False
for id in servers.mmgs:
mmg = servers.mmgs[id]
if filterQuery and filterQuery not in mmg["name"]:
continue
if "disabled" in mmg and mmg["disabled"]:
continue
worker.addMMG( mmg )
# Reducers and Verification
verifiedOnly = False
if "verified" in request.form:
verifiedOnly = True
for id in servers.reducers:
reducer = servers.reducers[id]
if verifiedOnly and reducer["verification"] != "GOOD":
continue
if "disabled" in reducer and reducer["disabled"]:
continue
worker.addReducer( reducer )
result = worker.createMosaic()
return jsonify(result)
except KeyError as e:
print(e)
return jsonify({"error": "Please upload an image file."}), 400
except Exception as e:
import traceback
traceback.print_exc()
return jsonify({"error": str(e)}), 400
@app.route("/serverList", methods=["GET"])
def GET_serverList():
"""Route to get connected servers"""
servers_by_author = {}
for key in servers.mmgs:
mmg = servers.mmgs[key]
author = mmg["author"]
if author not in servers_by_author:
servers_by_author[author] = []
servers_by_author[author].append(mmg)
for key in servers.reducers:
reducer = servers.reducers[key]
author = reducer["author"]
if author not in servers_by_author:
servers_by_author[author] = []
servers_by_author[author].append(reducer)
return render_template("servers.html", data=servers_by_author)
def isAdmin():
if os.getenv("ADMIN_PASSCODE") and "admin" in request.cookies and request.cookies.get("admin") == os.getenv("ADMIN_PASSCODE"):
return True
else:
return False
@app.route("/singleAuthor", methods=["GET"])
def GET_singleAuthor():
if not isAdmin():
return render_template("disabled.html")
author = request.args.get("author")
return render_template("singleAuthor.html", data={"author": author, "isAdmin": isAdmin()})
def verify(how):
if not isAdmin():
return jsonify({"error": "This server is currently in admin-only mode. You are unable to add an image."}), 400
author = request.args.get("author")
for key in servers.reducers:
reducer = servers.reducers[key]
if author == reducer["author"]:
servers.updateValue(reducer, "verification", how)
return jsonify({"verification": how})
return jsonify({"error": "author not found"})
@app.route("/verify_GOOD", methods=["GET"])
def GET_verify_GOOD():
return verify("GOOD")
@app.route("/verify_BROKEN", methods=["GET"])
def GET_verify_BROKEN():
return verify("BROKEN")
@app.route("/toggleLate", methods=["GET"])
def GET_toggleLate():
if not isAdmin(): return jsonify({"error": "Requires Admin"})
return jsonify({"result": servers.toggleAfterDeadline()})
@app.route("/clearErrors", methods=["GET"])
def GET_clearErrors():
if not isAdmin(): return jsonify({"error": "Requires Admin"})
return jsonify({"result": servers.clearErrors()})
@app.route("/admin", methods=["GET"])
def GET_admin():
return render_template("admin.html")
@app.route("/adminEnable", methods=["POST"])
def POST_adminEnable():
cookie = request.form["cookie"]
resp = make_response(render_template("admin.html"))
resp.set_cookie("admin", cookie)
return resp
rainbowTest = None
with open("testFiles/rainbow.png", "rb") as f:
rainbowTest = f.read()
imgA = None
with open("testFiles/A.png", "rb") as f:
imgA = f.read()
imgB = None
with open("testFiles/B.png", "rb") as f:
imgB = f.read()
imgC = None
with open("testFiles/C.png", "rb") as f:
imgC = f.read()
imgD = None
with open("testFiles/D.png", "rb") as f:
imgD = f.read()
@app.route("/testMosaic", methods=["GET"])
async def GET_testMosaic():
if not isAdmin():
return jsonify({"error": "Requires admin access."}), 400
author = request.args.get("author")
worker = MosaicWorker(
baseImage = rainbowTest,
tilesAcross = 50,
renderedTileSize = 10,
fileFormat = "PNG",
socketio = socketio,
servers = servers,
threadPool = threadPool,
socketio_filter = f" {author}",
)
for id in servers.mmgs:
mmg = servers.mmgs[id]
if "disabled" in mmg and mmg["disabled"]:
continue
if mmg["author"] == author:
worker.addMMG( mmg )
for id in servers.reducers:
reducer = servers.reducers[id]
if "disabled" in reducer and reducer["disabled"]:
continue
if reducer["author"] == author:
worker.addReducer( reducer )
try:
worker.testMosaic()
worker.testReduction(imgA, imgB, imgC, imgD)
return jsonify([])
except Exception as e:
import traceback
traceback.print_exc()
return jsonify({"error": str(e)}), 400