catch newly thrown ValueErrors on get_file_parser

This commit is contained in:
Jfriedli 2021-03-23 21:20:54 +01:00
parent 501ecbf97e
commit c0d8d6c8eb
4 changed files with 48 additions and 42 deletions

View file

@ -64,11 +64,11 @@ def upload_file():
current_app.logger.error('Invalid Filename in upload')
return redirect(request.url)
parser, mime = utils.get_file_parser(filepath)
if parser is None:
flash('The type %s is not supported' % mime)
current_app.logger.error('Unsupported type %s', mime)
try:
parser, mime = utils.get_file_parser(filepath)
except ValueError:
flash('The filetype is not supported')
current_app.logger.error('Unsupported filetype',)
return redirect(url_for('routes.upload_file'))
try:

View file

@ -41,14 +41,11 @@ class APIUpload(Resource):
current_app.logger.error('Upload - Invalid file name')
abort(400, message='Invalid Filename')
parser, mime = utils.get_file_parser(filepath)
if parser is None:
current_app.logger.error('Upload - Invalid mime type %s', mime)
abort(415, message='The type %s is not supported' % mime)
try:
parser, mime = utils.get_file_parser(filepath)
if not parser.remove_all():
raise ValueError()
current_app.logger.error('Upload - Cleaning failed with mime: %s', mime)
abort(400, message='Unable to clean %s' % mime)
meta = parser.get_meta()
key, secret, meta_after, output_filename = utils.cleanup(parser, filepath,
current_app.config['UPLOAD_FOLDER'])
@ -68,7 +65,10 @@ class APIUpload(Resource):
_external=True
)
), 201
except (ValueError, RuntimeError):
except ValueError:
current_app.logger.error('Upload - Invalid mime type')
abort(415, message='The filetype is not supported')
except RuntimeError:
current_app.logger.error('Upload - Cleaning failed with mime: %s', mime)
abort(400, message='Unable to clean %s' % mime)
@ -107,18 +107,19 @@ class APIClean(Resource):
current_app.logger.error('Clean - Invalid Filename')
abort(400, message='Invalid Filename')
parser, mime = utils.get_file_parser(filepath)
if parser is None:
current_app.logger.error('Clean - The type %s is not supported', mime)
abort(415, message='The type %s is not supported' % mime)
if parser.remove_all() is not True:
try:
parser, mime = utils.get_file_parser(filepath)
if parser is None:
raise ValueError()
parser.remove_all()
_, _, _, output_filename = utils.cleanup(parser, filepath, current_app.config['UPLOAD_FOLDER'])
except ValueError:
current_app.logger.error('Upload - Invalid mime type')
abort(415, message='The filetype is not supported')
except RuntimeError:
current_app.logger.error('Clean - Unable to clean %s', mime)
abort(500, message='Unable to clean %s' % mime)
_, _, _, output_filename = utils.cleanup(parser, filepath, current_app.config['UPLOAD_FOLDER'])
@after_this_request
def remove_file(response):
os.remove(os.path.join(current_app.config['UPLOAD_FOLDER'], output_filename))
@ -180,26 +181,31 @@ class APIBulkDownloadCreator(Resource):
current_app.logger.error('BulkDownload - Validating Zip failed: %s', e)
abort(400, message='Validating Zip failed')
parser, mime = utils.get_file_parser(zip_path)
if not parser.remove_all():
try:
parser, mime = utils.get_file_parser(zip_path)
parser.remove_all()
key, secret, meta_after, output_filename = utils.cleanup(parser, zip_path, current_app.config['UPLOAD_FOLDER'])
return {
'inactive_after_sec': utils.get_file_removal_max_age_sec(),
'output_filename': output_filename,
'mime': mime,
'key': key,
'secret': secret,
'meta_after': meta_after,
'download_link': url_for(
'api_bp.apidownload',
key=key,
secret=secret,
filename=output_filename,
_external=True
)
}, 201
except ValueError:
current_app.logger.error('BulkDownload - Invalid mime type')
abort(415, message='The filetype is not supported')
except RuntimeError:
current_app.logger.error('BulkDownload - Unable to clean Zip')
abort(500, message='Unable to clean %s' % mime)
key, secret, meta_after, output_filename = utils.cleanup(parser, zip_path, current_app.config['UPLOAD_FOLDER'])
return {
'inactive_after_sec': utils.get_file_removal_max_age_sec(),
'output_filename': output_filename,
'mime': mime,
'key': key,
'secret': secret,
'meta_after': meta_after,
'download_link': url_for(
'api_bp.apidownload',
key=key,
secret=secret,
filename=output_filename,
_external=True
)
}, 201
class APISupportedExtensions(Resource):

View file

@ -63,7 +63,7 @@ class Mat2WebTestCase(TestCase):
data=dict(
file=(io.BytesIO(b""), 'test.pdf'),
), follow_redirects=True)
self.assertIn(b'The type application/pdf is not supported',
self.assertIn(b'The filetype is not supported',
rv.data)
self.assertEqual(rv.status_code, 200)

View file

@ -86,7 +86,7 @@ class Mat2APITestCase(unittest.TestCase):
self.assertEqual(request.status_code, 415)
error = request.get_json()['message']
self.assertEqual(error, 'The type application/pdf is not supported')
self.assertEqual(error, 'The filetype is not supported')
def test_api_supported_extensions(self):
rv = self.app.get('/api/extension')
@ -487,7 +487,7 @@ class Mat2APITestCase(unittest.TestCase):
),
follow_redirects=False
)
self.assertEqual(r.get_json()['message'], 'The type None is not supported')
self.assertEqual(r.get_json()['message'], 'The filetype is not supported')
self.assertEqual(r.status_code, 415)