-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
75 lines (57 loc) · 2.16 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
import os
import logging
import requests
import json
from flask import Flask, request, render_template, jsonify, redirect, url_for
from flask_cors import CORS
from flask_socketio import SocketIO, emit
from werkzeug import secure_filename
from tools.config import UPLOAD_FOLDER, CONTENT_FOLDER, STYLED_FOLDER
# from tools.storage_utils import StorageUtils
from xform.tranImage import XForm
# Flask
app = Flask(__name__,
static_folder="./frontend/dist/static",
template_folder="./frontend/dist")
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# su = StorageUtils()
ALLOWED_EXTENSIONS = set(['png', 'ico', 'jpg', 'jpeg', 'gif'])
# CORS Middleware
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
CORS(app)
# app.config['CORS_HEADERS'] = 'Content-Type'
# Socket.io Middleware
socketio = SocketIO(app)
@socketio.on('upload', namespace='/style')
def image_upload(clientId, styleId, contentImage):
if (contentImage):
# TODO put this in try catch block and feature to upload / download file from S3
fileName = "{}.jpg".format(clientId)
fileWPath = os.path.join(CONTENT_FOLDER, fileName)
f = open(fileWPath, 'wb')
f.write(contentImage)
f.close()
print("file uploaded as {} and saved locally".format(fileWPath))
print(styleId)
xf = XForm()
status, outputImage, msg = xf.process_image(fileWPath,
styleId, False)
if status == True:
# convert the output image into a binary blob
f = open(outputImage, 'rb')
styledImageBlob = f.read()
print(outputImage)
emit('success', styledImageBlob, namespace='/style')
else:
emit("error", msg, namespace='/style')
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
if app.debug:
return requests.get('http://localhost:8080/{}'.format(path)).text
return render_template("index.html")
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
if __name__ == "__main__":
app.run(debug=True)