From 1035a24707276a97c75a6fd1ecf9f425fb01fc10 Mon Sep 17 00:00:00 2001 From: jfriedli Date: Sun, 12 Jan 2025 12:11:06 +0000 Subject: Added Non-Ascii filename support --- matweb/frontend.py | 2 +- matweb/utils.py | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) (limited to 'matweb') 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(): parser, mime = utils.get_file_parser(filepath) except ValueError: flash('The filetype is not supported') - current_app.logger.error('Unsupported filetype',) + current_app.logger.error('Unsupported filetype') return redirect(url_for('routes.upload_file')) 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 import os import hashlib import mimetypes as mtype +import pathlib +import uuid from typing import Tuple from flask_restful import abort, current_app @@ -68,9 +70,16 @@ def get_supported_extensions(): def save_file(file, upload_folder): - filename = secure_filename(file.filename) + path = pathlib.Path(file.filename) + extension = path.suffix + stem = path.stem + + filename = secure_filename(stem) if not filename: - raise ValueError('Invalid Filename') + filename = str(uuid.uuid4()) + + if extension: + filename = str(pathlib.Path(filename).with_suffix(extension)) filepath = os.path.join(upload_folder, filename) file.save(os.path.join(filepath)) return filename, filepath -- cgit v1.3