Voice et bot modif
This commit is contained in:
parent
189d56026b
commit
7333a22bcd
10774 changed files with 634644 additions and 933308 deletions
|
|
@ -26,7 +26,7 @@ from typing import ( # noqa
|
|||
|
||||
from . import hdrs
|
||||
from .abc import AbstractStreamWriter
|
||||
from .helpers import ETAG_ANY, ETag, must_be_empty_body
|
||||
from .helpers import DEFAULT_CHUNK_SIZE, ETAG_ANY, ETag, must_be_empty_body
|
||||
from .typedefs import LooseHeaders, PathLike
|
||||
from .web_exceptions import (
|
||||
HTTPForbidden,
|
||||
|
|
@ -86,7 +86,7 @@ for content_type, extension in ADDITIONAL_CONTENT_TYPES.items():
|
|||
CONTENT_TYPES.add_type(content_type, extension)
|
||||
|
||||
|
||||
_CLOSE_FUTURES: Set[asyncio.Future[None]] = set()
|
||||
_CLOSE_FUTURES: set[asyncio.Future[None]] = set()
|
||||
|
||||
|
||||
class FileResponse(StreamResponse):
|
||||
|
|
@ -95,10 +95,10 @@ class FileResponse(StreamResponse):
|
|||
def __init__(
|
||||
self,
|
||||
path: PathLike,
|
||||
chunk_size: int = 256 * 1024,
|
||||
chunk_size: int = DEFAULT_CHUNK_SIZE,
|
||||
status: int = 200,
|
||||
reason: Optional[str] = None,
|
||||
headers: Optional[LooseHeaders] = None,
|
||||
reason: str | None = None,
|
||||
headers: LooseHeaders | None = None,
|
||||
) -> None:
|
||||
super().__init__(status=status, reason=reason, headers=headers)
|
||||
|
||||
|
|
@ -118,11 +118,11 @@ class FileResponse(StreamResponse):
|
|||
chunk_size = self._chunk_size
|
||||
loop = asyncio.get_event_loop()
|
||||
chunk = await loop.run_in_executor(
|
||||
None, self._seek_and_read, fobj, offset, chunk_size
|
||||
None, self._seek_and_read, fobj, offset, min(chunk_size, count)
|
||||
)
|
||||
while chunk:
|
||||
await writer.write(chunk)
|
||||
count = count - chunk_size
|
||||
count = count - len(chunk)
|
||||
if count <= 0:
|
||||
break
|
||||
chunk = await loop.run_in_executor(None, fobj.read, min(chunk_size, count))
|
||||
|
|
@ -141,7 +141,8 @@ class FileResponse(StreamResponse):
|
|||
|
||||
loop = request._loop
|
||||
transport = request.transport
|
||||
assert transport is not None
|
||||
if transport is None:
|
||||
raise ConnectionResetError("Connection lost")
|
||||
|
||||
try:
|
||||
await loop.sendfile(transport, fobj, offset, count)
|
||||
|
|
@ -152,7 +153,7 @@ class FileResponse(StreamResponse):
|
|||
return writer
|
||||
|
||||
@staticmethod
|
||||
def _etag_match(etag_value: str, etags: Tuple[ETag, ...], *, weak: bool) -> bool:
|
||||
def _etag_match(etag_value: str, etags: tuple[ETag, ...], *, weak: bool) -> bool:
|
||||
if len(etags) == 1 and etags[0].value == ETAG_ANY:
|
||||
return True
|
||||
return any(
|
||||
|
|
@ -161,7 +162,7 @@ class FileResponse(StreamResponse):
|
|||
|
||||
async def _not_modified(
|
||||
self, request: "BaseRequest", etag_value: str, last_modified: float
|
||||
) -> Optional[AbstractStreamWriter]:
|
||||
) -> AbstractStreamWriter | None:
|
||||
self.set_status(HTTPNotModified.status_code)
|
||||
self._length_check = False
|
||||
self.etag = etag_value
|
||||
|
|
@ -172,15 +173,15 @@ class FileResponse(StreamResponse):
|
|||
|
||||
async def _precondition_failed(
|
||||
self, request: "BaseRequest"
|
||||
) -> Optional[AbstractStreamWriter]:
|
||||
) -> AbstractStreamWriter | None:
|
||||
self.set_status(HTTPPreconditionFailed.status_code)
|
||||
self.content_length = 0
|
||||
return await super().prepare(request)
|
||||
|
||||
def _make_response(
|
||||
self, request: "BaseRequest", accept_encoding: str
|
||||
) -> Tuple[
|
||||
_FileResponseResult, Optional[io.BufferedReader], os.stat_result, Optional[str]
|
||||
) -> tuple[
|
||||
_FileResponseResult, io.BufferedReader | None, os.stat_result, str | None
|
||||
]:
|
||||
"""Return the response result, io object, stat result, and encoding.
|
||||
|
||||
|
|
@ -235,7 +236,7 @@ class FileResponse(StreamResponse):
|
|||
|
||||
def _get_file_path_stat_encoding(
|
||||
self, accept_encoding: str
|
||||
) -> Tuple[Optional[pathlib.Path], os.stat_result, Optional[str]]:
|
||||
) -> tuple[pathlib.Path | None, os.stat_result, str | None]:
|
||||
file_path = self._path
|
||||
for file_extension, file_encoding in ENCODING_EXTENSIONS.items():
|
||||
if file_encoding not in accept_encoding:
|
||||
|
|
@ -252,7 +253,7 @@ class FileResponse(StreamResponse):
|
|||
st = file_path.stat()
|
||||
return file_path if S_ISREG(st.st_mode) else None, st, None
|
||||
|
||||
async def prepare(self, request: "BaseRequest") -> Optional[AbstractStreamWriter]:
|
||||
async def prepare(self, request: "BaseRequest") -> AbstractStreamWriter | None:
|
||||
loop = asyncio.get_running_loop()
|
||||
# Encoding comparisons should be case-insensitive
|
||||
# https://www.rfc-editor.org/rfc/rfc9110#section-8.4.1
|
||||
|
|
@ -302,13 +303,13 @@ class FileResponse(StreamResponse):
|
|||
request: "BaseRequest",
|
||||
fobj: io.BufferedReader,
|
||||
st: os.stat_result,
|
||||
file_encoding: Optional[str],
|
||||
) -> Optional[AbstractStreamWriter]:
|
||||
file_encoding: str | None,
|
||||
) -> AbstractStreamWriter | None:
|
||||
status = self._status
|
||||
file_size: int = st.st_size
|
||||
file_mtime: float = st.st_mtime
|
||||
count: int = file_size
|
||||
start: Optional[int] = None
|
||||
start: int | None = None
|
||||
|
||||
if (ifrange := request.if_range) is None or file_mtime <= ifrange.timestamp():
|
||||
# If-Range header check:
|
||||
|
|
@ -321,7 +322,7 @@ class FileResponse(StreamResponse):
|
|||
try:
|
||||
rng = request.http_range
|
||||
start = rng.start
|
||||
end: Optional[int] = rng.stop
|
||||
end: int | None = rng.stop
|
||||
except ValueError:
|
||||
# https://tools.ietf.org/html/rfc7233:
|
||||
# A server generating a 416 (Range Not Satisfiable) response to
|
||||
|
|
@ -404,8 +405,8 @@ class FileResponse(StreamResponse):
|
|||
if status == HTTPPartialContent.status_code:
|
||||
real_start = start
|
||||
assert real_start is not None
|
||||
self._headers[hdrs.CONTENT_RANGE] = "bytes {}-{}/{}".format(
|
||||
real_start, real_start + count - 1, file_size
|
||||
self._headers[hdrs.CONTENT_RANGE] = (
|
||||
f"bytes {real_start}-{real_start + count - 1}/{file_size}"
|
||||
)
|
||||
|
||||
# If we are sending 0 bytes calling sendfile() will throw a ValueError
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue