mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-12-23 11:58:49 +00:00
Wrap I/O and run in executor
This commit is contained in:
@@ -180,29 +180,39 @@ class APISnapshots(CoreSysAttributes):
|
|||||||
@api_process
|
@api_process
|
||||||
async def upload(self, request):
|
async def upload(self, request):
|
||||||
"""Upload a snapshot file."""
|
"""Upload a snapshot file."""
|
||||||
with TemporaryDirectory(dir=str(self.sys_config.path_tmp)) as temp_dir:
|
chunks = []
|
||||||
tar_file = Path(temp_dir, "snapshot.tar")
|
temp_dir = TemporaryDirectory(dir=str(self.sys_config.path_tmp))
|
||||||
reader = await request.multipart()
|
|
||||||
contents = await reader.next()
|
|
||||||
try:
|
|
||||||
with tar_file.open("wb") as snapshot:
|
|
||||||
while True:
|
|
||||||
chunk = await contents.read_chunk()
|
|
||||||
if not chunk:
|
|
||||||
break
|
|
||||||
snapshot.write(chunk)
|
|
||||||
|
|
||||||
except OSError as err:
|
def _write_chunks():
|
||||||
_LOGGER.error("Can't write new snapshot file: %s", err)
|
"""Write chunks to tempfile and return the filepath."""
|
||||||
return False
|
tar_file = Path(temp_dir.name, "snapshot.tar")
|
||||||
|
with tar_file.open("wb") as snapshot:
|
||||||
|
for chunk in chunks:
|
||||||
|
snapshot.write(chunk)
|
||||||
|
return tar_file
|
||||||
|
|
||||||
except asyncio.CancelledError:
|
reader = await request.multipart()
|
||||||
return False
|
contents = await reader.next()
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
chunk = await contents.read_chunk()
|
||||||
|
if not chunk:
|
||||||
|
break
|
||||||
|
chunks.append(chunk)
|
||||||
|
|
||||||
snapshot = await asyncio.shield(
|
except OSError as err:
|
||||||
self.sys_snapshots.import_snapshot(tar_file)
|
_LOGGER.error("Can't write new snapshot file: %s", err)
|
||||||
)
|
|
||||||
|
|
||||||
if snapshot:
|
|
||||||
return {ATTR_SLUG: snapshot.slug}
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
return False
|
||||||
|
|
||||||
|
tar_file = await self.sys_run_in_executor(_write_chunks)
|
||||||
|
snapshot = await asyncio.shield(self.sys_snapshots.import_snapshot(tar_file))
|
||||||
|
|
||||||
|
# Cleanup tmpdir
|
||||||
|
await self.sys_run_in_executor(temp_dir.cleanup)
|
||||||
|
|
||||||
|
if snapshot:
|
||||||
|
return {ATTR_SLUG: snapshot.slug}
|
||||||
|
return False
|
||||||
|
|||||||
Reference in New Issue
Block a user