handle not supported files in zips

This commit is contained in:
Jan Friedli 2020-09-13 16:56:02 +02:00
parent 493626e195
commit f93a793469
No known key found for this signature in database
GPG key ID: F945FA2FCA30549D
3 changed files with 41 additions and 37 deletions

View file

@ -71,13 +71,12 @@ def upload_file():
current_app.logger.error('Unsupported type %s', mime) current_app.logger.error('Unsupported type %s', mime)
return redirect(url_for('routes.upload_file')) return redirect(url_for('routes.upload_file'))
meta = parser.get_meta() try:
if parser.remove_all() is not True: if parser.remove_all() is not True:
flash('Unable to clean %s' % mime) flash('Unable to clean %s' % mime)
current_app.logger.error('Unable to clean %s', mime) current_app.logger.error('Unable to clean %s', mime)
return redirect(url_for('routes.upload_file')) 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']) key, secret, meta_after, output_filename = utils.cleanup(parser, filepath, current_app.config['UPLOAD_FOLDER'])
return render_template( return render_template(
@ -87,6 +86,8 @@ def upload_file():
download_uri=url_for('routes.download_file', key=key, secret=secret, filename=output_filename), download_uri=url_for('routes.download_file', key=key, secret=secret, filename=output_filename),
meta_after=meta_after, 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) 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) return render_template('index.html', max_file_size=max_file_size, mimetypes=mime_types)

View file

@ -46,13 +46,12 @@ class APIUpload(Resource):
if parser is None: if parser is None:
current_app.logger.error('Upload - Invalid mime type %s', mime) current_app.logger.error('Upload - Invalid mime type %s', mime)
abort(415, message='The type %s is not supported' % mime) abort(415, message='The type %s is not supported' % mime)
try:
meta = parser.get_meta()
if not parser.remove_all(): if not parser.remove_all():
current_app.logger.error('Upload - Cleaning failed with mime: %s', mime) raise ValueError()
abort(500, message='Unable to clean %s' % mime) meta = parser.get_meta()
key, secret, meta_after, output_filename = utils.cleanup(parser, filepath,
key, secret, meta_after, output_filename = utils.cleanup(parser, filepath, current_app.config['UPLOAD_FOLDER']) current_app.config['UPLOAD_FOLDER'])
return utils.return_file_created_response( return utils.return_file_created_response(
utils.get_file_removal_max_age_sec(), utils.get_file_removal_max_age_sec(),
output_filename, output_filename,
@ -69,6 +68,9 @@ class APIUpload(Resource):
_external=True _external=True
) )
), 201 ), 201
except (ValueError, RuntimeError) as e:
current_app.logger.error('Upload - Cleaning failed with mime: %s', mime)
abort(400, message='Unable to clean %s' % mime)
class APIDownload(Resource): class APIDownload(Resource):

View file

@ -135,6 +135,7 @@ class Mat2APITestCase(unittest.TestCase):
headers={'content-type': 'application/json'} headers={'content-type': 'application/json'}
) )
error = request.get_json()['message'] error = request.get_json()['message']
self.assertEqual(request.status_code, 400)
self.assertEqual(error, 'Unable to clean application/zip') self.assertEqual(error, 'Unable to clean application/zip')
def test_api_download(self): def test_api_download(self):