Opened 9 years ago

Last modified 9 years ago

#26495 closed Cleanup/optimization

Storage save method wraps StringIO in File object which is identified as False. — at Version 2

Reported by: Maxim Novikov Owned by: nobody
Component: File uploads/storage Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Maxim Novikov)

https://github.com/django/django/blob/master/django/core/files/storage.py#L50

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.

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

    if not hasattr(content, 'chunks'):
        content = File(content, name=name)

Change History (2)

comment:1 by Tim Graham, 9 years ago

It's not clear to me how to reproduce the issue. In particular I'm not sure what the store method refers to (I don't see anything in Django itself with that name). Please try to include more details such as a test for Django's test suite (ideally) or a sample project.

comment:2 by Maxim Novikov, 9 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top