mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 10:47:10 +00:00
Use HassKey in file_upload (#137294)
This commit is contained in:
parent
0f57347797
commit
5e0312ca60
@ -21,9 +21,11 @@ from homeassistant.core import Event, HomeAssistant, callback
|
|||||||
from homeassistant.helpers import config_validation as cv
|
from homeassistant.helpers import config_validation as cv
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
from homeassistant.util import raise_if_invalid_filename
|
from homeassistant.util import raise_if_invalid_filename
|
||||||
|
from homeassistant.util.hass_dict import HassKey
|
||||||
from homeassistant.util.ulid import ulid_hex
|
from homeassistant.util.ulid import ulid_hex
|
||||||
|
|
||||||
DOMAIN = "file_upload"
|
DOMAIN = "file_upload"
|
||||||
|
_DATA: HassKey[FileUploadData] = HassKey(DOMAIN)
|
||||||
|
|
||||||
ONE_MEGABYTE = 1024 * 1024
|
ONE_MEGABYTE = 1024 * 1024
|
||||||
MAX_SIZE = 100 * ONE_MEGABYTE
|
MAX_SIZE = 100 * ONE_MEGABYTE
|
||||||
@ -41,7 +43,7 @@ def process_uploaded_file(hass: HomeAssistant, file_id: str) -> Iterator[Path]:
|
|||||||
if DOMAIN not in hass.data:
|
if DOMAIN not in hass.data:
|
||||||
raise ValueError("File does not exist")
|
raise ValueError("File does not exist")
|
||||||
|
|
||||||
file_upload_data: FileUploadData = hass.data[DOMAIN]
|
file_upload_data = hass.data[_DATA]
|
||||||
|
|
||||||
if not file_upload_data.has_file(file_id):
|
if not file_upload_data.has_file(file_id):
|
||||||
raise ValueError("File does not exist")
|
raise ValueError("File does not exist")
|
||||||
@ -149,10 +151,10 @@ class FileUploadView(HomeAssistantView):
|
|||||||
hass = request.app[KEY_HASS]
|
hass = request.app[KEY_HASS]
|
||||||
file_id = ulid_hex()
|
file_id = ulid_hex()
|
||||||
|
|
||||||
if DOMAIN not in hass.data:
|
if _DATA not in hass.data:
|
||||||
hass.data[DOMAIN] = await FileUploadData.create(hass)
|
hass.data[_DATA] = await FileUploadData.create(hass)
|
||||||
|
|
||||||
file_upload_data: FileUploadData = hass.data[DOMAIN]
|
file_upload_data = hass.data[_DATA]
|
||||||
file_dir = file_upload_data.file_dir(file_id)
|
file_dir = file_upload_data.file_dir(file_id)
|
||||||
queue: SimpleQueue[tuple[bytes, asyncio.Future[None] | None] | None] = (
|
queue: SimpleQueue[tuple[bytes, asyncio.Future[None] | None] | None] = (
|
||||||
SimpleQueue()
|
SimpleQueue()
|
||||||
@ -206,7 +208,7 @@ class FileUploadView(HomeAssistantView):
|
|||||||
raise web.HTTPNotFound
|
raise web.HTTPNotFound
|
||||||
|
|
||||||
file_id = data["file_id"]
|
file_id = data["file_id"]
|
||||||
file_upload_data: FileUploadData = hass.data[DOMAIN]
|
file_upload_data = hass.data[_DATA]
|
||||||
|
|
||||||
if file_upload_data.files.pop(file_id, None) is None:
|
if file_upload_data.files.pop(file_id, None) is None:
|
||||||
raise web.HTTPNotFound
|
raise web.HTTPNotFound
|
||||||
|
Loading…
x
Reference in New Issue
Block a user