#26495: Storage save method wraps StringIO in File object which is identified as
False.
-------------------------------------+-------------------------------------
     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)
> }}}
> Underlying libraries may use following pattern of code:
> {{{
> #!python
> def store(data, name):
>      data = data or ''
> }}}
> But File object without a name resolved into False which can produce
> bugs.
> 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.
 {{{
 #!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)
 }}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/26495#comment:2>
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.d17a4c267e41753b412fd723d52b19db%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to