From: Michael Tremer Date: Fri, 3 Nov 2017 16:35:25 +0000 (+0000) Subject: hub: Add new handler for streaming uploads X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cc88e58ba392a3909fdfc398ca6cf7787f626f2d;p=pbs.git hub: Add new handler for streaming uploads Signed-off-by: Michael Tremer --- diff --git a/src/database.sql b/src/database.sql index ae02c687..7a09c891 100644 --- a/src/database.sql +++ b/src/database.sql @@ -1385,7 +1385,7 @@ CREATE TABLE uploads ( user_id integer, builder_id integer, filename text NOT NULL, - hash text NOT NULL, + hash text, size bigint NOT NULL, progress bigint DEFAULT 0 NOT NULL, finished boolean DEFAULT false NOT NULL, diff --git a/src/hub/__init__.py b/src/hub/__init__.py index e7b7391d..20bef7ff 100644 --- a/src/hub/__init__.py +++ b/src/hub/__init__.py @@ -55,6 +55,7 @@ class Application(tornado.web.Application): # Uploads (r"/uploads/create", handlers.UploadsCreateHandler), + (r"/uploads/stream", handlers.UploadsStreamHandler), (r"/uploads/(.*)/sendchunk", handlers.UploadsSendChunkHandler), (r"/uploads/(.*)/finished", handlers.UploadsFinishedHandler), (r"/uploads/(.*)/destroy", handlers.UploadsDestroyHandler), @@ -80,7 +81,8 @@ class Application(tornado.web.Application): def run(self, port=81): logging.debug("Going to background") - http_server = tornado.httpserver.HTTPServer(self, xheaders=True) + http_server = tornado.httpserver.HTTPServer(self, xheaders=True, + max_body_size=1 * (1024 ** 3)) # If we are not running in debug mode, we can actually run multiple # frontends to get best performance out of our service. diff --git a/src/hub/handlers.py b/src/hub/handlers.py index a4bd8164..aa4802c2 100644 --- a/src/hub/handlers.py +++ b/src/hub/handlers.py @@ -153,7 +153,7 @@ class UploadsCreateHandler(BaseHandler): filename = self.get_argument("filename") filesize = self.get_argument_int("filesize") - filehash = self.get_argument("hash") + filehash = self.get_argument("hash", None) with self.db.transaction(): upload = self.backend.uploads.create(filename, filesize, @@ -162,6 +162,37 @@ class UploadsCreateHandler(BaseHandler): self.finish(upload.uuid) +@tornado.web.stream_request_body +class UploadsStreamHandler(BaseHandler): + @tornado.web.authenticated + def prepare(self): + # Received file size + self.size = 0 + + upload_uuid = self.get_argument("id") + + # Fetch upload object from database + self.upload = self.backend.uploads.get_by_uuid(upload_uuid) + if not self.upload: + raise tornado.web.HTTPError(404) + + def data_received(self, data): + logging.debug("Received chunk of %s bytes" % len(data)) + self.size += len(data) + + # Write the received chunk to disk + with self.db.transaction(): + self.upload.append(data) + + def put(self): + logging.info("Received entire file (%s bytes)" % self.size) + + with self.db.transaction(): + self.upload.finished() + + self.finish("OK") + + class UploadsSendChunkHandler(BaseHandler): @tornado.web.authenticated def post(self, upload_id):