Skip to content

Commit

Permalink
Moves a payload file upload function at class level
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaptperez committed Jun 8, 2024
1 parent e7ac139 commit d8b03fc
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions app/api/v2/handlers/payload_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,8 @@ async def post_payloads(self, request: web.Request):

# The file_field.file is of type IOBase: It uses blocking methods.
# Putting blocking code into a dedicated method and thread...
def save_file(target_file_path: str, io_base_src: IOBase):
size: int = 0
read_chunk: bool = True
with open(target_file_path, 'wb') as buffered_io_base_dest:
while read_chunk:
chunk: bytes = io_base_src.read(8192)
if chunk:
size += len(chunk)
buffered_io_base_dest.write(chunk)
else:
read_chunk = False

loop: asyncio.AbstractEventLoop = asyncio.get_event_loop()
await loop.run_in_executor(None, save_file, file_path, file_field.file)
await loop.run_in_executor(None, self.__save_file, file_path, file_field.file)

body: dict[list[str]] = {"payloads": [file_name]}
return web.json_response(body)
Expand Down Expand Up @@ -134,3 +122,23 @@ async def __generate_file_name_and_path(cls, file_field: web.FileField) -> [str,
suffix += 1
file_name: str = file_name_candidate
return file_name, file_path

@staticmethod
def __save_file(target_file_path: str, io_base_src: IOBase):
"""
Save an uploaded file content into a targeted file path.
Note this method calls blocking methods and must be run into a dedicated thread.
:param target_file_path: The destination path to write to.
:param io_base_src: The stream with file content to read from.
"""
size: int = 0
read_chunk: bool = True
with open(target_file_path, 'wb') as buffered_io_base_dest:
while read_chunk:
chunk: bytes = io_base_src.read(8192)
if chunk:
size += len(chunk)
buffered_io_base_dest.write(chunk)
else:
read_chunk = False

0 comments on commit d8b03fc

Please sign in to comment.