max file size
authorRodney Ewing <ewing.rj@gmail.com>
Thu, 13 Jun 2013 23:37:42 +0000 (16:37 -0700)
committerRodney Ewing <ewing.rj@gmail.com>
Mon, 26 Aug 2013 13:33:30 +0000 (06:33 -0700)
mediagoblin/config_spec.ini
mediagoblin/submit/forms.py
mediagoblin/submit/views.py

index be6132f600588e08a6df0850d454b7ab553e1d2d..535af4c1cca7e46bdb2c099ed925d61c3b7e4e56 100644 (file)
@@ -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
index e9bd93fde6427de0c6f4d3dc37fbf3b9717adda5..0a3b4e4da672cf6072848ca9b12d6d79e97236cb 100644 (file)
 
 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
-                      <a href="http://daringfireball.net/projects/markdown/basics">
-                      Markdown</a> 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
+                        <a href="http://daringfireball.net/projects/markdown/basics">
+                        Markdown</a> 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(
index 7382c43dea2901ecea7e528ea399943df97994db..7608f509650d1f3f890021befaa068bcf5155ed1 100644 (file)
@@ -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)