Skip to content
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

FileResponse: accept BufferedReader #10465

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Derkades
Copy link
Contributor

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.

This is a proof of concept, I am not sure if this is the best way to solve this problem.

Are there changes in behavior for the user?

No breaking changes.

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.

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

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
    • The format is <Name> <Surname>.
    • Please keep alphabetical order, the file is sorted by names.
  • Add a new news fragment into the CHANGES/ folder
    • name 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 an
        improper 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 breaking
        changes 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 build
        process.
      • .packaging: Notes for downstreams about unobvious side effects
        and 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 above
        categories.
    • Make sure to use full sentences with correct case and punctuation,
      for example:

      Fixed issue with non-ascii contents in doctest text files
      -- by :user:`contributor-gh-handle`.

      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.

Copy link

codspeed-hq bot commented Feb 16, 2025

CodSpeed Performance Report

Merging #10465 will not alter performance

Comparing Derkades:fileresponse-bufferedreader (c7de271) with master (e29d989)

Summary

✅ 46 untouched benchmarks

Comment on lines +404 to +406
# content-type cannot be determined if self._io is used
# instead of self._path
self.content_type = FALLBACK_CONTENT_TYPE
Copy link
Member

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)

Copy link
Member

@Dreamsorcerer Dreamsorcerer left a 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?).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants