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

Support FileResponse.prepare() to be re-entrant just as StreamResponse.prepare(). #4647

Open
momocow opened this issue Mar 19, 2020 · 2 comments

Comments

@momocow
Copy link

momocow commented Mar 19, 2020

🐣 Is your feature request related to a problem? Please describe.

There is no way to know when a file is sent if the file is required to be removed after consumed.

As the docs of FileResponse reads,

The actual body sending happens in overridden prepare().

I tried to call await file_resp.prepare() myself in a handler function just like what we are used to do with StreamResponse, and then the file is removed after leaving the handler.

An Exception was raised after leaving the handler since prepare() was called again here without checking if the response had already been prepared.

await prepare_meth(request)

Although I can use a StreamResponse instead of a FileResponse to tackle this problem, doing the same thing as what FileResponse._sendfile_fallback() does. I think it's the responsibility of a FileResponse to send a file (though a file to be removed soon), also to avoid code duplication.

💡 Describe the solution you'd like

Just like what StreamResponse.prepare() does, I think the same check should also be implemented in FileResponse.prepare().

if self._eof_sent:
return None
if self._payload_writer is not None:
return self._payload_writer

Describe alternatives you've considered

Provide any way for developers to know a file is sent, maybe an asyncio.Event or a signal.


I know there are tons of issues here, but I think it's an improvement for the library to be more comprehensive.
Thank you!

@webknjaz
Copy link
Member

You can try sending a PR.

@pohmelie
Copy link

Voting up. No way to send temporary file, which created and removed by handler (archive, for example).

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

No branches or pull requests

3 participants