def pwg_images_addSimple(request): form = AddSimpleForm(request.form) if not form.validate(): _log.error("addSimple: form failed") raise BadRequest() dump = [] for f in form: dump.append("%s=%r" % (f.name, f.data)) _log.info("addSimple: %r %s %r", request.form, " ".join(dump), request.files) 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)}
def post_entry(request): _log.debug('Posting entry') if request.method == 'OPTIONS': return json_response({'status': 200}) if request.method != 'POST': _log.debug('Must POST against post_entry') raise BadRequest() if not check_file_field(request, 'file'): _log.debug('File field not found') raise BadRequest() upload_limit, max_file_size = get_upload_file_limits(request.user) callback_url = request.form.get('callback_url') if callback_url: callback_url = unicode(callback_url) try: entry = submit_media(mg_app=request.app, user=request.user, submitted_file=request.files['file'], filename=request.files['file'].filename, title=unicode(request.form.get('title')), description=unicode( request.form.get('description')), license=unicode(request.form.get('license', '')), tags_string=unicode(request.form.get('tags', '')), upload_limit=upload_limit, max_file_size=max_file_size, callback_url=callback_url) return json_response(get_entry_serializable(entry, request.urlgen)) # 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.')) except Exception as e: ''' This section is intended to catch exceptions raised in mediagoblin.media_types ''' if isinstance(e, InvalidFileType) or \ isinstance(e, FileTypeNotSupported): raise BadRequest(unicode(e)) else: raise
def post_entry(request): _log.debug('Posting entry') if request.method == 'OPTIONS': return json_response({'status': 200}) if request.method != 'POST': _log.debug('Must POST against post_entry') raise BadRequest() if not check_file_field(request, 'file'): _log.debug('File field not found') raise BadRequest() upload_limit, max_file_size = get_upload_file_limits(request.user) callback_url = request.form.get('callback_url') if callback_url: callback_url = unicode(callback_url) try: entry = submit_media( mg_app=request.app, user=request.user, submitted_file=request.files['file'], filename=request.files['file'].filename, title=unicode(request.form.get('title')), description=unicode(request.form.get('description')), license=unicode(request.form.get('license', '')), upload_limit=upload_limit, max_file_size=max_file_size, callback_url=callback_url) return json_response(get_entry_serializable(entry, request.urlgen)) # 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.')) except Exception as e: ''' This section is intended to catch exceptions raised in mediagoblin.media_types ''' if isinstance(e, InvalidFileType) or \ isinstance(e, FileTypeNotSupported): raise BadRequest(unicode(e)) else: raise
def post_entry(request): _log.debug('Posting entry') if request.method == 'OPTIONS': return json_response({'status': 200}) if request.method != 'POST': _log.debug('Must POST against post_entry') raise BadRequest() if not check_file_field(request, 'file'): _log.debug('File field not found') raise BadRequest() media_file = request.files['file'] media_type, media_manager = sniff_media(media_file) entry = new_upload_entry(request.user) entry.media_type = unicode(media_type) entry.title = unicode( request.form.get('title') or splitext(media_file.filename)[0]) entry.description = unicode(request.form.get('description')) entry.license = unicode(request.form.get('license', '')) entry.generate_slug() # queue appropriately queue_file = prepare_queue_task(request.app, entry, media_file.filename) with queue_file: queue_file.write(request.files['file'].stream.read()) # Save now so we have this data before kicking off processing entry.save() if request.form.get('callback_url'): metadata = request.db.ProcessingMetaData() metadata.media_entry = entry metadata.callback_url = unicode(request.form['callback_url']) metadata.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) return json_response(get_entry_serializable(entry, request.urlgen))
def post_entry(request): _log.debug('Posting entry') if request.method == 'OPTIONS': return json_response({'status': 200}) if request.method != 'POST': _log.debug('Must POST against post_entry') raise BadRequest() if not check_file_field(request, 'file'): _log.debug('File field not found') raise BadRequest() media_file = request.files['file'] media_type, media_manager = sniff_media(media_file) entry = new_upload_entry(request.user) entry.media_type = unicode(media_type) entry.title = unicode(request.form.get('title') or splitext(media_file.filename)[0]) entry.description = unicode(request.form.get('description')) entry.license = unicode(request.form.get('license', '')) entry.generate_slug() # queue appropriately queue_file = prepare_queue_task(request.app, entry, media_file.filename) with queue_file: queue_file.write(request.files['file'].stream.read()) # Save now so we have this data before kicking off processing entry.save() if request.form.get('callback_url'): metadata = request.db.ProcessingMetaData() metadata.media_entry = entry metadata.callback_url = unicode(request.form['callback_url']) metadata.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) return json_response(get_entry_serializable(entry, request.urlgen))
def post_entry(request): _log.debug("Posting entry") if request.method == "OPTIONS": return json_response({"status": 200}) if request.method != "POST": _log.debug("Must POST against post_entry") raise BadRequest() if not check_file_field(request, "file"): _log.debug("File field not found") raise BadRequest() media_file = request.files["file"] media_type, media_manager = sniff_media(media_file) entry = request.db.MediaEntry() entry.media_type = unicode(media_type) entry.title = unicode(request.form.get("title") or splitext(media_file.filename)[0]) entry.description = unicode(request.form.get("description")) entry.license = unicode(request.form.get("license", "")) entry.uploader = request.user.id entry.generate_slug() # queue appropriately queue_file = prepare_queue_task(request.app, entry, media_file.filename) with queue_file: queue_file.write(request.files["file"].stream.read()) # Save now so we have this data before kicking off processing entry.save() if request.form.get("callback_url"): metadata = request.db.ProcessingMetaData() metadata.media_entry = entry metadata.callback_url = unicode(request.form["callback_url"]) metadata.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) return json_response(get_entry_serializable(entry, request.urlgen))
def pwg_images_addSimple(request): form = AddSimpleForm(request.form) if not form.validate(): _log.error("addSimple: form failed") raise BadRequest() dump = [] for f in form: dump.append("%s=%r" % (f.name, f.data)) _log.info("addimple: %r %s %r", request.form, " ".join(dump), request.files) if not check_file_field(request, 'image'): raise BadRequest() return {'image_id': 123456, 'url': ''}
def pwg_images_addSimple(request): form = AddSimpleForm(request.form) if not form.validate(): _log.error("addSimple: form failed") raise BadRequest() dump = [] for f in form: dump.append("%s=%r" % (f.name, f.data)) _log.info("addSimple: %r %s %r", request.form, " ".join(dump), request.files) if not check_file_field(request, 'image'): raise BadRequest() upload_limit, max_file_size = get_upload_file_limits(request.user) try: entry = submit_media( mg_app=request.app, user=request.user, submitted_file=request.files['image'], filename=request.files['image'].filename, title=six.text_type(form.name.data), description=six.text_type(form.comment.data), upload_limit=upload_limit, max_file_size=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.actor == 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.'))
def post_entry(request): _log.debug('Posting entry') if request.method == 'OPTIONS': return json_response({'status': 200}) if request.method != 'POST': _log.debug('Must POST against post_entry') raise BadRequest() if not check_file_field(request, 'file'): _log.debug('File field not found') raise BadRequest() callback_url = request.form.get('callback_url') if callback_url: callback_url = six.text_type(callback_url) try: entry = submit_media( mg_app=request.app, user=request.user, submitted_file=request.files['file'], filename=request.files['file'].filename, title=six.text_type(request.form.get('title')), description=six.text_type(request.form.get('description')), license=six.text_type(request.form.get('license', '')), tags_string=six.text_type(request.form.get('tags', '')), callback_url=callback_url) return json_response(get_entry_serializable(entry, request.urlgen)) # 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.')) except FileTypeNotSupported as e: raise BadRequest(e)
def submit_start(request): """ First view for submitting a file. """ upload_limit, max_file_size = get_upload_file_limits(request.user) submit_form = submit_forms.get_submit_start_form( request.form, license=request.user.license_preference, max_file_size=max_file_size, upload_limit=upload_limit, uploaded=request.user.uploaded) if request.method == 'POST' and submit_form.validate(): if not check_file_field(request, 'file'): submit_form.file.errors.append( _(u'You must provide a file.')) else: try: submit_media( mg_app=request.app, user=request.user, submitted_file=request.files['file'], filename=request.files['file'].filename, title=six.text_type(submit_form.title.data), description=six.text_type(submit_form.description.data), license=six.text_type(submit_form.license.data) or None, tags_string=submit_form.tags.data, upload_limit=upload_limit, max_file_size=max_file_size, urlgen=request.urlgen) add_message(request, SUCCESS, _('Woohoo! Submitted!')) return redirect(request, "mediagoblin.user_pages.user_home", user=request.user.username) # Handle upload limit issues except FileUploadLimit: submit_form.file.errors.append( _(u'Sorry, the file size is too big.')) except UserUploadLimit: submit_form.file.errors.append( _('Sorry, uploading this file will put you over your' ' upload limit.')) except UserPastUploadLimit: messages.add_message( request, messages.WARNING, _('Sorry, you have reached your upload limit.')) return redirect(request, "mediagoblin.user_pages.user_home", user=request.user.username) except FileTypeNotSupported as e: submit_form.file.errors.append(e) except Exception as e: raise return render_to_response( request, 'mediagoblin/submit/start.html', {'submit_form': submit_form, 'app_config': mg_globals.app_config})
def submit_start(request): """ First view for submitting a file. """ upload_limit, max_file_size = get_upload_file_limits(request.user) submit_form = submit_forms.get_submit_start_form( request.form, license=request.user.license_preference, max_file_size=max_file_size, upload_limit=upload_limit, uploaded=request.user.uploaded) users_collections = Collection.query.filter_by( actor=request.user.id, type=Collection.USER_DEFINED_TYPE ).order_by(Collection.title) # Only show the Collections dropdown if the user has some # collections set up if users_collections.count() > 0: submit_form.collection.query = users_collections else: del submit_form.collection if request.method == 'POST' and submit_form.validate(): if not check_file_field(request, 'file'): submit_form.file.errors.append( _(u'You must provide a file.')) else: try: media = submit_media( mg_app=request.app, user=request.user, submitted_file=request.files['file'], filename=request.files['file'].filename, title=six.text_type(submit_form.title.data), description=six.text_type(submit_form.description.data), license=six.text_type(submit_form.license.data) or None, tags_string=submit_form.tags.data, upload_limit=upload_limit, max_file_size=max_file_size, urlgen=request.urlgen) if submit_form.collection and submit_form.collection.data: add_media_to_collection( submit_form.collection.data, media) create_activity( "add", media, request.user, target=submit_form.collection.data) messages.add_message( request, messages.SUCCESS, _('Woohoo! Submitted!')) return redirect(request, "mediagoblin.user_pages.user_home", user=request.user.username) # Handle upload limit issues except FileUploadLimit: submit_form.file.errors.append( _(u'Sorry, the file size is too big.')) except UserUploadLimit: submit_form.file.errors.append( _('Sorry, uploading this file will put you over your' ' upload limit.')) except UserPastUploadLimit: messages.add_message( request, messages.WARNING, _('Sorry, you have reached your upload limit.')) return redirect(request, "mediagoblin.user_pages.user_home", user=request.user.username) except FileTypeNotSupported as e: submit_form.file.errors.append(e) except Exception as e: raise return render_to_response( request, 'mediagoblin/submit/start.html', {'submit_form': submit_form, 'app_config': mg_globals.app_config})
def multi_submit_start(request): """ First view for submitting a file. """ submit_form = submit_forms.get_submit_start_form( request.form, license=request.user.license_preference) users_collections = Collection.query.filter_by( actor=request.user.id, type=Collection.USER_DEFINED_TYPE).order_by(Collection.title) if users_collections.count() > 0: submit_form.collection.query = users_collections else: del submit_form.collection # Below is what was used for mediagoblin 0.5.0-dev. Above is the new way. # submit_form = submit_forms.SubmitStartForm(request.form, license=request.user.license_preference) filecount = 0 if request.method == 'POST' and submit_form.validate(): if not check_file_field(request, 'file'): submit_form.file.errors.append( _(u'You must provide at least one file.')) else: for submitted_file in request.files.getlist('file'): try: if not submitted_file.filename: # MOST likely an invalid file continue # Skip the rest of the loop for this file else: filename = submitted_file.filename _log.info("html5-multi-upload: Got filename: %s" % filename) # If the filename contains non ascii generate a unique name if not all(ord(c) < 128 for c in filename): filename = str( uuid.uuid4()) + splitext(filename)[-1] # Sniff the submitted media to determine which # media plugin should handle processing media_type, media_manager = sniff_media( submitted_file, filename) # create entry and save in database entry = new_upload_entry(request.user) entry.media_type = str(media_type) entry.title = (str(submit_form.title.data) or str( splitext(submitted_file.filename)[0])) entry.description = str(submit_form.description.data) entry.license = str(submit_form.license.data) or None # Process the user's folksonomy "tags" entry.tags = convert_to_tag_list_of_dicts( submit_form.tags.data) # Generate a slug from the title entry.generate_slug() queue_file = prepare_queue_task( request.app, entry, filename) with queue_file: queue_file.write(submitted_file.stream.read()) # 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) if submit_form.collection and submit_form.collection.data: add_media_to_collection( submit_form.collection.data, entry) create_activity("add", entry, request.user, target=submit_form.collection.data) add_comment_subscription(request.user, entry) filecount = filecount + 1 except Exception as e: ''' This section is intended to catch exceptions raised in mediagoblin.media_types ''' if isinstance(e, TypeNotFound) or isinstance( e, FileTypeNotSupported): submit_form.file.errors.append(e) else: raise add_message(request, SUCCESS, _('Woohoo! Submitted %d Files!' % filecount)) return redirect(request, "mediagoblin.user_pages.user_home", user=request.user.username) return render_to_response(request, 'start.html', {'multi_submit_form': submit_form})
def submit_start(request): """ First view for submitting a file. """ submit_form = submit_forms.SubmitStartForm(request.form, license=request.user.license_preference) if request.method == 'POST' and submit_form.validate(): if not check_file_field(request, 'file'): submit_form.file.errors.append( _(u'You must provide a file.')) else: try: filename = request.files['file'].filename # If the filename contains non ascii generate a unique name if not all(ord(c) < 128 for c in filename): filename = unicode(uuid.uuid4()) + splitext(filename)[-1] # Sniff the submitted media to determine which # media plugin should handle processing media_type, media_manager = sniff_media( request.files['file']) # create entry and save in database entry = new_upload_entry(request.user) entry.media_type = unicode(media_type) entry.title = ( unicode(submit_form.title.data) or unicode(splitext(request.files['file'].filename)[0])) entry.description = unicode(submit_form.description.data) entry.license = unicode(submit_form.license.data) or None # Process the user's folksonomy "tags" entry.tags = convert_to_tag_list_of_dicts( submit_form.tags.data) # Generate a slug from the title entry.generate_slug() queue_file = prepare_queue_task(request.app, entry, filename) with queue_file: queue_file.write(request.files['file'].stream.read()) # 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!')) add_comment_subscription(request.user, entry) return redirect(request, "mediagoblin.user_pages.user_home", user=request.user.username) except Exception as e: ''' This section is intended to catch exceptions raised in mediagoblin.media_types ''' if isinstance(e, InvalidFileType) or \ isinstance(e, FileTypeNotSupported): submit_form.file.errors.append( e) else: raise return render_to_response( request, 'mediagoblin/submit/start.html', {'submit_form': submit_form, 'app_config': mg_globals.app_config})
def submit_start(request): """ First view for submitting a file. """ upload_limit, max_file_size = get_upload_file_limits(request.user) submit_form = submit_forms.get_submit_start_form( request.form, license=request.user.license_preference, max_file_size=max_file_size, upload_limit=upload_limit, uploaded=request.user.uploaded) if request.method == 'POST' and submit_form.validate(): if not check_file_field(request, 'file'): submit_form.file.errors.append(_(u'You must provide a file.')) else: try: submit_media(mg_app=request.app, user=request.user, submitted_file=request.files['file'], filename=request.files['file'].filename, title=unicode(submit_form.title.data), description=unicode(submit_form.description.data), license=unicode(submit_form.license.data) or None, tags_string=submit_form.tags.data, upload_limit=upload_limit, max_file_size=max_file_size, urlgen=request.urlgen) add_message(request, SUCCESS, _('Woohoo! Submitted!')) return redirect(request, "mediagoblin.user_pages.user_home", user=request.user.username) # Handle upload limit issues except FileUploadLimit: submit_form.file.errors.append( _(u'Sorry, the file size is too big.')) except UserUploadLimit: submit_form.file.errors.append( _('Sorry, uploading this file will put you over your' ' upload limit.')) except UserPastUploadLimit: messages.add_message( request, messages.WARNING, _('Sorry, you have reached your upload limit.')) return redirect(request, "mediagoblin.user_pages.user_home", user=request.user.username) except Exception as e: ''' This section is intended to catch exceptions raised in mediagoblin.media_types ''' if isinstance(e, InvalidFileType) or \ isinstance(e, FileTypeNotSupported): submit_form.file.errors.append(e) else: raise return render_to_response(request, 'mediagoblin/submit/start.html', { 'submit_form': submit_form, 'app_config': mg_globals.app_config })
def multi_submit_start(request): """ First view for submitting a file. """ submit_form = submit_forms.get_submit_start_form(request.form, license=request.user.license_preference) users_collections = Collection.query.filter_by( actor=request.user.id, type=Collection.USER_DEFINED_TYPE ).order_by(Collection.title) if users_collections.count() > 0: submit_form.collection.query = users_collections else: del submit_form.collection # Below is what was used for mediagoblin 0.5.0-dev. Above is the new way. # submit_form = submit_forms.SubmitStartForm(request.form, license=request.user.license_preference) filecount = 0 if request.method == 'POST' and submit_form.validate(): if not check_file_field(request, 'file'): submit_form.file.errors.append(_(u'You must provide at least one file.')) else: for submitted_file in request.files.getlist('file'): try: if not submitted_file.filename: # MOST likely an invalid file continue # Skip the rest of the loop for this file else: filename = submitted_file.filename _log.info("html5-multi-upload: Got filename: %s" % filename) # If the filename contains non ascii generate a unique name if not all(ord(c) < 128 for c in filename): filename = unicode(uuid.uuid4()) + splitext(filename)[-1] # Sniff the submitted media to determine which # media plugin should handle processing media_type, media_manager = sniff_media( submitted_file, filename) # create entry and save in database entry = new_upload_entry(request.user) entry.media_type = unicode(media_type) entry.title = ( unicode(submit_form.title.data) or unicode(splitext(submitted_file.filename)[0])) entry.description = unicode(submit_form.description.data) entry.license = unicode(submit_form.license.data) or None # Process the user's folksonomy "tags" entry.tags = convert_to_tag_list_of_dicts( submit_form.tags.data) # Generate a slug from the title entry.generate_slug() queue_file = prepare_queue_task(request.app, entry, filename) with queue_file: queue_file.write(submitted_file.stream.read()) # 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) if submit_form.collection and submit_form.collection.data: add_media_to_collection( submit_form.collection.data, entry) create_activity( "add", entry, request.user, target=submit_form.collection.data) add_comment_subscription(request.user, entry) filecount = filecount + 1 except Exception as e: ''' This section is intended to catch exceptions raised in mediagoblin.media_types ''' if isinstance(e, TypeNotFound) or isinstance(e, FileTypeNotSupported): submit_form.file.errors.append(e) else: raise add_message(request, SUCCESS, _('Woohoo! Submitted %d Files!' % filecount)) return redirect(request, "mediagoblin.user_pages.user_home", user=request.user.username) return render_to_response( request, 'start.html', {'multi_submit_form': submit_form})
def submit_start(request): """ First view for submitting a file. """ upload_limit, max_file_size = get_upload_file_limits(request.user) submit_form = submit_forms.get_submit_start_form( request.form, license=request.user.license_preference, max_file_size=max_file_size, upload_limit=upload_limit, uploaded=request.user.uploaded) users_collections = Collection.query.filter_by( actor=request.user.id, type=Collection.USER_DEFINED_TYPE ).order_by(Collection.title) # Only show the Collections dropdown if the user has some # collections set up if users_collections.count() > 0: submit_form.collection.query = users_collections else: del submit_form.collection if request.method == 'POST' and submit_form.validate(): if not check_file_field(request, 'file'): submit_form.file.errors.append( _('You must provide a file.')) else: try: media = submit_media( mg_app=request.app, user=request.user, submitted_file=request.files['file'], filename=request.files['file'].filename, title=str(submit_form.title.data), description=str(submit_form.description.data), license=str(submit_form.license.data) or None, tags_string=submit_form.tags.data, urlgen=request.urlgen) if submit_form.collection and submit_form.collection.data: add_media_to_collection( submit_form.collection.data, media) create_activity( "add", media, request.user, target=submit_form.collection.data) messages.add_message( request, messages.SUCCESS, _('Woohoo! Submitted!')) return redirect(request, "mediagoblin.user_pages.user_home", user=request.user.username) # Handle upload limit issues except FileUploadLimit: submit_form.file.errors.append( _('Sorry, the file size is too big.')) except UserUploadLimit: submit_form.file.errors.append( _('Sorry, uploading this file will put you over your' ' upload limit.')) except UserPastUploadLimit: messages.add_message( request, messages.WARNING, _('Sorry, you have reached your upload limit.')) return redirect(request, "mediagoblin.user_pages.user_home", user=request.user.username) except FileTypeNotSupported as e: submit_form.file.errors.append(e) except Exception as e: raise return render_to_response( request, 'mediagoblin/submit/start.html', {'submit_form': submit_form, 'app_config': mg_globals.app_config})
def submit_start(request): """ First view for submitting a file. """ user = request.user if user.upload_limit >= 0: upload_limit = user.upload_limit else: upload_limit = mg_globals.app_config.get('upload_limit', None) if upload_limit and user.uploaded >= upload_limit: messages.add_message( request, messages.WARNING, _('Sorry, you have reached your upload limit.')) return redirect(request, "mediagoblin.user_pages.user_home", user=request.user.username) max_file_size = mg_globals.app_config.get('max_file_size', None) submit_form = submit_forms.get_submit_start_form( request.form, license=request.user.license_preference, max_file_size=max_file_size, upload_limit=upload_limit, uploaded=user.uploaded) if request.method == 'POST' and submit_form.validate(): if not check_file_field(request, 'file'): submit_form.file.errors.append( _(u'You must provide a file.')) else: try: filename = request.files['file'].filename # If the filename contains non ascii generate a unique name if not all(ord(c) < 128 for c in filename): filename = unicode(uuid.uuid4()) + splitext(filename)[-1] # Sniff the submitted media to determine which # media plugin should handle processing media_type, media_manager = sniff_media( request.files['file']) # create entry and save in database entry = new_upload_entry(request.user) entry.media_type = unicode(media_type) entry.title = ( unicode(submit_form.title.data) or unicode(splitext(request.files['file'].filename)[0])) entry.description = unicode(submit_form.description.data) entry.license = unicode(submit_form.license.data) or None # Process the user's folksonomy "tags" entry.tags = convert_to_tag_list_of_dicts( submit_form.tags.data) # Generate a slug from the title entry.generate_slug() queue_file = prepare_queue_task(request.app, entry, filename) with queue_file: queue_file.write(request.files['file'].stream.read()) # Get file size and round to 2 decimal places file_size = request.app.queue_store.get_file_size( 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 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.')) 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) return redirect(request, "mediagoblin.user_pages.user_home", user=user.username) except Exception as e: ''' This section is intended to catch exceptions raised in mediagoblin.media_types ''' if isinstance(e, InvalidFileType) or \ isinstance(e, FileTypeNotSupported): submit_form.file.errors.append( e) else: raise return render_to_response( request, 'mediagoblin/submit/start.html', {'submit_form': submit_form, 'app_config': mg_globals.app_config})