diff --git a/echo360/__init__.py b/echo360/__init__.py index 80ffce2..473b7c4 100644 --- a/echo360/__init__.py +++ b/echo360/__init__.py @@ -2,6 +2,7 @@ import json from pathlib import Path import falcon +from falcon_multipart.middleware import MultipartMiddleware from .tools import JsonRequest, JsonResponse, error_handler from .controllers import * @@ -18,7 +19,9 @@ class AboutResource(): resp.body = json.dumps(r, indent=4) -app = application = falcon.API(request_type=JsonRequest, response_type=JsonResponse) +app = application = falcon.API(middleware=[MultipartMiddleware()], + request_type=JsonRequest, + response_type=JsonResponse) app.add_error_handler(ValueError, error_handler) app.add_route("/", AboutResource()) @@ -30,4 +33,5 @@ app.add_route("/Videos/{VideoUuid}/Streams/{StreamId}", StreamCollection()) FilesStoragePath = Path("files") FilesStoragePath.mkdir(parents=True, exist_ok=True) -app.add_route("/File/{FileId}", FileCollection(FilesStoragePath)) +app.add_route("/Files/{FileId}", FileCollection(FilesStoragePath)) +app.add_route("/Files", FileCollection(FilesStoragePath)) diff --git a/echo360/controllers.py b/echo360/controllers.py index e067e42..65935bf 100644 --- a/echo360/controllers.py +++ b/echo360/controllers.py @@ -1,6 +1,6 @@ from enum import IntEnum from pathlib import Path -from falcon import HTTPNotFound +from falcon import HTTPNotFound, HTTP_CREATED class TaskStatus(IntEnum): Created = 0 @@ -24,9 +24,9 @@ streams = [ "StreamId":0, "MimeType":"application/x-shockwave-flash", "Uris":[ - {"Uri":"http://10.42.0.1:8080/File/00000000.swf"}, - {"Uri":"http://10.42.0.1:8080/File/00015000.swf"}, - {"Uri":"http://10.42.0.1:8080/File/00030000.swf"} + {"Uri":"http://10.42.0.1:8080/Files/00000000.swf"}, + {"Uri":"http://10.42.0.1:8080/Files/00015000.swf"}, + {"Uri":"http://10.42.0.1:8080/Files/00030000.swf"} ] }, { @@ -88,3 +88,9 @@ class FileCollection(): resp.body= f.read() except FileNotFoundError: raise HTTPNotFound() + def on_post(self, req, resp): + f = req.get_param("file") + with open(self.path/f.filename, "wb") as fo: + fo.write(f.file.read()) + resp.status = HTTP_CREATED + resp.content_location = f"/Files/{f.filename}"