[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot] 2021-10-27 02:52:56 +00:00
parent 9196210e89
commit 06f5e7094f
2 changed files with 11 additions and 9 deletions

View file

@ -24,8 +24,8 @@ from .helpers import get_flashed_messages as get_flashed_messages
from .helpers import get_template_attribute as get_template_attribute from .helpers import get_template_attribute as get_template_attribute
from .helpers import make_response as make_response from .helpers import make_response as make_response
from .helpers import safe_join as safe_join from .helpers import safe_join as safe_join
from .helpers import send_file as send_file
from .helpers import send_chunked_file as send_chunked_file from .helpers import send_chunked_file as send_chunked_file
from .helpers import send_file as send_file
from .helpers import send_from_directory as send_from_directory from .helpers import send_from_directory as send_from_directory
from .helpers import stream_with_context as stream_with_context from .helpers import stream_with_context as stream_with_context
from .helpers import url_for as url_for from .helpers import url_for as url_for

View file

@ -626,6 +626,7 @@ def send_file(
) )
) )
def send_chunked_file(filepath: str, chunk_size: int = 8192): def send_chunked_file(filepath: str, chunk_size: int = 8192):
"""If you serve binary files, """If you serve binary files,
you should not iterate through lines since you should not iterate through lines since
@ -638,26 +639,27 @@ def send_chunked_file(filepath:str , chunk_size: int = 8192):
provided, error occured. provided, error occured.
:param chunk_size: defult size is 8192 :param chunk_size: defult size is 8192
""" """
name_splited = filepath.split('/') name_splited = filepath.split("/")
filename = name_splited[len(name_splited) - 1] filename = name_splited[len(name_splited) - 1]
def read_file_chunks(path, chunk_size): def read_file_chunks(path, chunk_size):
with open(path, 'rb') as fd: with open(path, "rb") as fd:
while 1: while 1:
buf = fd.read(chunk_size) buf = fd.read(chunk_size)
if buf: if buf:
yield buf yield buf
else: else:
break break
if os.path.isfile(filepath): if os.path.isfile(filepath):
return current_app.response_class( return current_app.response_class(
stream_with_context(read_file_chunks(filepath, chunk_size)), stream_with_context(read_file_chunks(filepath, chunk_size)),
headers={ headers={"Content-Disposition": f"attachment; filename={filename}"},
'Content-Disposition': f'attachment; filename={filename}'
}
) )
else: else:
raise NotFound() raise NotFound()
def safe_join(directory: str, *pathnames: str) -> str: def safe_join(directory: str, *pathnames: str) -> str:
"""Safely join zero or more untrusted path components to a base """Safely join zero or more untrusted path components to a base
directory to avoid escaping the base directory. directory to avoid escaping the base directory.