-
Notifications
You must be signed in to change notification settings - Fork 0
Better async file handling #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| __version__ = '0.0.2' | ||
| __version__ = '0.0.3' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| import logging | ||
| import mimetypes | ||
| import os | ||
|
|
||
| import aiofiles | ||
| from asgiref.sync import iscoroutinefunction | ||
| from django.conf import settings | ||
| from django.http import FileResponse | ||
| from django.http import FileResponse, StreamingHttpResponse | ||
| from django.utils.decorators import sync_and_async_middleware | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
@@ -38,7 +40,24 @@ def static_admin_middleware(get_response): | |
|
|
||
| async def middleware(request): | ||
| if files.get(request.path): | ||
| return FileResponse(open(files[request.path], 'rb')) | ||
| file_path = files[request.path] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not for now but ASGI has two relevant extensions which work a bit like X-SEND-FILE. One allows you to hand over a fd and the server will get the os to write the data, without python getting involved (Zero Copy Send). Even better you can return a path and have the server stream it for you (Path Send). These are both likely to be much faster.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, good to know, will have to have a look at some point, although not looking to complicate this library too much given it's target usage. |
||
|
|
||
| async def file_iterator(): | ||
| async with aiofiles.open(file_path, 'rb') as f: | ||
| while chunk := await f.read(8192): | ||
| yield chunk | ||
|
|
||
| content_type, _ = mimetypes.guess_type(file_path) | ||
| if content_type is None: | ||
| content_type = 'application/octet-stream' | ||
|
|
||
| response = StreamingHttpResponse( | ||
| file_iterator(), content_type=content_type | ||
| ) | ||
|
|
||
| filename = os.path.basename(file_path) | ||
| response['Content-Disposition'] = f'inline; filename="{filename}"' | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thought [non-blocking]:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Went for least surprise here, since the static filenames aren't hashed in any way (as far as I know) any implementation with caching could cause weirdness. If caching of these files is desired then not using this package and deploying as per Django recommendations on a CDN seems the approach to follow. |
||
| return response | ||
|
|
||
| response = await get_response(request) | ||
| return response | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| pytest>=8.0.0,<9.0.0 | ||
| pytest-cov>=3.0.0,<5.0.0 | ||
| pytest-mock>=3.13.0,<4.0.0 | ||
| aiofiles | ||
| django>=3.2.0,<6.0.0 | ||
| setuptools |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thought [non-blocking]:
Is it trivial to consider
uvhere?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll likely take a look at the build/packaging process at some point in the future, but not now, this approach is definitely more messy to use