From b36c5f25d286571dc4fdead50c625afccb784c89 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 10 Jun 2015 14:20:06 +0200 Subject: [PATCH] Improve memory consumption of file upload with the HTML form Fix #86 --- gns3server/handlers/upload_handler.py | 7 +++++-- tests/handlers/test_upload.py | 6 ++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/gns3server/handlers/upload_handler.py b/gns3server/handlers/upload_handler.py index 9a431276..2ca39658 100644 --- a/gns3server/handlers/upload_handler.py +++ b/gns3server/handlers/upload_handler.py @@ -71,8 +71,11 @@ class UploadHandler: try: os.makedirs(destination_dir, exist_ok=True) with open(destination_path, "wb+") as f: - chunk = data["file"].file.read() - f.write(chunk) + while True: + chunk = data["file"].file.read(512) + if not chunk: + break + f.write(chunk) st = os.stat(destination_path) os.chmod(destination_path, st.st_mode | stat.S_IXUSR) except OSError as e: diff --git a/tests/handlers/test_upload.py b/tests/handlers/test_upload.py index 63d60ef8..0c1c2b1e 100644 --- a/tests/handlers/test_upload.py +++ b/tests/handlers/test_upload.py @@ -31,8 +31,10 @@ def test_index_upload(server): def test_upload(server, tmpdir): + content = ''.join(['a' for _ in range(0, 1025)]) + with open(str(tmpdir / "test"), "w+") as f: - f.write("TEST") + f.write(content) body = aiohttp.FormData() body.add_field("type", "QEMU") body.add_field("file", open(str(tmpdir / "test"), "rb"), content_type="application/iou", filename="test2") @@ -41,6 +43,6 @@ def test_upload(server, tmpdir): response = server.post('/upload', api_version=None, body=body, raw=True) with open(str(tmpdir / "QEMU" / "test2")) as f: - assert f.read() == "TEST" + assert f.read() == content assert "test2" in response.body.decode("utf-8")