Permalink
Browse files

Merge pull request #8 from plone/tt-mimetypes

mimetype can't be None and always use filename if available
  • Loading branch information...
2 parents 6793591 + 222d303 commit d155ce2490aea4f86d0c4fefe1da53d57148a1dd @vangheem vangheem committed Sep 13, 2013
Showing with 23 additions and 2 deletions.
  1. +3 −0 CHANGES.rst
  2. +6 −1 src/plone/app/blob/field.py
  3. +14 −1 src/plone/app/blob/tests/test_base_fields.py
View
@@ -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]
@@ -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
@@ -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(
@@ -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

0 comments on commit d155ce2

Please sign in to comment.