-
-
Notifications
You must be signed in to change notification settings - Fork 863
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 Issue #708: Save files with string content #911
Fix Issue #708: Save files with string content #911
Conversation
cdf3180
to
0c64612
Compare
@jschneier Can I get a review on this? You can see my new tests that reproduce the issue, fail in the build on commit c3d00d9. The latest build is just flake8 failing which is also happening on master. |
@@ -440,6 +440,10 @@ def _save(self, name, content): | |||
name = self._normalize_name(cleaned_name) | |||
params = self._get_write_parameters(name, content) | |||
|
|||
# wrap content so read() always returns bytes. This is required for passing it | |||
# to obj.upload_fileobj() or self._compress_content() | |||
content = ReadBytesWrapper(content) |
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.
This is the core part of the fix. This approach has the benefit of not making any extra read() or seek() calls to the file object.
61e733b
to
b24e9a9
Compare
641cbfd
to
ca9c466
Compare
@jschneier Can I get this reviewed? Most of the diff is in tests. The new tests in |
@jschneier Still waiting for a review |
Also fixes #961 |
@LincolnPuzey I see there are conflicts, could you please resolve them? I hit the described issue using versions:
Would be lovely if django storages worked with s3 and boto3 out of the box, without workarounds. |
I'm happy to resolve the conflicts if @jschneier or another maintainer can indicate they're willing to review and merge the PR. |
Thanks for the PR , it fix a problem we have in our projects. |
Any update? |
@LincolnPuzey here is another one interested in this topic, as we're having problems with text files uploaded from Admin using django-import-export v3.2.0 Any merge prevision? |
This makes .read() always return bytes. If .read() returns a string, it will be encoded to bytes before being returned. The encoding to use can be specified, otherwise will use the .encoding property of the original file, otherwise will use utf-8.
…per. This correctly handles file-like-objects that are open in text/string mode by converting to strings returned by read() to bytes, which is what upload_fileobj() requires. This is done before gzip compressing, so also removed force_bytes() call in _compress_content().
Add these tests in a new test class that uses moto. Remove old test for saving ContentFile Move test for detecting content-type to this new class. Add some more tests around this. Fix tests that fail because settings.AWS_STORAGE_BUCKET_NAME is now defined. Fix tests that fail because content is always wrapped. Fix test for gzipped file since that now only takes bytes.
ca9c466
to
1f45042
Compare
Thanks, |
Thanks a lot @jschneier, we will wait for the next release with this improvement 👏🏽 |
No need to wait; I released it already.
…On Tuesday, September 5, 2023, Hugo Camino ***@***.***> wrote:
Thanks a lot @jschneier <https://github.com/jschneier>, we will wait for
the next release with this improvement 👏🏽
—
Reply to this email directly, view it on GitHub
<#911 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAREDWFH7G5NSXIOOTOIAMTXY3MEPANCNFSM4PIMAX3Q>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
@jschneier sorry I didn't find new releases in Github tags section like past ones but now I see it was in here. Thanks! |
Ah I forgot to apply the tag, fixed. Thanks.
… On Sep 5, 2023, at 06:37, Hugo Camino ***@***.***> wrote:
@jschneier <https://github.com/jschneier> sorry I didn't find new releases in Github tags section like past ones but now I see it was in here <https://pypi.org/project/django-storages/>. Thanks!
—
Reply to this email directly, view it on GitHub <#911 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AAREDWHFCPNSM33N4BVV3BLXY36HDANCNFSM4PIMAX3Q>.
You are receiving this because you were mentioned.
|
It's people like @jschneier that make OSS awesome. Thanks Josh ❤️ |
Wraps file objects open in string mode so they produce bytes and can be passed to
obj.upload_fileobj()