mirror of
https://0xacab.org/jvoisin/mat2-web.git
synced 2025-05-12 03:05:19 -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')
|
||||
return redirect(request.url)
|
||||
|
||||
try:
|
||||
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)
|
||||
except ValueError:
|
||||
flash('The filetype is not supported')
|
||||
current_app.logger.error('Unsupported filetype',)
|
||||
return redirect(url_for('routes.upload_file'))
|
||||
|
||||
try:
|
||||
|
|
|
@ -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')
|
||||
|
||||
try:
|
||||
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:
|
||||
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,10 +181,9 @@ class APIBulkDownloadCreator(Resource):
|
|||
current_app.logger.error('BulkDownload - Validating Zip failed: %s', e)
|
||||
abort(400, message='Validating Zip failed')
|
||||
|
||||
try:
|
||||
parser, mime = utils.get_file_parser(zip_path)
|
||||
if not parser.remove_all():
|
||||
current_app.logger.error('BulkDownload - Unable to clean Zip')
|
||||
abort(500, message='Unable to clean %s' % mime)
|
||||
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(),
|
||||
|
@ -200,6 +200,12 @@ class APIBulkDownloadCreator(Resource):
|
|||
_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)
|
||||
|
||||
|
||||
class APISupportedExtensions(Resource):
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue