From ecb45128229cfe22e29589237bdd59b7cc965181 Mon Sep 17 00:00:00 2001 From: Rodney Ewing Date: Thu, 13 Jun 2013 16:37:42 -0700 Subject: [PATCH] max file size --- mediagoblin/config_spec.ini | 3 +++ mediagoblin/submit/forms.py | 49 ++++++++++++++++++++-------------- mediagoblin/submit/views.py | 52 +++++++++++++++++++++---------------- 3 files changed, 63 insertions(+), 41 deletions(-) diff --git a/mediagoblin/config_spec.ini b/mediagoblin/config_spec.ini index be6132f6..535af4c1 100644 --- a/mediagoblin/config_spec.ini +++ b/mediagoblin/config_spec.ini @@ -78,6 +78,9 @@ plugin_linked_assets_dir = string(default="%(here)s/user_dev/plugin_static/") # Default user upload limit (in Mb) upload_limit = integer(default=None) +# Max file size (in Mb) +max_file_size = integer(default=5000) + [jinja2] # Jinja2 supports more directives than the minimum required by mediagoblin. # This setting allows users creating custom templates to specify a list of diff --git a/mediagoblin/submit/forms.py b/mediagoblin/submit/forms.py index e9bd93fd..0a3b4e4d 100644 --- a/mediagoblin/submit/forms.py +++ b/mediagoblin/submit/forms.py @@ -17,30 +17,41 @@ import wtforms +from mediagoblin import mg_globals from mediagoblin.tools.text import tag_length_validator from mediagoblin.tools.translate import lazy_pass_to_ugettext as _ from mediagoblin.tools.licenses import licenses_as_choices -class SubmitStartForm(wtforms.Form): - file = wtforms.FileField(_('File')) - title = wtforms.TextField( - _('Title'), - [wtforms.validators.Length(min=0, max=500)]) - description = wtforms.TextAreaField( - _('Description of this work'), - description=_("""You can use - - Markdown for formatting.""")) - tags = wtforms.TextField( - _('Tags'), - [tag_length_validator], - description=_( - "Separate tags by commas.")) - license = wtforms.SelectField( - _('License'), - [wtforms.validators.Optional(),], - choices=licenses_as_choices()) +def get_submit_start_form(form, **kwargs): + max_file_size = mg_globals.app_config.get('max_file_size', None) + desc = None + if max_file_size: + desc = _('Max file size: {0} mb'.format(max_file_size)) + + class SubmitStartForm(wtforms.Form): + file = wtforms.FileField( + _('File'), + description=desc) + title = wtforms.TextField( + _('Title'), + [wtforms.validators.Length(min=0, max=500)]) + description = wtforms.TextAreaField( + _('Description of this work'), + description=_("""You can use + + Markdown for formatting.""")) + tags = wtforms.TextField( + _('Tags'), + [tag_length_validator], + description=_( + "Separate tags by commas.")) + license = wtforms.SelectField( + _('License'), + [wtforms.validators.Optional(),], + choices=licenses_as_choices()) + + return SubmitStartForm(form, **kwargs) class AddCollectionForm(wtforms.Form): title = wtforms.TextField( diff --git a/mediagoblin/submit/views.py b/mediagoblin/submit/views.py index 7382c43d..7608f509 100644 --- a/mediagoblin/submit/views.py +++ b/mediagoblin/submit/views.py @@ -57,7 +57,7 @@ def submit_start(request): return redirect(request, "mediagoblin.user_pages.user_home", user=request.user.username) - submit_form = submit_forms.SubmitStartForm(request.form, + submit_form = submit_forms.get_submit_start_form(request.form, license=request.user.license_preference) if request.method == 'POST' and submit_form.validate(): @@ -105,32 +105,40 @@ def submit_start(request): entry.queued_media_file) / (1024.0 * 1024) file_size = float('{0:.2f}'.format(file_size)) + error = False + + # Check if file size is over the limit + max_file_size = mg_globals.app_config.get('max_file_size', None) + if max_file_size and file_size >= max_file_size: + submit_form.file.errors.append( + _(u'Sorry, the file size is too big.')) + error = True + # Check if user is over upload limit if upload_limit and (user.uploaded + file_size) >= upload_limit: submit_form.file.errors.append( _('Sorry, uploading this file will put you over your' ' upload limit.')) - return redirect(request, "mediagoblin.submit.start", - user=user.username) - - user.uploaded = user.uploaded + file_size - user.save() - - entry.file_size = file_size - - # Save now so we have this data before kicking off processing - entry.save() - - # Pass off to async processing - # - # (... don't change entry after this point to avoid race - # conditions with changes to the document via processing code) - feed_url = request.urlgen( - 'mediagoblin.user_pages.atom_feed', - qualified=True, user=request.user.username) - run_process_media(entry, feed_url) - - add_message(request, SUCCESS, _('Woohoo! Submitted!')) + error = True + + if not error: + user.uploaded = user.uploaded + file_size + user.save() + + entry.file_size = file_size + + # Save now so we have this data before kicking off processing + entry.save() + + # Pass off to processing + # + # (... don't change entry after this point to avoid race + # conditions with changes to the document via processing code) + feed_url = request.urlgen( + 'mediagoblin.user_pages.atom_feed', + qualified=True, user=request.user.username) + run_process_media(entry, feed_url) + add_message(request, SUCCESS, _('Woohoo! Submitted!')) add_comment_subscription(request.user, entry) -- 2.25.1