Skip to content

Commit

Permalink
Update tab_upload.py
Browse files Browse the repository at this point in the history
This change resolves /issues/54

The code in `tab_upload.py` reads uploaded files 1024 bytes at a time. This is not efficient for large files on modern systems. 

`UploadFile.read()` maps to `RawIOBase.read()` which handles multiple reads to a stream internally. 

This change delegates stream io to the lower levels of the runtime.
  • Loading branch information
gardner authored Sep 5, 2023
1 parent f99cb3f commit 3a33c19
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions self_hosting_machinery/webgui/tab_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,8 @@ async def _tab_files_upload(self, file: UploadFile):
return JSONResponse(content=response_data, status_code=409)
try:
with open(tmp_path, "wb") as f:
while True:
contents = await file.read(1024)
if not contents:
break
f.write(contents)
contents = await file.read()
f.write(contents)
os.rename(tmp_path, file_path)
except OSError as e:
log("Error while uploading file: %s" % (e or str(type(e))))
Expand Down

0 comments on commit 3a33c19

Please sign in to comment.