max file size

This commit is contained in:
Rodney Ewing 2013-06-13 16:37:42 -07:00
parent 2f74de492e
commit ecb4512822
3 changed files with 59 additions and 37 deletions

View File

@ -78,6 +78,9 @@ plugin_linked_assets_dir = string(default="%(here)s/user_dev/plugin_static/")
# Default user upload limit (in Mb) # Default user upload limit (in Mb)
upload_limit = integer(default=None) upload_limit = integer(default=None)
# Max file size (in Mb)
max_file_size = integer(default=5000)
[jinja2] [jinja2]
# Jinja2 supports more directives than the minimum required by mediagoblin. # Jinja2 supports more directives than the minimum required by mediagoblin.
# This setting allows users creating custom templates to specify a list of # This setting allows users creating custom templates to specify a list of

View File

@ -17,30 +17,41 @@
import wtforms import wtforms
from mediagoblin import mg_globals
from mediagoblin.tools.text import tag_length_validator from mediagoblin.tools.text import tag_length_validator
from mediagoblin.tools.translate import lazy_pass_to_ugettext as _ from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
from mediagoblin.tools.licenses import licenses_as_choices from mediagoblin.tools.licenses import licenses_as_choices
class SubmitStartForm(wtforms.Form): def get_submit_start_form(form, **kwargs):
file = wtforms.FileField(_('File')) max_file_size = mg_globals.app_config.get('max_file_size', None)
title = wtforms.TextField( desc = None
_('Title'), if max_file_size:
[wtforms.validators.Length(min=0, max=500)]) desc = _('Max file size: {0} mb'.format(max_file_size))
description = wtforms.TextAreaField(
_('Description of this work'), class SubmitStartForm(wtforms.Form):
description=_("""You can use file = wtforms.FileField(
<a href="http://daringfireball.net/projects/markdown/basics"> _('File'),
Markdown</a> for formatting.""")) description=desc)
tags = wtforms.TextField( title = wtforms.TextField(
_('Tags'), _('Title'),
[tag_length_validator], [wtforms.validators.Length(min=0, max=500)])
description=_( description = wtforms.TextAreaField(
"Separate tags by commas.")) _('Description of this work'),
license = wtforms.SelectField( description=_("""You can use
_('License'), <a href="http://daringfireball.net/projects/markdown/basics">
[wtforms.validators.Optional(),], Markdown</a> for formatting."""))
choices=licenses_as_choices()) 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): class AddCollectionForm(wtforms.Form):
title = wtforms.TextField( title = wtforms.TextField(

View File

@ -57,7 +57,7 @@ def submit_start(request):
return redirect(request, "mediagoblin.user_pages.user_home", return redirect(request, "mediagoblin.user_pages.user_home",
user=request.user.username) 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) license=request.user.license_preference)
if request.method == 'POST' and submit_form.validate(): if request.method == 'POST' and submit_form.validate():
@ -105,32 +105,40 @@ def submit_start(request):
entry.queued_media_file) / (1024.0 * 1024) entry.queued_media_file) / (1024.0 * 1024)
file_size = float('{0:.2f}'.format(file_size)) 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 # Check if user is over upload limit
if upload_limit and (user.uploaded + file_size) >= upload_limit: if upload_limit and (user.uploaded + file_size) >= upload_limit:
submit_form.file.errors.append( submit_form.file.errors.append(
_('Sorry, uploading this file will put you over your' _('Sorry, uploading this file will put you over your'
' upload limit.')) ' upload limit.'))
return redirect(request, "mediagoblin.submit.start", error = True
user=user.username)
user.uploaded = user.uploaded + file_size if not error:
user.save() user.uploaded = user.uploaded + file_size
user.save()
entry.file_size = file_size entry.file_size = file_size
# Save now so we have this data before kicking off processing # Save now so we have this data before kicking off processing
entry.save() entry.save()
# Pass off to async processing # Pass off to processing
# #
# (... don't change entry after this point to avoid race # (... don't change entry after this point to avoid race
# conditions with changes to the document via processing code) # conditions with changes to the document via processing code)
feed_url = request.urlgen( feed_url = request.urlgen(
'mediagoblin.user_pages.atom_feed', 'mediagoblin.user_pages.atom_feed',
qualified=True, user=request.user.username) qualified=True, user=request.user.username)
run_process_media(entry, feed_url) run_process_media(entry, feed_url)
add_message(request, SUCCESS, _('Woohoo! Submitted!'))
add_message(request, SUCCESS, _('Woohoo! Submitted!'))
add_comment_subscription(request.user, entry) add_comment_subscription(request.user, entry)