summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--matweb/frontend.py10
-rw-r--r--matweb/rest_api.py74
-rw-r--r--test/test.py2
-rw-r--r--test/test_api.py4
4 files changed, 48 insertions, 42 deletions
diff --git a/matweb/frontend.py b/matweb/frontend.py
index d29a2fb..395606f 100644
--- a/matweb/frontend.py
+++ b/matweb/frontend.py
@@ -64,11 +64,11 @@ def upload_file():
64 current_app.logger.error('Invalid Filename in upload') 64 current_app.logger.error('Invalid Filename in upload')
65 return redirect(request.url) 65 return redirect(request.url)
66 66
67 parser, mime = utils.get_file_parser(filepath) 67 try:
68 68 parser, mime = utils.get_file_parser(filepath)
69 if parser is None: 69 except ValueError:
70 flash('The type %s is not supported' % mime) 70 flash('The filetype is not supported')
71 current_app.logger.error('Unsupported type %s', mime) 71 current_app.logger.error('Unsupported filetype',)
72 return redirect(url_for('routes.upload_file')) 72 return redirect(url_for('routes.upload_file'))
73 73
74 try: 74 try:
diff --git a/matweb/rest_api.py b/matweb/rest_api.py
index c8edead..d44d838 100644
--- a/matweb/rest_api.py
+++ b/matweb/rest_api.py
@@ -41,14 +41,11 @@ class APIUpload(Resource):
41 current_app.logger.error('Upload - Invalid file name') 41 current_app.logger.error('Upload - Invalid file name')
42 abort(400, message='Invalid Filename') 42 abort(400, message='Invalid Filename')
43 43
44 parser, mime = utils.get_file_parser(filepath)
45
46 if parser is None:
47 current_app.logger.error('Upload - Invalid mime type %s', mime)
48 abort(415, message='The type %s is not supported' % mime)
49 try: 44 try:
45 parser, mime = utils.get_file_parser(filepath)
50 if not parser.remove_all(): 46 if not parser.remove_all():
51 raise ValueError() 47 current_app.logger.error('Upload - Cleaning failed with mime: %s', mime)
48 abort(400, message='Unable to clean %s' % mime)
52 meta = parser.get_meta() 49 meta = parser.get_meta()
53 key, secret, meta_after, output_filename = utils.cleanup(parser, filepath, 50 key, secret, meta_after, output_filename = utils.cleanup(parser, filepath,
54 current_app.config['UPLOAD_FOLDER']) 51 current_app.config['UPLOAD_FOLDER'])
@@ -68,7 +65,10 @@ class APIUpload(Resource):
68 _external=True 65 _external=True
69 ) 66 )
70 ), 201 67 ), 201
71 except (ValueError, RuntimeError): 68 except ValueError:
69 current_app.logger.error('Upload - Invalid mime type')
70 abort(415, message='The filetype is not supported')
71 except RuntimeError:
72 current_app.logger.error('Upload - Cleaning failed with mime: %s', mime) 72 current_app.logger.error('Upload - Cleaning failed with mime: %s', mime)
73 abort(400, message='Unable to clean %s' % mime) 73 abort(400, message='Unable to clean %s' % mime)
74 74
@@ -107,18 +107,19 @@ class APIClean(Resource):
107 current_app.logger.error('Clean - Invalid Filename') 107 current_app.logger.error('Clean - Invalid Filename')
108 abort(400, message='Invalid Filename') 108 abort(400, message='Invalid Filename')
109 109
110 parser, mime = utils.get_file_parser(filepath) 110 try:
111 111 parser, mime = utils.get_file_parser(filepath)
112 if parser is None: 112 if parser is None:
113 current_app.logger.error('Clean - The type %s is not supported', mime) 113 raise ValueError()
114 abort(415, message='The type %s is not supported' % mime) 114 parser.remove_all()
115 115 _, _, _, output_filename = utils.cleanup(parser, filepath, current_app.config['UPLOAD_FOLDER'])
116 if parser.remove_all() is not True: 116 except ValueError:
117 current_app.logger.error('Upload - Invalid mime type')
118 abort(415, message='The filetype is not supported')
119 except RuntimeError:
117 current_app.logger.error('Clean - Unable to clean %s', mime) 120 current_app.logger.error('Clean - Unable to clean %s', mime)
118 abort(500, message='Unable to clean %s' % mime) 121 abort(500, message='Unable to clean %s' % mime)
119 122
120 _, _, _, output_filename = utils.cleanup(parser, filepath, current_app.config['UPLOAD_FOLDER'])
121
122 @after_this_request 123 @after_this_request
123 def remove_file(response): 124 def remove_file(response):
124 os.remove(os.path.join(current_app.config['UPLOAD_FOLDER'], output_filename)) 125 os.remove(os.path.join(current_app.config['UPLOAD_FOLDER'], output_filename))
@@ -180,26 +181,31 @@ class APIBulkDownloadCreator(Resource):
180 current_app.logger.error('BulkDownload - Validating Zip failed: %s', e) 181 current_app.logger.error('BulkDownload - Validating Zip failed: %s', e)
181 abort(400, message='Validating Zip failed') 182 abort(400, message='Validating Zip failed')
182 183
183 parser, mime = utils.get_file_parser(zip_path) 184 try:
184 if not parser.remove_all(): 185 parser, mime = utils.get_file_parser(zip_path)
186 parser.remove_all()
187 key, secret, meta_after, output_filename = utils.cleanup(parser, zip_path, current_app.config['UPLOAD_FOLDER'])
188 return {
189 'inactive_after_sec': utils.get_file_removal_max_age_sec(),
190 'output_filename': output_filename,
191 'mime': mime,
192 'key': key,
193 'secret': secret,
194 'meta_after': meta_after,
195 'download_link': url_for(
196 'api_bp.apidownload',
197 key=key,
198 secret=secret,
199 filename=output_filename,
200 _external=True
201 )
202 }, 201
203 except ValueError:
204 current_app.logger.error('BulkDownload - Invalid mime type')
205 abort(415, message='The filetype is not supported')
206 except RuntimeError:
185 current_app.logger.error('BulkDownload - Unable to clean Zip') 207 current_app.logger.error('BulkDownload - Unable to clean Zip')
186 abort(500, message='Unable to clean %s' % mime) 208 abort(500, message='Unable to clean %s' % mime)
187 key, secret, meta_after, output_filename = utils.cleanup(parser, zip_path, current_app.config['UPLOAD_FOLDER'])
188 return {
189 'inactive_after_sec': utils.get_file_removal_max_age_sec(),
190 'output_filename': output_filename,
191 'mime': mime,
192 'key': key,
193 'secret': secret,
194 'meta_after': meta_after,
195 'download_link': url_for(
196 'api_bp.apidownload',
197 key=key,
198 secret=secret,
199 filename=output_filename,
200 _external=True
201 )
202 }, 201
203 209
204 210
205class APISupportedExtensions(Resource): 211class APISupportedExtensions(Resource):
diff --git a/test/test.py b/test/test.py
index 3666457..d9d0fbf 100644
--- a/test/test.py
+++ b/test/test.py
@@ -63,7 +63,7 @@ class Mat2WebTestCase(TestCase):
63 data=dict( 63 data=dict(
64 file=(io.BytesIO(b""), 'test.pdf'), 64 file=(io.BytesIO(b""), 'test.pdf'),
65 ), follow_redirects=True) 65 ), follow_redirects=True)
66 self.assertIn(b'The type application/pdf is not supported', 66 self.assertIn(b'The filetype is not supported',
67 rv.data) 67 rv.data)
68 self.assertEqual(rv.status_code, 200) 68 self.assertEqual(rv.status_code, 200)
69 69
diff --git a/test/test_api.py b/test/test_api.py
index ad2525e..8be165e 100644
--- a/test/test_api.py
+++ b/test/test_api.py
@@ -86,7 +86,7 @@ class Mat2APITestCase(unittest.TestCase):
86 self.assertEqual(request.status_code, 415) 86 self.assertEqual(request.status_code, 415)
87 87
88 error = request.get_json()['message'] 88 error = request.get_json()['message']
89 self.assertEqual(error, 'The type application/pdf is not supported') 89 self.assertEqual(error, 'The filetype is not supported')
90 90
91 def test_api_supported_extensions(self): 91 def test_api_supported_extensions(self):
92 rv = self.app.get('/api/extension') 92 rv = self.app.get('/api/extension')
@@ -487,7 +487,7 @@ class Mat2APITestCase(unittest.TestCase):
487 ), 487 ),
488 follow_redirects=False 488 follow_redirects=False
489 ) 489 )
490 self.assertEqual(r.get_json()['message'], 'The type None is not supported') 490 self.assertEqual(r.get_json()['message'], 'The filetype is not supported')
491 self.assertEqual(r.status_code, 415) 491 self.assertEqual(r.status_code, 415)
492 492
493 493