Fix blocking I/O in xmpp notify to read uploaded files (#120801)

detected by ruff in https://github.com/home-assistant/core/pull/120799
This commit is contained in:
J. Nick Koston 2024-06-29 00:40:35 -05:00 committed by GitHub
parent 04ab74589a
commit d4ecbc91c3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -305,16 +305,20 @@ async def async_send_message( # noqa: C901
timeout=timeout,
)
async def upload_file_from_path(self, path, timeout=None):
def _read_upload_file(self, path: str) -> bytes:
"""Read file from path."""
with open(path, "rb") as upfile:
_LOGGER.debug("Reading file %s", path)
return upfile.read()
async def upload_file_from_path(self, path: str, timeout=None):
"""Upload a file from a local file path via XEP_0363."""
_LOGGER.info("Uploading file from path, %s", path)
if not hass.config.is_allowed_path(path):
raise PermissionError("Could not access file. Path not allowed")
with open(path, "rb") as upfile:
_LOGGER.debug("Reading file %s", path)
input_file = upfile.read()
input_file = await hass.async_add_executor_job(self._read_upload_file, path)
filesize = len(input_file)
_LOGGER.debug("Filesize is %s bytes", filesize)