utils returns list dict

This commit is contained in:
buttle 2021-06-08 11:08:02 +02:00
parent 3cd559fde5
commit e37c81811b
8 changed files with 123 additions and 86878 deletions

31
app.py
View File

@ -21,12 +21,10 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
import os, sys
from pathlib import Path
from bottle import route, run, error, abort, template, response, static_file
from utils.utils import BASE_DIR, OBJECT_DIR
from utils import utils
BASE_DIR = os.path.dirname(os.path.realpath(__file__))
OBJECT_DIR = os.path.join(BASE_DIR, 'objects')
@route('/static/<filename:path>', name='static')
def serve_static(filename):
return static_file(filename, root=os.path.join(BASE_DIR, 'static'))
@ -68,19 +66,24 @@ def list_dir(dir_path):
@route('/render/<file_path:path>')
def render(file_path):
file_path = file_path.replace('/render/', '')
print(file_path)
current_path=os.path.join(OBJECT_DIR, file_path)
file = Path(current_path)
model_type = (Path(file.name).suffix).lstrip('.').lower()
current_dir=file_path.replace(file.name, '')
current_dir = current_dir.rstrip('/')
object_url = f"/objects/{current_dir}/{file.name}"
parts = file_path.split('/')
file_name = parts.pop()
file_dir = os.path.join(OBJECT_DIR, *parts)
print("file_dir: ", file_dir)
print("file_name: ", file_name)
object = utils.get_object(file_dir, [], {})
if not object:
return "Opps, no object type"
print("object: ", object)
return template('render',
current_dir=current_dir,
name=file.name,
object_url=object_url,
model_type=model_type,
current_dir=file_path.replace(file_name, ""),
name=file_name,
object=object,
)
run(host='localhost', port=8080, debug=True)

View File

@ -20,14 +20,20 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
import os
from pathlib import Path
MODEL_TYPES = ['gltf', 'obj', 'fbx', 'stl']
BASE_DIR = os.path.abspath(
os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'../')
)
OBJECT_DIR = os.path.join(BASE_DIR, 'objects')
OBJECT_TYPES = ['gltf', 'obj', 'fbx', 'stl']
def get_directory_content(dir_path):
content = []
for p in Path(dir_path).iterdir():
file = Path(p)
model_type = (Path(file.name).suffix).lstrip('.').lower()
is_object_file = True if model_type in MODEL_TYPES else False
is_object_file = True if model_type in OBJECT_TYPES else False
item = {
'name': os.path.basename(p),
'size': human_readable_bytes(os.path.getsize(p)),
@ -38,13 +44,70 @@ def get_directory_content(dir_path):
return content
def get_object(dir_path, extentions=[], result={}):
print('get_object(dir_path): ', dir_path)
if not result:
result['urls'] = []
result['type'] = None
if not extentions:
for p in Path(dir_path).iterdir():
suffix = Path(p).suffix.lstrip('.').lower()
if suffix in OBJECT_TYPES:
extentions.append(suffix)
for p in Path(dir_path).iterdir():
suffix = Path(p).suffix.lstrip('.').lower()
if suffix in extentions and suffix == "gltf":
result['type']="gltf"
print("p: ",p)
print("Path(p): ",Path(p))
object_path = str(p).replace(OBJECT_DIR, "")
result['urls'].append(f"/objects{object_path}")
return result
if suffix in extentions and suffix == "obj":
result['type']="obj"
object_path = str(p).replace(OBJECT_DIR, "")
result['urls'].append(f"/objects{object_path}")
print("result_1: ", result)
result = get_object(dir_path, extentions=['mtl'], result=result)
print("result_2: ", result)
return result
if suffix in extentions and suffix == "mtl":
if 'obj' in result['type']:
result['type'] = "obj-mtl"
object_path = str(p).replace(OBJECT_DIR, "")
result['urls'].append(f"/objects{object_path}")
return result
if suffix in extentions and suffix == 'stl':
result['type'] = "stl"
object_path = str(p).replace(OBJECT_DIR, "")
result['urls'].append(f"/objects{object_path}")
return result
"""
if 'obj' in extentions and 'mtl' in extentions:
print("p: ",p)
return 'obj-mtl'
if 'obj' in extentions:
return 'obj'
if 'gltf' in extentions:
return 'gltf'
if 'fbx' in extentions:
return 'fbx'
if 'stl' in extentions:
return 'stl'
"""
def human_readable_bytes(bytes):
""" 1 KibiByte == 1024 Bytes
1 Mebibyte == 1024*1024 Bytes
1 GibiByte == 1024*1024*1024 Bytes
"""
if bytes == 0:
return "0 KB"
return "0 bytes"
if bytes < 1024:
return f"{bytes} bytes"
if bytes < 1024*1024:
return f"{int(round(bytes/(1024), 2))} KB"
if bytes < 1024*1024*1024:

View File

@ -1,21 +0,0 @@
The MIT License (MIT)
Copyright (c) 2017-present, Jiulong Hu
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@ -1,6 +0,0 @@
# vue-3d-model
https://github.com/hujiulong/vue-3d-model

File diff suppressed because one or more lines are too long

11965
vendor/vue/vue.js vendored

File diff suppressed because it is too large Load Diff

View File

@ -8,10 +8,6 @@ td .fa {
</style>
<h1>
Directory list
</h1>
<h3>
<i class="fa fa-folder-o" aria-hidden="true"></i>
objects{{current_dir}}

View File

@ -1,42 +1,54 @@
% rebase('base.tpl')
<script src="/vendor/vue/vue.js"></script>
<script src="/vendor/vue-3d-model/vue-3d-model.umd.js"></script>
<script src="/static/vendor/vue/vue.js"></script>
<script src="/static/vendor/vue-3d-model/vue-3d-model.umd.js"></script>
<h1>
Render {{name}}
</h1>
<h3>
<i class="fa fa-folder-o" aria-hidden="true"></i>
<a href="/list/{{current_dir}}">objects{{current_dir}}</a>
</h3>
<h4>
<span style="text-transform: uppercase">
{{ object['type'] }}:
</span>
{{name}}
</h4>
<div class="model-wrap">
%if model_type == "gltf":
<div id="object">
<div id="object">
%if object['type'] == "gltf":
<model-gltf
src="{{object_url}}"
@on-mousemove="onMouseMove"
>
src="{{ object['urls'][0] }}"
@on-mousemove="onMouseMove"
>
</model-gltf>
</div>
%end
%if model_type == "obj":
<div id="object">
<model-obj src="{{object_url}}"></model-obj>
</div>
%end
%if model_type == "fbx":
<div id="object">
<model-fbx src="{{object_url}}"></model-fbx>
</div>
%end
%if model_type == "stl":
<div id="object">
<model-stl src="{{object_url}}"></model-stl>
</div>
%end
%end
%if object['type'] == "obj-mtl":
<model-obj
src="{{ object['urls'][0] }}"
mtl="{{ object['urls'][1] }}"
>
</model-obj>
%end
%if object['type'] == "obj":
<model-obj
src="{{ object['urls'][0] }}"
>
</model-obj>
%end
%if object['type'] == "fbx":
<model-fbx
src="{{ object['urls'][0] }}"
>
</model-fbx>
%end
%if object['type'] == "stl":
<model-stl
src="{{ object['urls'][0] }}"
>
</model-stl>
%end
</div>
</div>
<script>