import logging
import re
-from os.path import splitext
-import shutil
from werkzeug.exceptions import MethodNotAllowed, BadRequest, NotImplemented
from werkzeug.wrappers import BaseResponse
+from mediagoblin.tools.translate import pass_to_ugettext as _
from mediagoblin.meddleware.csrf import csrf_exempt
from mediagoblin.auth.tools import check_login_simple
-from mediagoblin.media_types import sniff_media
-from mediagoblin.submit.lib import check_file_field, prepare_queue_task, \
- run_process_media, new_upload_entry
+from mediagoblin.submit.lib import \
+ submit_media, check_file_field, get_upload_file_limits, \
+ FileUploadLimit, UserUploadLimit, UserPastUploadLimit
+
from mediagoblin.user_pages.lib import add_media_to_collection
from mediagoblin.db.models import Collection
if not check_file_field(request, 'image'):
raise BadRequest()
- filename = request.files['image'].filename
-
- # Sniff the submitted media to determine which
- # media plugin should handle processing
- media_type, media_manager = sniff_media(
- request.files['image'])
-
- # create entry and save in database
- entry = new_upload_entry(request.user)
- 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
- if collection_id > 0:
- collection = Collection.query.get(collection_id)
- if collection is not None and collection.creator == request.user.id:
- add_media_to_collection(collection, entry, "")
-
- return {'image_id': entry.id, 'url': entry.url_for_self(request.urlgen,
- qualified=True)}
+ upload_limit, max_file_size = get_upload_file_limits(request.user)
+
+ try:
+ entry = submit_media(
+ request.app, request.user,
+ request.files['image'], request.files['image'].filename,
+ unicode(form.name.data),
+ unicode(form.comment.data),
+ upload_limit, max_file_size)
+
+ collection_id = form.category.data
+ if collection_id > 0:
+ collection = Collection.query.get(collection_id)
+ if collection is not None and collection.creator == request.user.id:
+ add_media_to_collection(collection, entry, "")
+
+ return {
+ 'image_id': entry.id,
+ 'url': entry.url_for_self(
+ request.urlgen,
+ 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}$")