diff options
| author | jfriedli | 2025-01-12 12:11:06 +0000 |
|---|---|---|
| committer | jfriedli | 2025-01-12 12:11:06 +0000 |
| commit | 1035a24707276a97c75a6fd1ecf9f425fb01fc10 (patch) | |
| tree | 5543133f4c6b8d15fb438e244a21091450fa5822 | |
| parent | a5715f9f8cecaa74a5f5f385b63631d15102567c (diff) | |
Added Non-Ascii filename support
| -rw-r--r-- | matweb/frontend.py | 2 | ||||
| -rw-r--r-- | matweb/utils.py | 13 | ||||
| -rw-r--r-- | test/test.py | 17 | ||||
| -rw-r--r-- | test/test_api.py | 8 |
4 files changed, 30 insertions, 10 deletions
diff --git a/matweb/frontend.py b/matweb/frontend.py index 018fc97..ec9ef23 100644 --- a/matweb/frontend.py +++ b/matweb/frontend.py | |||
| @@ -68,7 +68,7 @@ def upload_file(): | |||
| 68 | parser, mime = utils.get_file_parser(filepath) | 68 | parser, mime = utils.get_file_parser(filepath) |
| 69 | except ValueError: | 69 | except ValueError: |
| 70 | flash('The filetype is not supported') | 70 | flash('The filetype is not supported') |
| 71 | current_app.logger.error('Unsupported filetype',) | 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/utils.py b/matweb/utils.py index 270c5f3..39159e1 100644 --- a/matweb/utils.py +++ b/matweb/utils.py | |||
| @@ -2,6 +2,8 @@ import hmac | |||
| 2 | import os | 2 | import os |
| 3 | import hashlib | 3 | import hashlib |
| 4 | import mimetypes as mtype | 4 | import mimetypes as mtype |
| 5 | import pathlib | ||
| 6 | import uuid | ||
| 5 | from typing import Tuple | 7 | from typing import Tuple |
| 6 | 8 | ||
| 7 | from flask_restful import abort, current_app | 9 | from flask_restful import abort, current_app |
| @@ -68,9 +70,16 @@ def get_supported_extensions(): | |||
| 68 | 70 | ||
| 69 | 71 | ||
| 70 | def save_file(file, upload_folder): | 72 | def save_file(file, upload_folder): |
| 71 | filename = secure_filename(file.filename) | 73 | path = pathlib.Path(file.filename) |
| 74 | extension = path.suffix | ||
| 75 | stem = path.stem | ||
| 76 | |||
| 77 | filename = secure_filename(stem) | ||
| 72 | if not filename: | 78 | if not filename: |
| 73 | raise ValueError('Invalid Filename') | 79 | filename = str(uuid.uuid4()) |
| 80 | |||
| 81 | if extension: | ||
| 82 | filename = str(pathlib.Path(filename).with_suffix(extension)) | ||
| 74 | filepath = os.path.join(upload_folder, filename) | 83 | filepath = os.path.join(upload_folder, filename) |
| 75 | file.save(os.path.join(filepath)) | 84 | file.save(os.path.join(filepath)) |
| 76 | return filename, filepath | 85 | return filename, filepath |
diff --git a/test/test.py b/test/test.py index 4d2907f..d5ef52d 100644 --- a/test/test.py +++ b/test/test.py | |||
| @@ -187,17 +187,28 @@ class Mat2WebTestCase(TestCase): | |||
| 187 | self.assertIn(b'.mp2', rv.data) | 187 | self.assertIn(b'.mp2', rv.data) |
| 188 | self.assertEqual(rv.status_code, 200) | 188 | self.assertEqual(rv.status_code, 200) |
| 189 | 189 | ||
| 190 | def test_get_upload_naughty_input(self): | 190 | def test_get_upload_no_ascii_no_ext_input(self): |
| 191 | rv = self.client.post( | 191 | rv = self.client.post( |
| 192 | '/', | 192 | '/', |
| 193 | data=dict( | 193 | data=dict( |
| 194 | file=(io.BytesIO(b"a"), '﷽'), | 194 | file=(io.BytesIO(b"a"), '﷽.txt'), |
| 195 | ), | 195 | ), |
| 196 | follow_redirects=True | 196 | follow_redirects=True |
| 197 | ) | 197 | ) |
| 198 | self.assertEqual(rv.status_code, 200) | 198 | self.assertEqual(rv.status_code, 200) |
| 199 | self.assertIn(b'Invalid Filename', rv.data) | 199 | self.assertIn(b'.cleaned.txt', rv.data) |
| 200 | 200 | ||
| 201 | def test_get_upload_no_ascii_stem_input(self): | ||
| 202 | pdfBytes = b"%PDF-1.\n 1 0 obj<</Pages 2 0 R>>endobj\n2 0 obj<</Kids[3 0 R]/Count 1>>endobj\n3 0 obj<</Parent 2 0 R>>endobj\ntrailer <</Root 1 0 R>>" | ||
| 203 | rv = self.client.post( | ||
| 204 | '/', | ||
| 205 | data=dict( | ||
| 206 | file=(io.BytesIO(pdfBytes), '한국어.pdf'), | ||
| 207 | ), | ||
| 208 | follow_redirects=True | ||
| 209 | ) | ||
| 210 | self.assertEqual(rv.status_code, 200) | ||
| 211 | self.assertIn(b'.cleaned.pdf', rv.data) | ||
| 201 | 212 | ||
| 202 | if __name__ == '__main__': | 213 | if __name__ == '__main__': |
| 203 | unittest.main() | 214 | unittest.main() |
diff --git a/test/test_api.py b/test/test_api.py index 2f52279..0ceb846 100644 --- a/test/test_api.py +++ b/test/test_api.py | |||
| @@ -451,8 +451,8 @@ class Mat2APITestCase(unittest.TestCase): | |||
| 451 | headers={'content-type': 'application/json'} | 451 | headers={'content-type': 'application/json'} |
| 452 | ) | 452 | ) |
| 453 | error_message = request.get_json()['message'] | 453 | error_message = request.get_json()['message'] |
| 454 | self.assertEqual(400, request.status_code) | 454 | self.assertEqual(415, request.status_code) |
| 455 | self.assertEqual("Invalid Filename", error_message) | 455 | self.assertEqual("The filetype is not supported", error_message) |
| 456 | 456 | ||
| 457 | request = self.app.post('/api/upload', | 457 | request = self.app.post('/api/upload', |
| 458 | data='{"file_name": "﷽", ' | 458 | data='{"file_name": "﷽", ' |
| @@ -534,8 +534,8 @@ class Mat2APITestCase(unittest.TestCase): | |||
| 534 | ), | 534 | ), |
| 535 | follow_redirects=False | 535 | follow_redirects=False |
| 536 | ) | 536 | ) |
| 537 | self.assertEqual(r.get_json()['message'], 'Invalid Filename') | 537 | self.assertEqual(r.get_json()['message'], 'The filetype is not supported') |
| 538 | self.assertEqual(r.status_code, 400) | 538 | self.assertEqual(r.status_code, 415) |
| 539 | 539 | ||
| 540 | r = self.app.post( | 540 | r = self.app.post( |
| 541 | '/api/remove_metadata', | 541 | '/api/remove_metadata', |
