mirror of
https://github.com/QingdaoU/Spirit.git
synced 2024-12-28 15:32:12 +00:00
make libmagic optional (#201)
This commit is contained in:
parent
bbe9969573
commit
73b73e8a9e
@ -5,8 +5,6 @@ from __future__ import unicode_literals
|
||||
import os
|
||||
import logging
|
||||
|
||||
import magic
|
||||
|
||||
from django import forms
|
||||
from django.core.files.storage import default_storage
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
@ -22,6 +20,13 @@ from .models import Comment
|
||||
|
||||
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):
|
||||
comment = forms.CharField(
|
||||
@ -158,14 +163,8 @@ class CommentFileForm(forms.Form):
|
||||
def clean_file(self):
|
||||
file = self.cleaned_data['file']
|
||||
|
||||
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 not magic:
|
||||
raise forms.ValidationError(_("The file could not be validated"))
|
||||
|
||||
# Won't ever raise. Has at most one '.' so lstrip is fine here
|
||||
ext = os.path.splitext(file.name)[1].lstrip('.')
|
||||
@ -178,6 +177,15 @@ class CommentFileForm(forms.Form):
|
||||
", ".join(
|
||||
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:
|
||||
raise forms.ValidationError(
|
||||
_("Unsupported file mime type %s. Supported types are %s." % (
|
||||
|
@ -16,10 +16,15 @@ from django.core.files.uploadedfile import SimpleUploadedFile
|
||||
from django.test.utils import override_settings
|
||||
from django.utils.six import BytesIO
|
||||
|
||||
from . import forms as comment_forms
|
||||
from ..core.conf import settings
|
||||
from ..core.tests import utils
|
||||
from .models import Comment
|
||||
from .forms import CommentForm, CommentMoveForm, CommentImageForm
|
||||
from .forms import (
|
||||
CommentForm,
|
||||
CommentMoveForm,
|
||||
CommentImageForm,
|
||||
CommentFileForm)
|
||||
from .tags import render_comments_form
|
||||
from ..core.utils import markdown
|
||||
from .views import delete as comment_delete
|
||||
@ -794,6 +799,25 @@ class CommentFormTest(TestCase):
|
||||
form = CommentImageForm(data={}, files=files)
|
||||
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):
|
||||
"""
|
||||
Restrict comment len
|
||||
|
Loading…
Reference in New Issue
Block a user