Skip to content
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

mimetype can't be None and always use filename if available #8

Merged
merged 1 commit into from
Sep 13, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Changelog
1.5.9 (unreleased)
------------------

- Make sure mimetype is not None and use use filename for detection if available.
[tschanzt]

- Make sure the id is safe before setting the filename as id
[pbauer]

Expand Down
7 changes: 6 additions & 1 deletion src/plone/app/blob/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,18 @@ def set(self, instance, value, **kwargs):
blob = BlobWrapper(self.default_content_type)
if isinstance(value, basestring):
value = StringIO(value) # simple strings cannot be adapted...
setattr(value, 'filename', kwargs.get('filename', None))
if value is not None:
blobbable = IBlobbable(value)
try:
blobbable.feed(blob.getBlob())
except ReuseBlob, exception:
blob.setBlob(exception.args[0]) # reuse the given blob
blob.setContentType(kwargs.get('mimetype', blobbable.mimetype()))
mimetype = kwargs.get('mimetype', None)
if not mimetype:
mimetype = blobbable.mimetype()
if mimetype and mimetype != 'None':
blob.setContentType(mimetype)
blob.setFilename(kwargs.get('filename', blobbable.filename()))
super(BlobField, self).set(instance, blob, **kwargs)
# a transaction savepoint is created after setting the blob's value
Expand Down
15 changes: 14 additions & 1 deletion src/plone/app/blob/tests/test_base_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from plone.app.blob.field import FileField, ImageField
from plone.app.blob.tests.utils import getFile


SampleSchema = BaseSchema.copy() + Schema((

FileField(
Expand Down Expand Up @@ -83,6 +82,20 @@ def testGetSize(self):
field = item.getField('hmm')
self.assertEqual(field.getSize(item), (0, 0))

def testSetFieldDefaultMime(self):
item = self.create()
file_ = getFile('test.pdf')
item.setFoo(file_, filename='file.blubb', mimetype=None)
self.assertEqual('application/pdf', item.getFoo().getContentType())

def testStringDataRespectsFilename(self):
item = self.create()
file_ = getFile('test.pdf')
item.setFoo(file_.read(), filename='file.xls', mimetype=None)
self.assertEqual(
'application/vnd.ms-excel',
item.getFoo().getContentType()
)

def test_suite():
from unittest import defaultTestLoader
Expand Down