make libmagic optional (#201)

This commit is contained in:
Esteban Castro Borsani 2017-09-25 04:34:57 -03:00 committed by GitHub
parent bbe9969573
commit 73b73e8a9e
2 changed files with 43 additions and 11 deletions

View File

@ -5,8 +5,6 @@ from __future__ import unicode_literals
import os import os
import logging import logging
import magic
from django import forms from django import forms
from django.core.files.storage import default_storage from django.core.files.storage import default_storage
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
@ -22,6 +20,13 @@ from .models import Comment
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# Mostly for Windows users who don't need file upload
try:
import magic
except ImportError as err:
logger.exceptioin(err)
magic = None
class CommentForm(forms.ModelForm): class CommentForm(forms.ModelForm):
comment = forms.CharField( comment = forms.CharField(
@ -158,13 +163,7 @@ class CommentFileForm(forms.Form):
def clean_file(self): def clean_file(self):
file = self.cleaned_data['file'] file = self.cleaned_data['file']
try: if not magic:
if isinstance(file, TemporaryUploadedFile):
file_mime = magic.from_file(file.temporary_file_path(), mime=True)
else: # In-memory file
file_mime = magic.from_buffer(file.read(), mime=True)
except magic.MagicException as e:
logger.exception(e)
raise forms.ValidationError(_("The file could not be validated")) raise forms.ValidationError(_("The file could not be validated"))
# Won't ever raise. Has at most one '.' so lstrip is fine here # Won't ever raise. Has at most one '.' so lstrip is fine here
@ -178,6 +177,15 @@ class CommentFileForm(forms.Form):
", ".join( ", ".join(
sorted(settings.ST_ALLOWED_UPLOAD_FILE_MEDIA_TYPE.keys()))))) sorted(settings.ST_ALLOWED_UPLOAD_FILE_MEDIA_TYPE.keys())))))
try:
if isinstance(file, TemporaryUploadedFile):
file_mime = magic.from_file(file.temporary_file_path(), mime=True)
else: # In-memory file
file_mime = magic.from_buffer(file.read(), mime=True)
except magic.MagicException as e:
logger.exception(e)
raise forms.ValidationError(_("The file could not be validated"))
if mime != file_mime: if mime != file_mime:
raise forms.ValidationError( raise forms.ValidationError(
_("Unsupported file mime type %s. Supported types are %s." % ( _("Unsupported file mime type %s. Supported types are %s." % (

View File

@ -16,10 +16,15 @@ from django.core.files.uploadedfile import SimpleUploadedFile
from django.test.utils import override_settings from django.test.utils import override_settings
from django.utils.six import BytesIO from django.utils.six import BytesIO
from . import forms as comment_forms
from ..core.conf import settings from ..core.conf import settings
from ..core.tests import utils from ..core.tests import utils
from .models import Comment from .models import Comment
from .forms import CommentForm, CommentMoveForm, CommentImageForm from .forms import (
CommentForm,
CommentMoveForm,
CommentImageForm,
CommentFileForm)
from .tags import render_comments_form from .tags import render_comments_form
from ..core.utils import markdown from ..core.utils import markdown
from .views import delete as comment_delete from .views import delete as comment_delete
@ -794,6 +799,25 @@ class CommentFormTest(TestCase):
form = CommentImageForm(data={}, files=files) form = CommentImageForm(data={}, files=files)
self.assertFalse(form.is_valid()) self.assertFalse(form.is_valid())
def test_comment_file_upload_no_libmagic(self):
"""
Magic lib should be optional
"""
utils.login(self)
file = BytesIO(
b'%PDF-1.0\n1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj 2 0 obj<</Type/Pages/Kids[3 0 R]/Count 1'
b'>>endobj 3 0 obj<</Type/Page/MediaBox[0 0 3 3]>>endobj\nxref\n0 4\n0000000000 65535 f\n000000'
b'0010 00000 n\n0000000053 00000 n\n0000000102 00000 n\ntrailer<</Size 4/Root 1 0 R>>\nstartxre'
b'f\n149\n%EOF\n')
files = {'file': SimpleUploadedFile('file.pdf', file.read(), content_type='application/pdf'),}
form = CommentFileForm(data={}, files=files)
comment_forms_magic_orig, comment_forms.magic = comment_forms.magic, None
try:
self.assertFalse(form.is_valid())
finally:
comment_forms.magic = comment_forms_magic_orig
def test_comment_max_len(self): def test_comment_max_len(self):
""" """
Restrict comment len Restrict comment len