mirror of
https://github.com/esphome/esphome.git
synced 2024-11-26 00:48:19 +01:00
web_server.py: return empty content when file doesn't exist (#5980)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
parent
059e4cee58
commit
70fdc3c3f8
1 changed files with 14 additions and 5 deletions
|
@ -792,13 +792,22 @@ class EditRequestHandler(BaseHandler):
|
||||||
"""Get the content of a file."""
|
"""Get the content of a file."""
|
||||||
loop = asyncio.get_running_loop()
|
loop = asyncio.get_running_loop()
|
||||||
filename = settings.rel_path(configuration)
|
filename = settings.rel_path(configuration)
|
||||||
content = await loop.run_in_executor(None, self._read_file, filename)
|
content = await loop.run_in_executor(
|
||||||
|
None, self._read_file, filename, configuration
|
||||||
|
)
|
||||||
|
if content is not None:
|
||||||
self.write(content)
|
self.write(content)
|
||||||
|
|
||||||
def _read_file(self, filename: str) -> bytes:
|
def _read_file(self, filename: str, configuration: str) -> bytes | None:
|
||||||
"""Read a file and return the content as bytes."""
|
"""Read a file and return the content as bytes."""
|
||||||
|
try:
|
||||||
with open(file=filename, encoding="utf-8") as f:
|
with open(file=filename, encoding="utf-8") as f:
|
||||||
return f.read()
|
return f.read()
|
||||||
|
except FileNotFoundError:
|
||||||
|
if configuration in const.SECRETS_FILES:
|
||||||
|
return ""
|
||||||
|
self.set_status(404)
|
||||||
|
return None
|
||||||
|
|
||||||
def _write_file(self, filename: str, content: bytes) -> None:
|
def _write_file(self, filename: str, content: bytes) -> None:
|
||||||
"""Write a file with the given content."""
|
"""Write a file with the given content."""
|
||||||
|
|
Loading…
Reference in a new issue