openwebrx-clone/owrx/controllers/imageupload.py

37 lines
1.3 KiB
Python
Raw Normal View History

2021-02-08 22:29:24 +00:00
from owrx.controllers.assets import AssetsController
from owrx.config import CoreConfig
import uuid
import json
2021-02-08 22:30:44 +00:00
# TODO: implement authorization
2021-02-08 22:29:24 +00:00
class ImageUploadController(AssetsController):
def __init__(self, handler, request, options):
super().__init__(handler, request, options)
self.uuid = request.query["uuid"][0] if "uuid" in request.query else None
self.id = request.query["id"][0] if "id" in request.query else None
def getFilePath(self, file=None):
if self.uuid is None:
raise FileNotFoundError("missing uuid")
if self.id is None:
raise FileNotFoundError("missing id")
return "{tmp}/{file}-{uuid}".format(
tmp=CoreConfig().get_temporary_directory(),
file=self.id,
uuid=self.uuid,
)
def indexAction(self):
self.serve_file(None)
def processImage(self):
self.uuid = uuid.uuid4().hex
2021-02-08 22:30:44 +00:00
# TODO: limit file size
# TODO: check image mime type, if possible
2021-02-08 22:29:24 +00:00
contents = self.get_body()
# TODO: clean up files after timeout or on shutdown
2021-02-08 22:29:24 +00:00
with open(self.getFilePath(), 'wb') as f:
f.write(contents)
self.send_response(json.dumps({"uuid": self.uuid}), content_type="application/json")