adds view for .mtl and other text files

This commit is contained in:
buttle 2021-06-21 08:55:38 +02:00
parent 04d04ea4ba
commit 69089624b7
3 changed files with 30 additions and 1 deletions

22
app.py
View File

@ -82,6 +82,28 @@ def image(file_path):
} }
) )
@route('/txt/<file_path:path>')
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', '<br />')
text_file_content = text_file_content.replace('\n', '<br />')
return template('text',
current_dir=file_path.replace(file_name, ""),
name=file_name,
object={'type': 'text',
'file_content': text_file_content
}
)
@route('/render/<file_path:path>') @route('/render/<file_path:path>')
def render(file_path): def render(file_path):
file_path = file_path.replace('/render/', '') file_path = file_path.replace('/render/', '')

View File

@ -28,6 +28,7 @@ BASE_DIR = os.path.abspath(
) )
OBJECT_DIR = os.path.join(BASE_DIR, 'objects') OBJECT_DIR = os.path.join(BASE_DIR, 'objects')
OBJECT_TYPES = ['gltf', 'glb', 'obj', 'fbx', 'stl', 'dae', 'json', 'ply'] OBJECT_TYPES = ['gltf', 'glb', 'obj', 'fbx', 'stl', 'dae', 'json', 'ply']
TEXT_TYPES = ['txt', 'mtl', 'md']
def get_directory_content(dir_path): def get_directory_content(dir_path):
mimetypes.init() mimetypes.init()
@ -39,6 +40,7 @@ def get_directory_content(dir_path):
continue continue
file_extension = (Path(file_path.name).suffix).lstrip('.').lower() file_extension = (Path(file_path.name).suffix).lstrip('.').lower()
is_object_file = True if file_extension in OBJECT_TYPES else False 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_dir = True if p.is_dir() else False
is_image = False is_image = False
if not is_dir: if not is_dir:
@ -49,6 +51,7 @@ def get_directory_content(dir_path):
'size': human_readable_bytes(os.path.getsize(p)), 'size': human_readable_bytes(os.path.getsize(p)),
'is_dir': is_dir, 'is_dir': is_dir,
'is_object_file': is_object_file, 'is_object_file': is_object_file,
'is_text': is_text_file,
'is_image': is_image 'is_image': is_image
} }
content.append(item) content.append(item)

View File

@ -10,7 +10,7 @@ td .fa {
<h3> <h3>
<i class="fa fa-folder-o" aria-hidden="true"></i> <i class="fa fa-folder-o" aria-hidden="true"></i>
objects/{{current_dir}} /{{current_dir}}
</h3> </h3>
<h4><a href="/list/{{parent_dir}}">Parent directory</a></h4> <h4><a href="/list/{{parent_dir}}">Parent directory</a></h4>
@ -37,6 +37,10 @@ td .fa {
<a href="/image/{{current_dir}}/{{item['name']}}"> <a href="/image/{{current_dir}}/{{item['name']}}">
<i class="fa fa-file-image-o" aria-hidden="true"></i></a> <i class="fa fa-file-image-o" aria-hidden="true"></i></a>
{{item['name']}} {{item['name']}}
%elif item['is_text']:
<a href="/txt/{{current_dir}}/{{item['name']}}">
<i class="fa fa-file-text-o" aria-hidden="true"></i></a>
{{item['name']}}
%else: %else:
<i class="fa fa-file-o" aria-hidden="true"></i> <i class="fa fa-file-o" aria-hidden="true"></i>
{{item['name']}} {{item['name']}}