-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New IOResponse class for serving a response using any IO object #10465
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: master
Are you sure you want to change the base?
Conversation
CodSpeed Performance ReportMerging #10465 will degrade performances by 18.77%Comparing Summary
Benchmarks breakdown
|
aiohttp/web_fileresponse.py
Outdated
# content-type cannot be determined if self._io is used | ||
# instead of self._path | ||
self.content_type = FALLBACK_CONTENT_TYPE |
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.
Unless I'm missing something, it seems like FileResponse is missing Content-Disposition by default. If we were to add this in and recommend the user to specify a filename for the download, then we could use that filename here (or just use it if the header has been explicitly passed):
>>> mimetypes.guess_type("foo.html")
('text/html', None)
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 think this looks reasonable. I suspect a subclass may be cleaner though (FileIOResponse maybe?).
c7de271
to
c16bc35
Compare
Now rewritten with subclasses. It is much cleaner. I still need to bring back Accept-Encoding support. |
Looks promising. We'll need to update the docs too, and make sure we have a warning highlighting that the handler should not close the file object (including using a context manager). |
for more information, see https://pre-commit.ci
I am having difficulty solving test failures with this error: It happens when setting |
CodSpeed reports a small regression, I think this is because the current implementation always opens the file even if it is unchanged. I can probably fix this. |
As far as I can see, last_modified comes from st_mtime, so we'll need to set it on the mock. e.g. Add a line after this one (presumably set to that value divided by 1000000 or whatever the appropriate order of magnitude is): |
With st_mtime added to the mock, the tests indeed pass. I am not sure why they didn't fail before, st_mtime was used previously.
|
What do these changes do?
FileResponse now accepts an opened file instead of only a file path. This allows sending a temporary file to the client by opening the file, deleting it, and then passing the open file to FileResponse. FileResponse handles closing the file when it is no longer used.
Are there changes in behavior for the user?
FileResponse is now a subclass of BaseIOResponse, I don't expect any compatibility issues for normal API usage
Is it a substantial burden for the maintainers to support this?
The FileResponse class is now more complicated. I am still thinking about if it makes sense to split FileResponse into two classes, one for handling IO and one for handling paths.Now split into subclasses, I think it makes the code clearer than it was before.It is a bit unusual for an API to take an open file and close it by itself. But it solves a problem. Other suggestions are welcome.
Related issue number
#10360
Checklist
CONTRIBUTORS.txt
CHANGES/
foldername it
<issue_or_pr_num>.<type>.rst
(e.g.588.bugfix.rst
)if you don't have an issue number, change it to the pull request
number after creating the PR
.bugfix
: A bug fix for something the maintainers deemed animproper undesired behavior that got corrected to match
pre-agreed expectations.
.feature
: A new behavior, public APIs. That sort of stuff..deprecation
: A declaration of future API removals and breakingchanges in behavior.
.breaking
: When something public is removed in a breaking way.Could be deprecated in an earlier release.
.doc
: Notable updates to the documentation structure or buildprocess.
.packaging
: Notes for downstreams about unobvious side effectsand tooling. Changes in the test invocation considerations and
runtime assumptions.
.contrib
: Stuff that affects the contributor experience. e.g.Running tests, building the docs, setting up the development
environment.
.misc
: Changes that are hard to assign to any of the abovecategories.
Make sure to use full sentences with correct case and punctuation,
for example:
Use the past tense or the present tense a non-imperative mood,
referring to what's changed compared to the last released version
of this project.