diff options
| author | jfriedli | 2020-04-23 10:39:35 -0700 |
|---|---|---|
| committer | jfriedli | 2020-04-23 10:39:35 -0700 |
| commit | e1bac8b6a7fd857f38b7bcb678398c82baaa8fd5 (patch) | |
| tree | fa87e526289e455f2f17b86973d08eb6850e721f /matweb/utils.py | |
| parent | d14988fa3fa97f549fb8eaf601cb2c687cdce143 (diff) | |
Refactoring
Diffstat (limited to '')
| -rw-r--r-- | matweb/utils.py | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/matweb/utils.py b/matweb/utils.py new file mode 100644 index 0000000..8dfff45 --- /dev/null +++ b/matweb/utils.py | |||
| @@ -0,0 +1,91 @@ | |||
| 1 | import hmac | ||
| 2 | import os | ||
| 3 | import hashlib | ||
| 4 | import mimetypes as mtype | ||
| 5 | |||
| 6 | from flask_restful import abort | ||
| 7 | from libmat2 import parser_factory | ||
| 8 | from werkzeug.utils import secure_filename | ||
| 9 | |||
| 10 | |||
| 11 | def get_allow_origin_header_value(): | ||
| 12 | return os.environ.get('MAT2_ALLOW_ORIGIN_WHITELIST', '*').split(" ") | ||
| 13 | |||
| 14 | |||
| 15 | def hash_file(filepath: str) -> str: | ||
| 16 | sha256 = hashlib.sha256() | ||
| 17 | with open(filepath, 'rb') as f: | ||
| 18 | while True: | ||
| 19 | data = f.read(65536) # read the file by chunk of 64k | ||
| 20 | if not data: | ||
| 21 | break | ||
| 22 | sha256.update(data) | ||
| 23 | return sha256.hexdigest() | ||
| 24 | |||
| 25 | |||
| 26 | def check_upload_folder(upload_folder): | ||
| 27 | if not os.path.exists(upload_folder): | ||
| 28 | os.mkdir(upload_folder) | ||
| 29 | |||
| 30 | |||
| 31 | def return_file_created_response(output_filename, mime, key, meta, meta_after, download_link): | ||
| 32 | return { | ||
| 33 | 'output_filename': output_filename, | ||
| 34 | 'mime': mime, | ||
| 35 | 'key': key, | ||
| 36 | 'meta': meta, | ||
| 37 | 'meta_after': meta_after, | ||
| 38 | 'download_link': download_link | ||
| 39 | } | ||
| 40 | |||
| 41 | |||
| 42 | def get_supported_extensions(): | ||
| 43 | extensions = set() | ||
| 44 | for parser in parser_factory._get_parsers(): | ||
| 45 | for m in parser.mimetypes: | ||
| 46 | extensions |= set(mtype.guess_all_extensions(m, strict=False)) | ||
| 47 | # since `guess_extension` might return `None`, we need to filter it out | ||
| 48 | return sorted(filter(None, extensions)) | ||
| 49 | |||
| 50 | |||
| 51 | def save_file(file, upload_folder): | ||
| 52 | filename = secure_filename(file.filename) | ||
| 53 | filepath = os.path.join(upload_folder, filename) | ||
| 54 | file.save(os.path.join(filepath)) | ||
| 55 | return filename, filepath | ||
| 56 | |||
| 57 | |||
| 58 | def get_file_parser(filepath: str): | ||
| 59 | parser, mime = parser_factory.get_parser(filepath) | ||
| 60 | return parser, mime | ||
| 61 | |||
| 62 | |||
| 63 | def cleanup(parser, filepath, upload_folder): | ||
| 64 | output_filename = os.path.basename(parser.output_filename) | ||
| 65 | parser, _ = parser_factory.get_parser(parser.output_filename) | ||
| 66 | meta_after = parser.get_meta() | ||
| 67 | os.remove(filepath) | ||
| 68 | |||
| 69 | key = hash_file(os.path.join(upload_folder, output_filename)) | ||
| 70 | return key, meta_after, output_filename | ||
| 71 | |||
| 72 | |||
| 73 | def get_file_paths(filename, upload_folder): | ||
| 74 | filepath = secure_filename(filename) | ||
| 75 | |||
| 76 | complete_path = os.path.join(upload_folder, filepath) | ||
| 77 | return complete_path, filepath | ||
| 78 | |||
| 79 | |||
| 80 | def is_valid_api_download_file(filename, key, upload_folder): | ||
| 81 | if filename != secure_filename(filename): | ||
| 82 | abort(400, message='Insecure filename') | ||
| 83 | |||
| 84 | complete_path, filepath = get_file_paths(filename, upload_folder) | ||
| 85 | |||
| 86 | if not os.path.exists(complete_path): | ||
| 87 | abort(404, message='File not found') | ||
| 88 | |||
| 89 | if hmac.compare_digest(hash_file(complete_path), key) is False: | ||
| 90 | abort(400, message='The file hash does not match') | ||
| 91 | return complete_path, filepath | ||
