Skip to content

Commit

Permalink
Ignore contentType sent by browser for file uploads.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasgraf committed Jun 16, 2014
1 parent 176ba5d commit 5f78970
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
9 changes: 5 additions & 4 deletions docs/HISTORY.txt
@@ -1,11 +1,12 @@
Changelog
=========

1.0.11 (unreleased)
-------------------

- Nothing changed yet.
1.1 (unreleased)
----------------

- Ignore contentType sent by browser for file uploads.
See https://github.com/plone/plone.formwidget.namedfile/issues/9
[lgraf]

1.0.10 (2014-05-26)
-------------------
Expand Down
9 changes: 2 additions & 7 deletions plone/formwidget/namedfile/converter.py
Expand Up @@ -26,22 +26,17 @@ def toFieldValue(self, value):
return value
elif isinstance(value, FileUpload):

headers = value.headers
filename = safe_basename(value.filename)

if filename is not None and not isinstance(filename, unicode):
# Work-around for
# https://bugs.launchpad.net/zope2/+bug/499696
filename = filename.decode('utf-8')

contentType = 'application/octet-stream'
if headers:
contentType = headers.get('Content-Type', contentType)


value.seek(0)
data = value.read()
if data or filename:
return self.field._type(data=data, contentType=contentType, filename=filename)
return self.field._type(data=data, filename=filename)
else:
return self.field.missing_value

Expand Down
19 changes: 13 additions & 6 deletions plone/formwidget/namedfile/widget.txt
Expand Up @@ -512,8 +512,11 @@ A data string is converted to the appropriate type:
>>> image_converter.toFieldValue('random data')
<plone.namedfile.file.NamedImage object at ...>

A FileUpload object is converted to the appropriate type, preserving filename
and content type, and possibly handling international characters in filenames.
A FileUpload object is converted to the appropriate type, preserving filename,
and possibly handling international characters in filenames.
The content type sent by the browser will be ignored because it's unreliable
- it's left to the implementation of the file field to determine the proper
content type.

>>> myfile = cStringIO.StringIO('File upload contents.')
>>> # \xc3\xb8 is UTF-8 for a small letter o with slash
Expand All @@ -524,8 +527,11 @@ and content type, and possibly handling international characters in filenames.
'File upload contents.'
>>> file_obj.filename
u'rand\xf8m.txt'
>>> file_obj.contentType
'text/x-dummy'

Content type from headers sent by browser should be ignored:

>>> file_obj.contentType != 'text/x-dummy'
True

>>> myfile = cStringIO.StringIO('Random image content.')
>>> aFieldStorage = FieldStorageStub(myfile, filename='random.png', headers={'Content-Type': 'image/x-dummy'})
Expand All @@ -534,8 +540,9 @@ and content type, and possibly handling international characters in filenames.
'Random image content.'
>>> image_obj.filename
u'random.png'
>>> image_obj.contentType
'image/x-dummy'
>>> image_obj.contentType != 'image/x-dummy'
True


However, a zero-length, unnamed FileUpload results in the field's missing_value
being returned.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
@@ -1,7 +1,7 @@
from setuptools import setup, find_packages
import os

version = '1.0.11.dev0'
version = '1.1.dev0'

setup(name='plone.formwidget.namedfile',
version=version,
Expand Down

0 comments on commit 5f78970

Please sign in to comment.