summaryrefslogtreecommitdiff
path: root/matweb
diff options
context:
space:
mode:
authorJfriedli2021-03-23 21:20:54 +0100
committerJfriedli2021-03-23 21:20:54 +0100
commitc0d8d6c8eb74d839e520cd2e4429763208b478dd (patch)
tree2aa90ac6bb593a6a29ba357d3d840bcefcb644ad /matweb
parent501ecbf97e342c6d3a4c5d89131937f615cc98cf (diff)
catch newly thrown ValueErrors on get_file_parserv0.7.0
Diffstat (limited to '')
-rw-r--r--matweb/frontend.py10
-rw-r--r--matweb/rest_api.py74
2 files changed, 45 insertions, 39 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):