-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Fix file upload not chunked #327
Conversation
The upload of a single file is not chunked anymore as the content-length can be determined, except if the chunked argument is passed forced as True. This fixes #126 for uploading a single file, but does not apply to multipart uploads.
@kxepal could you review this PR? |
self.chunked = True | ||
if not self.chunked and isinstance(data, io.BufferedReader): | ||
# Not chunking if content-length can be determined | ||
self.headers[hdrs.CONTENT_LENGTH] = str(getsize(data.name)) |
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.
What if file was read a bit before it's going to be sent with a request?
with open('foo.bar', 'rb') as f:
magic = f.read(3)
...
yield from aiohttp.request('post', 'http://localhost/upload', data=f)
In this case Content-Length header will declare more bytes than actually could be sent. Use tell() to figure out how much were already read. Also, since you have file description here, you can do just os.fstat(data.fileno()).st_size
what is a bit more efficient.
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.
Thx, I'll have a look at that.
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.
Fixed both seeking and more efficient file size access.
@fafhrd91 sure. |
self.chunked = True | ||
if not self.chunked and isinstance(data, io.BufferedReader): | ||
# Not chunking if content-length can be determined | ||
size = fstat(data.fileno()).st_size - data.tell() |
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 would use full os.fstat
here since it doesn't simply to figure out what fstat is and where it comes from. Also look on the other imports from stdlib.
LGFM expect the import thing. Please, squash all the commits and it will be ready for merge (: |
Thanks, Hugo! |
@kxepal looks like your request for |
@asvetlov yes, fixed that. thanks for notice! |
The upload of a single file is not chunked anymore as the content-length can be determined,
except if the chunked argument is passed forced as True.
This fixes #126 for uploading a single file, but does not apply to multipart uploads.