3D-tester/app.py

137 lines
4.3 KiB
Python
Raw Permalink Normal View History

2021-06-05 18:59:19 +02:00
"""
Copyright 2021 Hangar.org
This file is part of 3D-tester.
3D-tester is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import os, sys
from pathlib import Path
2021-06-18 20:27:33 +02:00
import bottle
2021-06-05 18:59:19 +02:00
from bottle import route, run, error, abort, template, response, static_file
2021-06-08 11:08:02 +02:00
from utils.utils import BASE_DIR, OBJECT_DIR
2021-06-05 18:59:19 +02:00
from utils import utils
@route('/static/<filename:path>', name='static')
def serve_static(filename):
return static_file(filename, root=os.path.join(BASE_DIR, 'static'))
@route('/vendor/<filename:path>', name='static')
def serve_static(filename):
return static_file(filename, root=os.path.join(BASE_DIR, 'vendor'))
@route('/objects/<filename:path>', name='static')
def serve_static(filename):
return static_file(filename, root=os.path.join(BASE_DIR, 'objects'))
@route('/')
@route('/list')
@route('/list/')
def index():
content = utils.get_directory_content(OBJECT_DIR)
return template('list-dir',
parent_dir="",
current_dir="",
content=content)
@route('/list/<dir_path:path>')
def list_dir(dir_path):
print("list path: ",dir_path)
#dir_path = dir_path.replace('/list/', '')
parts = dir_path.split('/')
print("list path: ",dir_path)
current_dir=os.path.join(OBJECT_DIR, *parts)
content = utils.get_directory_content(current_dir)
parts.pop()
parent_dir='/'.join(parts) if parts else ""
return template('list-dir',
parent_dir=parent_dir,
current_dir=dir_path.rstrip('/'),
content=content)
2021-06-20 19:32:46 +02:00
@route('/img/<filename:path>')
def send_image(filename):
return static_file(filename, root=OBJECT_DIR)
@route('/image/<file_path:path>')
def image(file_path):
file_path = file_path.replace('/img/', '')
parts = file_path.split('/')
file_name = parts.pop()
return template('image',
current_dir=file_path.replace(file_name, ""),
name=file_name,
object={'type': 'image',
2021-06-20 19:34:18 +02:00
'url': f"/img/{file_path}"
2021-06-20 19:32:46 +02:00
}
)
@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
}
)
2021-06-05 18:59:19 +02:00
@route('/render/<file_path:path>')
def render(file_path):
file_path = file_path.replace('/render/', '')
2021-06-08 11:08:02 +02:00
parts = file_path.split('/')
file_name = parts.pop()
file_dir = os.path.join(OBJECT_DIR, *parts)
2021-06-08 11:23:16 +02:00
#print("file_dir: ", file_dir)
#print("file_name: ", file_name)
2021-06-08 11:08:02 +02:00
2021-06-08 14:19:24 +02:00
object = utils.get_object(file_path, {})
2021-06-08 11:08:02 +02:00
if not object:
return "Opps, no object type"
print("object: ", object)
2021-06-13 14:11:22 +02:00
if object['type'] == 'gltf' or object['type'] == 'glb':
page_tpl = "google"
2021-06-08 14:19:24 +02:00
else:
2021-06-13 14:11:22 +02:00
page_tpl = "vue-3d"
2021-06-08 11:08:02 +02:00
2021-06-08 14:19:24 +02:00
return template(page_tpl,
2021-06-08 11:08:02 +02:00
current_dir=file_path.replace(file_name, ""),
name=file_name,
object=object,
2021-06-05 18:59:19 +02:00
)
2021-06-18 20:27:33 +02:00
if __name__ == "__main__":
run(host='localhost', port=8080, debug=True)
2021-06-05 18:59:19 +02:00
2021-06-18 20:27:33 +02:00
app = bottle.default_app()