Skip to content

Commit f2ffcef

Browse files
committed
Use chunked read for uploads
1 parent 51ef2df commit f2ffcef

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

gramps_webapi/api/resources/import_media.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ def post(self) -> Response:
4747
file_path = os.path.join(export_path, file_name)
4848

4949
with open(file_path, "w+b") as ftmp:
50-
ftmp.write(request_stream.read())
50+
chunk_size = 4 * 1024 # reading in 4 KB chunks
51+
while True:
52+
chunk = request_stream.read(chunk_size)
53+
if not chunk:
54+
break
55+
ftmp.write(chunk)
5156

5257
if os.path.getsize(file_path) == 0:
5358
abort_with_message(400, "Imported file is empty")

gramps_webapi/api/resources/importers.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,13 @@ def post(self, args: Dict, extension: str) -> Response:
8080
file_name = f"{uuid.uuid4()}.{extension}"
8181
file_path = os.path.join(export_path, file_name)
8282
with open(file_path, "w+b") as ftmp:
83-
ftmp.write(request_stream.read())
83+
chunk_size = 4 * 1024 # reading in 4 KB chunks
84+
while True:
85+
chunk = request_stream.read(chunk_size)
86+
if not chunk:
87+
break
88+
ftmp.write(chunk)
89+
8490
if os.path.getsize(file_path) == 0:
8591
abort_with_message(400, "Imported file is empty")
8692
importers = get_importers(extension.lower())

0 commit comments

Comments
 (0)