#26495: Saving StringIO using custom storage class may result in empty content
written to file
-------------------------------------+-------------------------------------
     Reporter:  m-novikov            |                    Owner:  nobody
         Type:  Uncategorized        |                   Status:  new
    Component:  File                 |                  Version:  master
  uploads/storage                    |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by m-novikov:

Old description:

> https://github.com/django/django/blob/master/django/core/files/storage.py#L50
> {{{
> #!python
> class Storage(object):
>
>     def save(self, name, content, max_length=None):
>         ...
>         if not hasattr(content, 'chunks'):
>             content = File(content)
> }}}
> For example if custom storage uses requests with post query.
> {{{
> #!python
> import requests
> class CustomStorage(object):
>     def _save(self, name, content, max_length=None):
>           requests.post('http://testhost.org/upload', data=content)
> }}}
> But it will result in empty upload as bool(content) is False.
> Possibly better to pass a name to File object. To ensure bool(content) is
> True
> {{{
> #!python
>     if not hasattr(content, 'chunks'):
>         content = File(content, name=name)
> }}}

New description:

 https://github.com/django/django/blob/master/django/core/files/storage.py#L50
 {{{
 #!python
 class Storage(object):

     def save(self, name, content, max_length=None):
         ...
         if not hasattr(content, 'chunks'):
             content = File(content)
 }}}
 For example if custom storage uses requests with post query and we are
 saving StringIO object.
 {{{
 #!python
 from StringIO import StringIO

 from django.core.files.storage import Storage
 import requests

 class CustomStorage(Storage):
     def _save(self, name, content, max_length=None):
           requests.post('http://testhost.org/upload', data=content)
 custom_storage = CustomStorage()
 custom_storage.save('new_name', StringIO('content'))
 }}}
 But it will result in empty upload as bool(content) is False.
 Possibly better to pass a name to File object. To ensure bool(content) is
 True
 {{{
 #!python
     if not hasattr(content, 'chunks'):
         content = File(content, name=name)
 }}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/26495#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/067.c7cf305dd91385c2bd44ac93127f852f%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to