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-20 19:32:46 +02:00
|
|
|
import mimetypes
|
2021-06-05 18:59:19 +02:00
|
|
|
|
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')
|
2021-06-13 14:34:58 +02:00
|
|
|
OBJECT_TYPES = ['gltf', 'glb', 'obj', 'fbx', 'stl', 'dae', 'json', 'ply']
|
2021-06-21 08:55:38 +02:00
|
|
|
TEXT_TYPES = ['txt', 'mtl', 'md']
|
2021-06-05 18:59:19 +02:00
|
|
|
|
|
|
|
def get_directory_content(dir_path):
|
2021-06-20 19:32:46 +02:00
|
|
|
mimetypes.init()
|
2021-06-05 18:59:19 +02:00
|
|
|
content = []
|
|
|
|
for p in Path(dir_path).iterdir():
|
2021-06-20 19:32:46 +02:00
|
|
|
file_path = Path(p)
|
|
|
|
file_name = Path(file_path.name)
|
|
|
|
if file_path.name.startswith("."):
|
2021-06-19 08:21:42 +02:00
|
|
|
continue
|
2021-06-20 19:32:46 +02:00
|
|
|
file_extension = (Path(file_path.name).suffix).lstrip('.').lower()
|
|
|
|
is_object_file = True if file_extension in OBJECT_TYPES else False
|
2021-06-21 08:55:38 +02:00
|
|
|
is_text_file = True if file_extension in TEXT_TYPES else False
|
2021-06-20 19:32:46 +02:00
|
|
|
is_dir = True if p.is_dir() else False
|
|
|
|
is_image = False
|
|
|
|
if not is_dir:
|
|
|
|
mimetype, _ = mimetypes.guess_type(str(Path(p)))
|
|
|
|
is_image = True if mimetype and 'image' in mimetype else False
|
2021-06-05 18:59:19 +02:00
|
|
|
item = {
|
|
|
|
'name': os.path.basename(p),
|
|
|
|
'size': human_readable_bytes(os.path.getsize(p)),
|
2021-06-20 19:32:46 +02:00
|
|
|
'is_dir': is_dir,
|
|
|
|
'is_object_file': is_object_file,
|
2021-06-21 08:55:38 +02:00
|
|
|
'is_text': is_text_file,
|
2021-06-20 19:32:46 +02:00
|
|
|
'is_image': is_image
|
2021-06-05 18:59:19 +02:00
|
|
|
}
|
|
|
|
content.append(item)
|
|
|
|
return content
|
|
|
|
|
|
|
|
|
2021-06-20 15:16:37 +02:00
|
|
|
def find_skybox_image(dir):
|
|
|
|
for p in Path(dir).iterdir():
|
|
|
|
extension = Path(p).suffix
|
2021-06-20 16:18:51 +02:00
|
|
|
if str(Path(p)).endswith(f"sky-box-image{extension}"):
|
|
|
|
return str(p)
|
|
|
|
return None
|
|
|
|
|
|
|
|
def find_pre_loader_image(dir):
|
|
|
|
for p in Path(dir).iterdir():
|
|
|
|
extension = Path(p).suffix
|
|
|
|
if str(Path(p)).endswith(f"pre-load-image{extension}"):
|
2021-06-20 15:16:37 +02:00
|
|
|
return str(p)
|
|
|
|
return None
|
|
|
|
|
2021-06-08 14:19:24 +02:00
|
|
|
def find_file_with_extension(dir, extension):
|
|
|
|
for p in Path(dir).iterdir():
|
|
|
|
if Path(p).suffix.lstrip('.').lower() == extension:
|
2021-06-20 15:16:37 +02:00
|
|
|
return str(p).replace(OBJECT_DIR, "")
|
2021-06-08 14:19:24 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def get_object(file_path, result={}):
|
|
|
|
parts = file_path.split('/')
|
|
|
|
file_name = parts.pop()
|
|
|
|
file_dir = os.path.join(OBJECT_DIR, *parts)
|
|
|
|
|
|
|
|
print('get_object(file_dir): ', file_dir)
|
|
|
|
print('get_object(file_name): ', file_name)
|
|
|
|
print('get_object(file_path): ', os.path.join(OBJECT_DIR, *parts, file_name))
|
|
|
|
if not os.path.exists(os.path.join(OBJECT_DIR, *parts, file_name)):
|
|
|
|
print("cant find file")
|
|
|
|
return result
|
|
|
|
|
2021-06-08 11:08:02 +02:00
|
|
|
if not result:
|
|
|
|
result['urls'] = []
|
|
|
|
result['type'] = None
|
2021-06-08 14:19:24 +02:00
|
|
|
extension = Path(file_name).suffix.lstrip('.').lower()
|
|
|
|
print('get_object(extension):', extension)
|
2021-06-08 11:08:02 +02:00
|
|
|
|
2021-06-20 15:16:37 +02:00
|
|
|
if extension == "gltf" or extension == 'glb':
|
|
|
|
if extension == "gltf":
|
|
|
|
result['type']="gltf"
|
|
|
|
if extension == 'glb':
|
|
|
|
result['type'] = "glb"
|
2021-06-08 14:19:24 +02:00
|
|
|
object_path = str(file_path).replace(OBJECT_DIR, "")
|
2021-06-18 20:44:14 +02:00
|
|
|
result['urls'].append(f"/objects/{object_path}")
|
2021-06-20 15:16:37 +02:00
|
|
|
skybox_path = find_skybox_image(file_dir)
|
|
|
|
if skybox_path:
|
|
|
|
skybox_path = str(skybox_path).replace(OBJECT_DIR, '')
|
|
|
|
result['skybox'] = f"/objects{skybox_path}"
|
2021-06-20 16:18:51 +02:00
|
|
|
preloader_path = find_pre_loader_image(file_dir)
|
|
|
|
if preloader_path:
|
|
|
|
preloader_path = str(preloader_path).replace(OBJECT_DIR, '')
|
|
|
|
result['preloader'] = f"/objects{preloader_path}"
|
2021-06-08 14:19:24 +02:00
|
|
|
return result
|
|
|
|
if extension == "obj":
|
|
|
|
result['type']="obj"
|
|
|
|
object_path = str(file_path).replace(OBJECT_DIR, "")
|
2021-06-20 19:58:21 +02:00
|
|
|
result['urls'].append(f"/objects/{object_path}")
|
2021-06-08 14:19:24 +02:00
|
|
|
mtl_file = find_file_with_extension(file_dir, 'mtl')
|
|
|
|
if mtl_file:
|
|
|
|
result = get_object(mtl_file.replace(OBJECT_DIR, ""), result)
|
|
|
|
result['type']="obj-mtl"
|
|
|
|
return result
|
|
|
|
if extension == 'stl':
|
|
|
|
result['type'] = "stl"
|
|
|
|
object_path = str(file_path).replace(OBJECT_DIR, "")
|
2021-06-18 20:44:14 +02:00
|
|
|
result['urls'].append(f"/objects/{object_path}")
|
2021-06-08 14:19:24 +02:00
|
|
|
return result
|
|
|
|
if extension == 'mtl':
|
|
|
|
object_path = str(file_path).replace(OBJECT_DIR, "")
|
2021-06-18 20:44:14 +02:00
|
|
|
result['urls'].append(f"/objects/{object_path}")
|
2021-06-08 14:19:24 +02:00
|
|
|
return result
|
|
|
|
if extension == 'fbx':
|
|
|
|
result['type'] = "fbx"
|
|
|
|
object_path = str(file_path).replace(OBJECT_DIR, "")
|
2021-06-18 20:44:14 +02:00
|
|
|
result['urls'].append(f"/objects/{object_path}")
|
2021-06-08 14:19:24 +02:00
|
|
|
return result
|
|
|
|
if extension == 'dae':
|
|
|
|
result['type'] = "dae"
|
|
|
|
object_path = str(file_path).replace(OBJECT_DIR, "")
|
2021-06-18 20:44:14 +02:00
|
|
|
result['urls'].append(f"/objects/{object_path}")
|
2021-06-08 14:19:24 +02:00
|
|
|
return result
|
2021-06-13 14:28:28 +02:00
|
|
|
if extension == 'json':
|
|
|
|
result['type'] = "json"
|
|
|
|
object_path = str(file_path).replace(OBJECT_DIR, "")
|
2021-06-18 20:44:14 +02:00
|
|
|
result['urls'].append(f"/objects/{object_path}")
|
2021-06-13 14:28:28 +02:00
|
|
|
return result
|
2021-06-13 14:34:58 +02:00
|
|
|
if extension == 'ply':
|
|
|
|
result['type'] = "ply"
|
|
|
|
object_path = str(file_path).replace(OBJECT_DIR, "")
|
2021-06-18 20:44:14 +02:00
|
|
|
result['urls'].append(f"/objects/{object_path}")
|
2021-06-13 14:34:58 +02:00
|
|
|
return result
|
2021-06-08 14:19:24 +02:00
|
|
|
return None
|
2021-06-08 11:08:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
"""
|
2021-06-08 14:19:24 +02:00
|
|
|
if 'obj' in extensions and 'mtl' in extensions:
|
2021-06-08 11:08:02 +02:00
|
|
|
print("p: ",p)
|
|
|
|
return 'obj-mtl'
|
2021-06-08 14:19:24 +02:00
|
|
|
if 'obj' in extensions:
|
2021-06-08 11:08:02 +02:00
|
|
|
return 'obj'
|
2021-06-08 14:19:24 +02:00
|
|
|
if 'gltf' in extensions:
|
2021-06-08 11:08:02 +02:00
|
|
|
return 'gltf'
|
2021-06-08 14:19:24 +02:00
|
|
|
if 'fbx' in extensions:
|
2021-06-08 11:08:02 +02:00
|
|
|
return 'fbx'
|
2021-06-08 14:19:24 +02:00
|
|
|
if 'stl' in extensions:
|
2021-06-08 11:08:02 +02:00
|
|
|
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"
|