From 69089624b75d35d66a5bbf11831fb9bb0db84d7f Mon Sep 17 00:00:00 2001 From: buttle Date: Mon, 21 Jun 2021 08:55:38 +0200 Subject: [PATCH] adds view for .mtl and other text files --- app.py | 22 ++++++++++++++++++++++ utils/utils.py | 3 +++ views/list-dir.tpl | 6 +++++- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 2af6b37..eeba397 100644 --- a/app.py +++ b/app.py @@ -82,6 +82,28 @@ def image(file_path): } ) +@route('/txt/') +def text(file_path): + file_path = file_path.replace('/txt/', '') + parts = file_path.split('/') + file_name = parts.pop() + print(OBJECT_DIR) + _file_path = os.path.join(OBJECT_DIR, file_path.lstrip('/')) + if not os.path.exists(_file_path): + return "No file found" + text_file_content = None + with open(os.path.join(_file_path), 'r') as text_file: + text_file_content = text_file.read() + text_file_content = text_file_content.replace('\r\n', '
') + text_file_content = text_file_content.replace('\n', '
') + return template('text', + current_dir=file_path.replace(file_name, ""), + name=file_name, + object={'type': 'text', + 'file_content': text_file_content + } + ) + @route('/render/') def render(file_path): file_path = file_path.replace('/render/', '') diff --git a/utils/utils.py b/utils/utils.py index cc0974e..ab476ba 100644 --- a/utils/utils.py +++ b/utils/utils.py @@ -28,6 +28,7 @@ BASE_DIR = os.path.abspath( ) OBJECT_DIR = os.path.join(BASE_DIR, 'objects') OBJECT_TYPES = ['gltf', 'glb', 'obj', 'fbx', 'stl', 'dae', 'json', 'ply'] +TEXT_TYPES = ['txt', 'mtl', 'md'] def get_directory_content(dir_path): mimetypes.init() @@ -39,6 +40,7 @@ def get_directory_content(dir_path): continue file_extension = (Path(file_path.name).suffix).lstrip('.').lower() is_object_file = True if file_extension in OBJECT_TYPES else False + is_text_file = True if file_extension in TEXT_TYPES else False is_dir = True if p.is_dir() else False is_image = False if not is_dir: @@ -49,6 +51,7 @@ def get_directory_content(dir_path): 'size': human_readable_bytes(os.path.getsize(p)), 'is_dir': is_dir, 'is_object_file': is_object_file, + 'is_text': is_text_file, 'is_image': is_image } content.append(item) diff --git a/views/list-dir.tpl b/views/list-dir.tpl index 3abe792..9c95ebf 100644 --- a/views/list-dir.tpl +++ b/views/list-dir.tpl @@ -10,7 +10,7 @@ td .fa {

- objects/{{current_dir}} + /{{current_dir}}

Parent directory

@@ -37,6 +37,10 @@ td .fa { {{item['name']}} + %elif item['is_text']: + + + {{item['name']}} %else: {{item['name']}}