From 2ef2f46e73845dcd55666cad49c5a17908bf5b46 Mon Sep 17 00:00:00 2001 From: Elrond Date: Fri, 22 Mar 2013 15:45:21 +0100 Subject: [PATCH] Refactor file field checking. When uploading, the file field needs some checks, it seems. So refactor them into check_file_field and use around. --- mediagoblin/plugins/api/views.py | 8 +++----- mediagoblin/submit/lib.py | 11 +++++++++++ mediagoblin/submit/views.py | 8 +++----- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/mediagoblin/plugins/api/views.py b/mediagoblin/plugins/api/views.py index 2055a663..fde76fe4 100644 --- a/mediagoblin/plugins/api/views.py +++ b/mediagoblin/plugins/api/views.py @@ -18,7 +18,6 @@ import json import logging from os.path import splitext -from werkzeug.datastructures import FileStorage from werkzeug.exceptions import BadRequest, Forbidden from werkzeug.wrappers import Response @@ -27,7 +26,8 @@ from mediagoblin.meddleware.csrf import csrf_exempt from mediagoblin.media_types import sniff_media from mediagoblin.plugins.api.tools import api_auth, get_entry_serializable, \ json_response -from mediagoblin.submit.lib import prepare_queue_task, run_process_media +from mediagoblin.submit.lib import check_file_field, prepare_queue_task, \ + run_process_media _log = logging.getLogger(__name__) @@ -45,9 +45,7 @@ def post_entry(request): _log.debug('Must POST against post_entry') raise BadRequest() - if not 'file' in request.files \ - or not isinstance(request.files['file'], FileStorage) \ - or not request.files['file'].stream: + if not check_file_field(request, 'file'): _log.debug('File field not found') raise BadRequest() diff --git a/mediagoblin/submit/lib.py b/mediagoblin/submit/lib.py index 679fc543..a5483471 100644 --- a/mediagoblin/submit/lib.py +++ b/mediagoblin/submit/lib.py @@ -17,6 +17,7 @@ import logging import uuid from werkzeug.utils import secure_filename +from werkzeug.datastructures import FileStorage from mediagoblin.processing import mark_entry_failed from mediagoblin.processing.task import process_media @@ -25,6 +26,16 @@ from mediagoblin.processing.task import process_media _log = logging.getLogger(__name__) +def check_file_field(request, field_name): + """Check if a file field meets minimal criteria""" + retval = (field_name in request.files + and isinstance(request.files[field_name], FileStorage) + and request.files[field_name].stream) + if not retval: + _log.debug("Form did not contain proper file field %s", field_name) + return retval + + def prepare_queue_task(app, entry, filename): """ Prepare a MediaEntry for the processing queue and get a queue file diff --git a/mediagoblin/submit/views.py b/mediagoblin/submit/views.py index def7e839..1e47d259 100644 --- a/mediagoblin/submit/views.py +++ b/mediagoblin/submit/views.py @@ -22,7 +22,6 @@ import logging _log = logging.getLogger(__name__) -from werkzeug.datastructures import FileStorage from mediagoblin.tools.text import convert_to_tag_list_of_dicts from mediagoblin.tools.translate import pass_to_ugettext as _ @@ -32,7 +31,8 @@ from mediagoblin.submit import forms as submit_forms from mediagoblin.messages import add_message, SUCCESS from mediagoblin.media_types import sniff_media, \ InvalidFileType, FileTypeNotSupported -from mediagoblin.submit.lib import run_process_media, prepare_queue_task +from mediagoblin.submit.lib import check_file_field, prepare_queue_task, \ + run_process_media @require_active_login @@ -44,9 +44,7 @@ def submit_start(request): license=request.user.license_preference) if request.method == 'POST' and submit_form.validate(): - if not ('file' in request.files - and isinstance(request.files['file'], FileStorage) - and request.files['file'].stream): + if not check_file_field(request, 'file'): submit_form.file.errors.append( _(u'You must provide a file.')) else: -- 2.25.1