mirror of
https://0xacab.org/jvoisin/mat2-web.git
synced 2025-05-12 11:12:17 -04:00
catch newly thrown ValueErrors on get_file_parser
This commit is contained in:
parent
501ecbf97e
commit
c0d8d6c8eb
4 changed files with 48 additions and 42 deletions
|
@ -64,11 +64,11 @@ def upload_file():
|
||||||
current_app.logger.error('Invalid Filename in upload')
|
current_app.logger.error('Invalid Filename in upload')
|
||||||
return redirect(request.url)
|
return redirect(request.url)
|
||||||
|
|
||||||
|
try:
|
||||||
parser, mime = utils.get_file_parser(filepath)
|
parser, mime = utils.get_file_parser(filepath)
|
||||||
|
except ValueError:
|
||||||
if parser is None:
|
flash('The filetype is not supported')
|
||||||
flash('The type %s is not supported' % mime)
|
current_app.logger.error('Unsupported filetype',)
|
||||||
current_app.logger.error('Unsupported type %s', mime)
|
|
||||||
return redirect(url_for('routes.upload_file'))
|
return redirect(url_for('routes.upload_file'))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -41,14 +41,11 @@ class APIUpload(Resource):
|
||||||
current_app.logger.error('Upload - Invalid file name')
|
current_app.logger.error('Upload - Invalid file name')
|
||||||
abort(400, message='Invalid Filename')
|
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:
|
try:
|
||||||
|
parser, mime = utils.get_file_parser(filepath)
|
||||||
if not parser.remove_all():
|
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()
|
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'])
|
||||||
|
@ -68,7 +65,10 @@ class APIUpload(Resource):
|
||||||
_external=True
|
_external=True
|
||||||
)
|
)
|
||||||
), 201
|
), 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)
|
current_app.logger.error('Upload - Cleaning failed with mime: %s', mime)
|
||||||
abort(400, message='Unable to clean %s' % mime)
|
abort(400, message='Unable to clean %s' % mime)
|
||||||
|
|
||||||
|
@ -107,18 +107,19 @@ class APIClean(Resource):
|
||||||
current_app.logger.error('Clean - Invalid Filename')
|
current_app.logger.error('Clean - Invalid Filename')
|
||||||
abort(400, message='Invalid Filename')
|
abort(400, message='Invalid Filename')
|
||||||
|
|
||||||
|
try:
|
||||||
parser, mime = utils.get_file_parser(filepath)
|
parser, mime = utils.get_file_parser(filepath)
|
||||||
|
|
||||||
if parser is None:
|
if parser is None:
|
||||||
current_app.logger.error('Clean - The type %s is not supported', mime)
|
raise ValueError()
|
||||||
abort(415, message='The type %s is not supported' % mime)
|
parser.remove_all()
|
||||||
|
_, _, _, output_filename = utils.cleanup(parser, filepath, current_app.config['UPLOAD_FOLDER'])
|
||||||
if parser.remove_all() is not True:
|
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)
|
current_app.logger.error('Clean - Unable to clean %s', mime)
|
||||||
abort(500, message='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
|
@after_this_request
|
||||||
def remove_file(response):
|
def remove_file(response):
|
||||||
os.remove(os.path.join(current_app.config['UPLOAD_FOLDER'], output_filename))
|
os.remove(os.path.join(current_app.config['UPLOAD_FOLDER'], output_filename))
|
||||||
|
@ -180,10 +181,9 @@ class APIBulkDownloadCreator(Resource):
|
||||||
current_app.logger.error('BulkDownload - Validating Zip failed: %s', e)
|
current_app.logger.error('BulkDownload - Validating Zip failed: %s', e)
|
||||||
abort(400, message='Validating Zip failed')
|
abort(400, message='Validating Zip failed')
|
||||||
|
|
||||||
|
try:
|
||||||
parser, mime = utils.get_file_parser(zip_path)
|
parser, mime = utils.get_file_parser(zip_path)
|
||||||
if not parser.remove_all():
|
parser.remove_all()
|
||||||
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'])
|
key, secret, meta_after, output_filename = utils.cleanup(parser, zip_path, current_app.config['UPLOAD_FOLDER'])
|
||||||
return {
|
return {
|
||||||
'inactive_after_sec': utils.get_file_removal_max_age_sec(),
|
'inactive_after_sec': utils.get_file_removal_max_age_sec(),
|
||||||
|
@ -200,6 +200,12 @@ class APIBulkDownloadCreator(Resource):
|
||||||
_external=True
|
_external=True
|
||||||
)
|
)
|
||||||
}, 201
|
}, 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)
|
||||||
|
|
||||||
|
|
||||||
class APISupportedExtensions(Resource):
|
class APISupportedExtensions(Resource):
|
||||||
|
|
|
@ -63,7 +63,7 @@ class Mat2WebTestCase(TestCase):
|
||||||
data=dict(
|
data=dict(
|
||||||
file=(io.BytesIO(b""), 'test.pdf'),
|
file=(io.BytesIO(b""), 'test.pdf'),
|
||||||
), follow_redirects=True)
|
), follow_redirects=True)
|
||||||
self.assertIn(b'The type application/pdf is not supported',
|
self.assertIn(b'The filetype is not supported',
|
||||||
rv.data)
|
rv.data)
|
||||||
self.assertEqual(rv.status_code, 200)
|
self.assertEqual(rv.status_code, 200)
|
||||||
|
|
||||||
|
|
|
@ -86,7 +86,7 @@ class Mat2APITestCase(unittest.TestCase):
|
||||||
self.assertEqual(request.status_code, 415)
|
self.assertEqual(request.status_code, 415)
|
||||||
|
|
||||||
error = request.get_json()['message']
|
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):
|
def test_api_supported_extensions(self):
|
||||||
rv = self.app.get('/api/extension')
|
rv = self.app.get('/api/extension')
|
||||||
|
@ -487,7 +487,7 @@ class Mat2APITestCase(unittest.TestCase):
|
||||||
),
|
),
|
||||||
follow_redirects=False
|
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)
|
self.assertEqual(r.status_code, 415)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue