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
|
|
|
|
from pathlib import Path
|
|
|
|
|
2021-06-08 11:08:02 +02:00
|
|
|
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']
|
2021-06-05 18:59:19 +02:00
|
|
|
|
|
|
|
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()
|
2021-06-08 11:08:02 +02:00
|
|
|
is_object_file = True if model_type in OBJECT_TYPES else False
|
2021-06-05 18:59:19 +02:00
|
|
|
item = {
|
|
|
|
'name': os.path.basename(p),
|
|
|
|
'size': human_readable_bytes(os.path.getsize(p)),
|
|
|
|
'is_dir': True if p.is_dir() else False,
|
|
|
|
'is_object_file': is_object_file
|
|
|
|
}
|
|
|
|
content.append(item)
|
|
|
|
return content
|
|
|
|
|
|
|
|
|
2021-06-08 11:08:02 +02:00
|
|
|
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'
|
|
|
|
"""
|
|
|
|
|
2021-06-05 18:59:19 +02:00
|
|
|
def human_readable_bytes(bytes):
|
|
|
|
""" 1 KibiByte == 1024 Bytes
|
|
|
|
1 Mebibyte == 1024*1024 Bytes
|
|
|
|
1 GibiByte == 1024*1024*1024 Bytes
|
|
|
|
"""
|
|
|
|
if bytes == 0:
|
2021-06-08 11:08:02 +02:00
|
|
|
return "0 bytes"
|
|
|
|
if bytes < 1024:
|
|
|
|
return f"{bytes} bytes"
|
2021-06-05 18:59:19 +02:00
|
|
|
if bytes < 1024*1024:
|
|
|
|
return f"{int(round(bytes/(1024), 2))} KB"
|
|
|
|
if bytes < 1024*1024*1024:
|
|
|
|
return f"{int(round(bytes/(1024*1024), 2))} MB"
|
|
|
|
return f"{int(round(bytes/(1024*1024*1024), 2))} GB"
|