diff options
| author | Jan Friedli | 2020-08-21 09:09:19 +0200 |
|---|---|---|
| committer | Jan Friedli | 2020-08-21 09:09:19 +0200 |
| commit | 668451751411b0d43ecc0791c6524e7e9a0cd21e (patch) | |
| tree | f1010f427b258121998bf46477bf6bbbba602df6 /matweb/rest_api.py | |
| parent | 5b9bd99eb7e90b29022a2c0466ee6b6dbaf0b9ac (diff) | |
added endpoint to clean file and return it directly for automated clients
Diffstat (limited to 'matweb/rest_api.py')
| -rw-r--r-- | matweb/rest_api.py | 36 |
1 files changed, 36 insertions, 0 deletions
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): | |||
| 84 | return send_from_directory(current_app.config['UPLOAD_FOLDER'], filepath, as_attachment=True) | 84 | return send_from_directory(current_app.config['UPLOAD_FOLDER'], filepath, as_attachment=True) |
| 85 | 85 | ||
| 86 | 86 | ||
| 87 | class APIClean(Resource): | ||
| 88 | @swag_from('./oas/remove_metadata.yml') | ||
| 89 | def post(self): | ||
| 90 | if 'file' not in request.files: | ||
| 91 | abort(400, message='No file part') | ||
| 92 | |||
| 93 | uploaded_file = request.files['file'] | ||
| 94 | if not uploaded_file.filename: | ||
| 95 | abort(400, message='No selected `file`') | ||
| 96 | try: | ||
| 97 | filename, filepath = utils.save_file(uploaded_file, current_app.config['UPLOAD_FOLDER']) | ||
| 98 | except ValueError: | ||
| 99 | abort(400, message='Invalid Filename') | ||
| 100 | |||
| 101 | parser, mime = utils.get_file_parser(filepath) | ||
| 102 | |||
| 103 | if parser is None: | ||
| 104 | abort(415, message='The type %s is not supported' % mime) | ||
| 105 | |||
| 106 | if parser.remove_all() is not True: | ||
| 107 | abort(500, message='Unable to clean %s' % mime) | ||
| 108 | |||
| 109 | _, _, _, output_filename = utils.cleanup(parser, filepath, current_app.config['UPLOAD_FOLDER']) | ||
| 110 | |||
| 111 | @after_this_request | ||
| 112 | def remove_file(response): | ||
| 113 | os.remove(os.path.join(current_app.config['UPLOAD_FOLDER'], output_filename)) | ||
| 114 | return response | ||
| 115 | |||
| 116 | return send_from_directory(current_app.config['UPLOAD_FOLDER'], output_filename, as_attachment=True) | ||
| 117 | |||
| 118 | |||
| 87 | class APIBulkDownloadCreator(Resource): | 119 | class APIBulkDownloadCreator(Resource): |
| 88 | schema = { | 120 | schema = { |
| 89 | 'download_list': { | 121 | 'download_list': { |
| @@ -169,6 +201,10 @@ api.add_resource( | |||
| 169 | '/download/<string:key>/<string:secret>/<string:filename>' | 201 | '/download/<string:key>/<string:secret>/<string:filename>' |
| 170 | ) | 202 | ) |
| 171 | api.add_resource( | 203 | api.add_resource( |
| 204 | APIClean, | ||
| 205 | '/remove_metadata' | ||
| 206 | ) | ||
| 207 | api.add_resource( | ||
| 172 | APIBulkDownloadCreator, | 208 | APIBulkDownloadCreator, |
| 173 | '/download/bulk' | 209 | '/download/bulk' |
| 174 | ) | 210 | ) |
