Skip to content

Commit

Permalink
Merge pull request #46 from collective/tt-flash_mimetypes
Browse files Browse the repository at this point in the history
Use same function to detect mimetypes in flash & quick upload.
  • Loading branch information
buchi committed Jul 3, 2014
2 parents a3fce65 + 75f6a4b commit 711967c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
5 changes: 4 additions & 1 deletion collective/quickupload/browser/quick_upload.py
Expand Up @@ -12,6 +12,7 @@
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from Products.Sessions.SessionDataManager import SessionDataManagerErr
from ZPublisher.HTTPRequest import HTTPRequest
from ZPublisher.HTTPRequest import FileUpload
from collective.quickupload import HAS_DEXTERITY
from collective.quickupload import logger
from collective.quickupload import siteMessageFactory as _
Expand Down Expand Up @@ -583,7 +584,7 @@ def flash_upload_file(self):

file_name = request.form.get("Filename", "")
file_data = request.form.get("Filedata", None)
content_type = mimetypes.guess_type(file_name)[0]
content_type = get_content_type(context, file_data, file_name)
portal_type = request.form.get('typeupload', '')
title = request.form.get("title", None)
description = request.form.get("description", None)
Expand Down Expand Up @@ -783,6 +784,8 @@ def _check_file_size(self, data):


def get_content_type(context, file_data, filename):
if isinstance(file_data, FileUpload):
file_data = file_data.read()
content_type = mimetypes.guess_type(filename)[0]
# sometimes plone mimetypes registry could be more powerful
if not content_type:
Expand Down
11 changes: 10 additions & 1 deletion collective/quickupload/tests/test_mimetypes.py
Expand Up @@ -3,6 +3,8 @@
import os.path
from collective.quickupload.browser.quick_upload import get_content_type
from Products.CMFCore.utils import getToolByName
from ZPublisher.HTTPRequest import FileUpload
import cgi

class TestMimetypes(unittest.TestCase):

Expand All @@ -12,6 +14,10 @@ def setUp(self):
self.portal = self.layer['portal']
file_ = open("%s/testfile_mimetypes.txt" % os.path.split(__file__)[0], 'r')
self.file_data = file_.read()
fieldstorage = cgi.FieldStorage()
fieldstorage.file = file_
fieldstorage.filename = 'testfile_mimetypes.txt'
self.fileupload = FileUpload(fieldstorage)
mtr = getToolByName(self.portal, 'mimetypes_registry')
mtr.manage_addMimeType('Only globs mimetype', ['application/x-only-glob'], [], 'application.png', globs=['*.onlyglob'])
mtr.manage_addMimeType('Only extension mimetype', ['application/x-only-ext'], ['onlyext'], 'application.png')
Expand All @@ -27,4 +33,7 @@ def test_only_ext(self):
def test_recognized_by_python(self):
content_type = get_content_type(self.portal, self.file_data, 'dummy.txt')
self.assertEqual('text/plain', content_type)


def test_fileupload_instance(self):
content_type = get_content_type(self.portal, self.fileupload, 'dummy.txt')
self.assertEqual('text/plain', content_type)
3 changes: 2 additions & 1 deletion docs/HISTORY.rst
Expand Up @@ -4,7 +4,8 @@ Changelog
1.6.5 (unreleased)
------------------

- Nothing changed yet.
- Use same function to detect mimetypes in flash & quick upload.
[tschanzt]


1.6.4 (2014-06-03)
Expand Down

0 comments on commit 711967c

Please sign in to comment.