Refactoring

This commit is contained in:
2022-05-01 21:14:35 +03:00
parent d625d7926b
commit 124ad5e533

View File

@@ -37,6 +37,7 @@ async def convert(
temp_filename = str(temp_uuid) + ".fb2"
converted_temp_filename = str(temp_uuid) + "." + format_lower
try:
async with aiofiles.open(temp_filename, "wb") as f:
while content := await file.read(1024):
if isinstance(content, str):
@@ -55,7 +56,7 @@ async def convert(
)
_, stderr = await proc.communicate()
finally:
await aiofiles.os.remove(temp_filename)
if proc.returncode != 0 or len(stderr) != 0:
@@ -69,10 +70,11 @@ async def convert(
)
async def result_iterator() -> AsyncIterator[bytes]:
try:
async with aiofiles.open(converted_temp_filename, "rb") as f:
while data := await f.read(2048):
yield data
finally:
await aiofiles.os.remove(converted_temp_filename)
return StreamingResponse(result_iterator())