From 668451751411b0d43ecc0791c6524e7e9a0cd21e Mon Sep 17 00:00:00 2001 From: Jan Friedli Date: Fri, 21 Aug 2020 09:09:19 +0200 Subject: added endpoint to clean file and return it directly for automated clients --- matweb/rest_api.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'matweb/rest_api.py') diff --git a/matweb/rest_api.py b/matweb/rest_api.py index 2909cf6..50b7c37 100644 --- a/matweb/rest_api.py +++ b/matweb/rest_api.py @@ -84,6 +84,38 @@ class APIDownload(Resource): return send_from_directory(current_app.config['UPLOAD_FOLDER'], filepath, as_attachment=True) +class APIClean(Resource): + @swag_from('./oas/remove_metadata.yml') + def post(self): + if 'file' not in request.files: + abort(400, message='No file part') + + uploaded_file = request.files['file'] + if not uploaded_file.filename: + abort(400, message='No selected `file`') + try: + filename, filepath = utils.save_file(uploaded_file, current_app.config['UPLOAD_FOLDER']) + except ValueError: + abort(400, message='Invalid Filename') + + parser, mime = utils.get_file_parser(filepath) + + if parser is None: + abort(415, message='The type %s is not supported' % mime) + + if parser.remove_all() is not True: + 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)) + return response + + return send_from_directory(current_app.config['UPLOAD_FOLDER'], output_filename, as_attachment=True) + + class APIBulkDownloadCreator(Resource): schema = { 'download_list': { @@ -168,6 +200,10 @@ api.add_resource( APIDownload, '/download///' ) +api.add_resource( + APIClean, + '/remove_metadata' +) api.add_resource( APIBulkDownloadCreator, '/download/bulk' -- cgit v1.3