This repository has been archived by the owner on Jan 28, 2020. It is now read-only.
forked from architdate/CoreConsole
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.py
59 lines (50 loc) · 2.15 KB
/
server.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
#!/usr/bin/python -u
import subprocess
import os
import io
import uuid
import sys
from datetime import datetime
import base64
from flask import Flask, url_for, request, jsonify, send_file, Response
os.chdir(os.path.dirname(os.path.abspath(__file__)))
app = Flask(__name__)
DIR_OUTPUT = "output"
def ensureFolderExists(folder):
if not os.path.exists(folder):
os.makedirs(folder)
def datePath():
return datetime.now().strftime("%Y%m%d")
@app.route('/', methods=['GET'])
def hello():
return "PKSM"
@app.route('/api/legalize', methods = ['POST'])
def api_legalize():
try:
if request.headers['Content-Type'] == 'application/base64':
if not request.data or not request.headers.get('Version') or not request.headers.get("Size"):
print("Invalid request made!")
return None
storeFolder = os.path.join(DIR_OUTPUT, datePath())
ensureFolderExists(storeFolder)
uniqint = uuid.uuid4()
inputPath = os.path.join(storeFolder, str(uniqint) + ".pkx")
with open(inputPath, 'wb') as f:
f.write(base64.b64decode(request.data)[:int(request.headers['Size'])])
if os.name == 'nt':
proc = subprocess.Popen(['CoreConsole.exe', '-alm', '--version', request.headers.get('Version'), '-i', inputPath, '-o', inputPath], stdout=subprocess.PIPE, shell=True)
else:
proc = subprocess.Popen('mono CoreConsole.exe -alm --version {} -i "{}" -o "{}"'.format(request.headers.get('Version'), inputPath, inputPath), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = proc.communicate()
if err:
return Response("Error", status=400, mimetype='text/plain')
fname = str(uniqint) + '.pkx'
with open(inputPath, 'rb') as f:
pkx = f.read()
return send_file(io.BytesIO(pkx), attachment_filename=fname)
except Exception as e:
print(e)
return None, 400
if __name__ == '__main__':
ensureFolderExists(DIR_OUTPUT)
app.run(host='0.0.0.0', port=4001)