Porting the piwigo submit system over to using the new submit utility.

This also adds upload limit checks to the piwigo plugin!

This commit sponsored by Sam Black.  Thank you!
This commit is contained in:
Christopher Allan Webber 2013-11-14 11:07:12 -06:00
parent 5d754da741
commit 860b380bb5

View File

@ -16,17 +16,17 @@
import logging import logging
import re import re
from os.path import splitext
import shutil
from werkzeug.exceptions import MethodNotAllowed, BadRequest, NotImplemented from werkzeug.exceptions import MethodNotAllowed, BadRequest, NotImplemented
from werkzeug.wrappers import BaseResponse from werkzeug.wrappers import BaseResponse
from mediagoblin.tools.translate import pass_to_ugettext as _
from mediagoblin.meddleware.csrf import csrf_exempt from mediagoblin.meddleware.csrf import csrf_exempt
from mediagoblin.auth.tools import check_login_simple from mediagoblin.auth.tools import check_login_simple
from mediagoblin.media_types import sniff_media from mediagoblin.submit.lib import \
from mediagoblin.submit.lib import check_file_field, prepare_queue_task, \ submit_media, check_file_field, get_upload_file_limits, \
run_process_media, new_upload_entry FileUploadLimit, UserUploadLimit, UserPastUploadLimit
from mediagoblin.user_pages.lib import add_media_to_collection from mediagoblin.user_pages.lib import add_media_to_collection
from mediagoblin.db.models import Collection from mediagoblin.db.models import Collection
@ -126,49 +126,15 @@ def pwg_images_addSimple(request):
if not check_file_field(request, 'image'): if not check_file_field(request, 'image'):
raise BadRequest() raise BadRequest()
filename = request.files['image'].filename upload_limit, max_file_size = get_upload_file_limits(request.user)
# Sniff the submitted media to determine which try:
# media plugin should handle processing entry = submit_media(
media_type, media_manager = sniff_media( request.app, request.user,
request.files['image']) request.files['image'], request.files['image'].filename,
unicode(form.name.data),
# create entry and save in database unicode(form.comment.data),
entry = new_upload_entry(request.user) upload_limit, max_file_size)
entry.media_type = unicode(media_type)
entry.title = (
unicode(form.name.data)
or unicode(splitext(filename)[0]))
entry.description = unicode(form.comment.data)
'''
# Process the user's folksonomy "tags"
entry.tags = convert_to_tag_list_of_dicts(
form.tags.data)
'''
# Generate a slug from the title
entry.generate_slug()
queue_file = prepare_queue_task(request.app, entry, filename)
with queue_file:
shutil.copyfileobj(request.files['image'].stream,
queue_file,
length=4 * 1048576)
# 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)
collection_id = form.category.data collection_id = form.category.data
if collection_id > 0: if collection_id > 0:
@ -176,9 +142,24 @@ def pwg_images_addSimple(request):
if collection is not None and collection.creator == request.user.id: if collection is not None and collection.creator == request.user.id:
add_media_to_collection(collection, entry, "") add_media_to_collection(collection, entry, "")
return {'image_id': entry.id, 'url': entry.url_for_self(request.urlgen, return {
'image_id': entry.id,
'url': entry.url_for_self(
request.urlgen,
qualified=True)} qualified=True)}
# Handle upload limit issues
except FileUploadLimit:
raise BadRequest(
_(u'Sorry, the file size is too big.'))
except UserUploadLimit:
raise BadRequest(
_('Sorry, uploading this file will put you over your'
' upload limit.'))
except UserPastUploadLimit:
raise BadRequest(
_('Sorry, you have reached your upload limit.'))
md5sum_matcher = re.compile(r"^[0-9a-fA-F]{32}$") md5sum_matcher = re.compile(r"^[0-9a-fA-F]{32}$")