2018-10-18 17:22:51 +00:00
|
|
|
from server.connections import Request, Response
|
2018-10-18 17:04:05 +00:00
|
|
|
import mimetypes
|
|
|
|
import handler
|
|
|
|
import uuid
|
|
|
|
import configparser
|
|
|
|
import os
|
|
|
|
import glob
|
2018-10-19 17:36:59 +00:00
|
|
|
import hashlib
|
|
|
|
import time
|
2018-10-21 17:27:34 +00:00
|
|
|
import re
|
2018-10-18 17:04:05 +00:00
|
|
|
|
|
|
|
HTTP200 = "200 OK"
|
|
|
|
HTTP202 = "202 Accepted"
|
|
|
|
HTTP400 = "400 Bad Request"
|
|
|
|
HTTP404 = "404 File Not Found"
|
|
|
|
HTTP405 = "405 Method Not Allowed"
|
|
|
|
HTTP500 = "500 Internal Server Error"
|
|
|
|
|
|
|
|
HTML = "text/html"
|
|
|
|
JSON = "application/json"
|
|
|
|
XML = "text/xml"
|
|
|
|
TEXT = "text/plain"
|
2018-10-21 06:57:55 +00:00
|
|
|
PNG = "image/png"
|
2019-02-18 16:23:47 +00:00
|
|
|
MKV = "video/mkv"
|
|
|
|
JPG = "image/jpeg"
|
2018-10-18 17:04:05 +00:00
|
|
|
|
|
|
|
def static(req):
|
|
|
|
try:
|
|
|
|
content = open("server/static/" + req.endpoint, "rb").read()
|
|
|
|
code = HTTP200
|
|
|
|
ctype = mimetypes.guess_type(req.endpoint)[0]
|
|
|
|
except:
|
|
|
|
code = HTTP404
|
|
|
|
content = "<h1>404 File Not Found</h1>"
|
|
|
|
content += """The file you requested was not found on the server.
|
|
|
|
Check the URL maybe?"""
|
|
|
|
ctype = HTML
|
|
|
|
content = content.encode()
|
|
|
|
|
|
|
|
return Response(code, ctype, content)
|
|
|
|
|
|
|
|
def addjob(req):
|
|
|
|
jobid = str(uuid.uuid4())
|
2018-10-21 12:21:48 +00:00
|
|
|
config = configparser.ConfigParser(strict=False, interpolation=None)
|
2018-10-21 07:16:27 +00:00
|
|
|
|
2018-10-18 17:04:05 +00:00
|
|
|
try:
|
2018-10-21 17:21:56 +00:00
|
|
|
title = re.sub(r"[^a-zA-Z0-9_\-]", "_", req.args["title"][0]) or "output"
|
2018-10-21 17:27:34 +00:00
|
|
|
except Exception as e:
|
2018-10-18 17:04:05 +00:00
|
|
|
title = "output"
|
|
|
|
|
|
|
|
try:
|
2018-10-21 07:16:27 +00:00
|
|
|
rx = int(req.args["rx"][0]) or 0
|
2018-10-18 17:04:05 +00:00
|
|
|
except:
|
|
|
|
rx = 0
|
|
|
|
|
|
|
|
try:
|
2018-10-21 07:16:27 +00:00
|
|
|
ry = int(req.args["ry"][0]) or 0
|
2018-10-18 17:04:05 +00:00
|
|
|
except:
|
|
|
|
ry = 0
|
|
|
|
|
|
|
|
try:
|
2018-10-21 07:16:27 +00:00
|
|
|
rz = int(req.args["rz"][0]) or 0
|
2018-10-18 17:04:05 +00:00
|
|
|
except:
|
|
|
|
rz = 0
|
|
|
|
|
|
|
|
try:
|
2018-10-21 07:16:27 +00:00
|
|
|
width = int(req.args["width"][0]) or 3840
|
2018-10-18 17:04:05 +00:00
|
|
|
except:
|
|
|
|
width = 3840
|
|
|
|
|
|
|
|
try:
|
2018-10-21 07:16:27 +00:00
|
|
|
height = int(req.args["height"][0]) or 1920
|
2018-10-18 17:04:05 +00:00
|
|
|
except:
|
|
|
|
height = 1920
|
|
|
|
|
|
|
|
config["Job"] = {
|
|
|
|
"url": req.args["url"][0],
|
|
|
|
"title": title,
|
|
|
|
"rx": rx,
|
|
|
|
"ry": ry,
|
|
|
|
"rz": rz,
|
|
|
|
"width": width,
|
|
|
|
"height": height
|
|
|
|
}
|
|
|
|
|
|
|
|
with open("/tmp/panosteal/" + jobid, "w") as outfile:
|
|
|
|
config.write(outfile)
|
|
|
|
|
|
|
|
content = jobid.encode()
|
|
|
|
ctype = TEXT
|
|
|
|
status = HTTP200
|
|
|
|
|
|
|
|
return Response(status, ctype, content)
|
|
|
|
|
|
|
|
def getjob(req):
|
2019-02-18 16:23:47 +00:00
|
|
|
jobid = req.path[-1].rstrip("-thumb").rstrip("-info")
|
2018-10-18 17:04:05 +00:00
|
|
|
content_disposition = None
|
|
|
|
|
2019-02-18 16:23:47 +00:00
|
|
|
found = (glob.glob("/tmp/panosteal/%s---*.png" % jobid) + glob.glob("/tmp/panosteal/%s---*.mkv" % jobid)) if not req.path[-1].endswith("thumb") else glob.glob("/tmp/panosteal/%s---*.jpg" % jobid)
|
2018-10-19 17:36:59 +00:00
|
|
|
|
|
|
|
if found:
|
|
|
|
md5 = "Not happening."
|
|
|
|
while True:
|
2019-02-18 16:23:47 +00:00
|
|
|
content = open(found[0], "rb").read() if not req.path[-1].endswith("info") else b""
|
2018-10-19 17:36:59 +00:00
|
|
|
newmd5 = hashlib.md5(content).hexdigest()
|
|
|
|
if newmd5 == md5:
|
|
|
|
break
|
|
|
|
md5 = newmd5
|
|
|
|
time.sleep(0.5)
|
|
|
|
|
2018-10-18 17:04:05 +00:00
|
|
|
code = HTTP200
|
2019-02-18 16:23:47 +00:00
|
|
|
ctype = PNG if found[0].endswith(".png") else JPG if found[0].endswith(".jpg") else MKV
|
|
|
|
content_disposition = found[0].split("---")[-1] if not req.path[-1].endswith("info") else None
|
2018-10-21 06:57:55 +00:00
|
|
|
|
2018-10-19 17:36:59 +00:00
|
|
|
elif glob.glob("/tmp/panosteal/%s*err" % jobid):
|
2018-10-18 17:04:05 +00:00
|
|
|
content = "<h1>500 Internal Server Error</h1>".encode()
|
|
|
|
code = HTTP500
|
|
|
|
ctype = HTML
|
2018-10-21 06:57:55 +00:00
|
|
|
|
2018-10-18 17:04:05 +00:00
|
|
|
elif not os.path.isfile("/tmp/panosteal/%s" % jobid):
|
|
|
|
content = "<h1>404 File Not Found</h1>".encode()
|
|
|
|
code = HTTP404
|
|
|
|
ctype = HTML
|
|
|
|
else:
|
|
|
|
content = "".encode()
|
|
|
|
code = HTTP202
|
2018-10-19 17:36:59 +00:00
|
|
|
ctype = TEXT
|
2018-10-18 17:04:05 +00:00
|
|
|
|
|
|
|
res = Response(code, ctype, content)
|
|
|
|
|
|
|
|
if content_disposition:
|
|
|
|
res.addHeader("Content-Disposition", 'attachment; filename="%s"' % content_disposition)
|
|
|
|
return res
|
|
|
|
|
|
|
|
def application(env, re):
|
|
|
|
req = Request(env)
|
|
|
|
|
|
|
|
if req.endpoint.lower() == "addjob":
|
|
|
|
handler = addjob
|
|
|
|
elif req.endpoint.lower() == "getjob":
|
|
|
|
handler = getjob
|
|
|
|
else:
|
|
|
|
handler = static
|
|
|
|
|
|
|
|
res = handler(req)
|
|
|
|
re(res.status, res.headers)
|
|
|
|
yield res.content
|
|
|
|
|