From f93a79346909e853a4b9f4c7626df1df9f3df79f Mon Sep 17 00:00:00 2001 From: Jan Friedli Date: Sun, 13 Sep 2020 16:56:02 +0200 Subject: handle not supported files in zips --- matweb/frontend.py | 33 +++++++++++++++++---------------- matweb/rest_api.py | 46 ++++++++++++++++++++++++---------------------- 2 files changed, 41 insertions(+), 38 deletions(-) (limited to 'matweb') diff --git a/matweb/frontend.py b/matweb/frontend.py index 48e4c19..7a3037d 100644 --- a/matweb/frontend.py +++ b/matweb/frontend.py @@ -71,22 +71,23 @@ def upload_file(): current_app.logger.error('Unsupported type %s', mime) return redirect(url_for('routes.upload_file')) - meta = parser.get_meta() - - if parser.remove_all() is not True: - flash('Unable to clean %s' % mime) - current_app.logger.error('Unable to clean %s', mime) - return redirect(url_for('routes.upload_file')) - - key, secret, meta_after, output_filename = utils.cleanup(parser, filepath, current_app.config['UPLOAD_FOLDER']) - - return render_template( - 'download.html', - mimetypes=mime_types, - meta=meta, - download_uri=url_for('routes.download_file', key=key, secret=secret, filename=output_filename), - meta_after=meta_after, - ) + try: + if parser.remove_all() is not True: + flash('Unable to clean %s' % mime) + current_app.logger.error('Unable to clean %s', mime) + return redirect(url_for('routes.upload_file')) + meta = parser.get_meta() + key, secret, meta_after, output_filename = utils.cleanup(parser, filepath, current_app.config['UPLOAD_FOLDER']) + + return render_template( + 'download.html', + mimetypes=mime_types, + meta=meta, + download_uri=url_for('routes.download_file', key=key, secret=secret, filename=output_filename), + meta_after=meta_after, + ) + except (RuntimeError, ValueError): + flash('The type %s could no be cleaned' % mime) max_file_size = int(current_app.config['MAX_CONTENT_LENGTH'] / 1024 / 1024) return render_template('index.html', max_file_size=max_file_size, mimetypes=mime_types) \ No newline at end of file diff --git a/matweb/rest_api.py b/matweb/rest_api.py index d3d9d8f..ef1a55a 100644 --- a/matweb/rest_api.py +++ b/matweb/rest_api.py @@ -46,29 +46,31 @@ class APIUpload(Resource): if parser is None: current_app.logger.error('Upload - Invalid mime type %s', mime) abort(415, message='The type %s is not supported' % mime) - - meta = parser.get_meta() - if not parser.remove_all(): + try: + if not parser.remove_all(): + raise ValueError() + meta = parser.get_meta() + key, secret, meta_after, output_filename = utils.cleanup(parser, filepath, + current_app.config['UPLOAD_FOLDER']) + return utils.return_file_created_response( + utils.get_file_removal_max_age_sec(), + output_filename, + mime, + key, + secret, + meta, + meta_after, + url_for( + 'api_bp.apidownload', + key=key, + secret=secret, + filename=output_filename, + _external=True + ) + ), 201 + except (ValueError, RuntimeError) as e: current_app.logger.error('Upload - Cleaning failed with mime: %s', mime) - abort(500, message='Unable to clean %s' % mime) - - key, secret, meta_after, output_filename = utils.cleanup(parser, filepath, current_app.config['UPLOAD_FOLDER']) - return utils.return_file_created_response( - utils.get_file_removal_max_age_sec(), - output_filename, - mime, - key, - secret, - meta, - meta_after, - url_for( - 'api_bp.apidownload', - key=key, - secret=secret, - filename=output_filename, - _external=True - ) - ), 201 + abort(400, message='Unable to clean %s' % mime) class APIDownload(Resource): -- cgit v1.3