Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,7 +10,7 @@ from datetime import datetime
|
|
| 10 |
from fastapi import FastAPI, BackgroundTasks
|
| 11 |
from pydantic import BaseModel
|
| 12 |
|
| 13 |
-
# --- CONFIG (
|
| 14 |
os.chdir("/tmp")
|
| 15 |
os.environ["HOME"] = "/tmp"
|
| 16 |
os.environ["UNITY_PATH"] = "/opt/unity/Editor/Unity"
|
|
@@ -29,17 +29,15 @@ class ConversionRequest(BaseModel):
|
|
| 29 |
file_url: str
|
| 30 |
filename: str
|
| 31 |
|
| 32 |
-
# --- HEALTH CHECK (
|
| 33 |
@app.get("/")
|
| 34 |
async def health_check():
|
| 35 |
-
return {"status": "
|
| 36 |
|
| 37 |
-
# --- SETUP LISENSI (
|
| 38 |
def setup_license():
|
| 39 |
ulf_name = "Unity_v2022.x.ulf"
|
| 40 |
-
source_ulf = os.path.join("/app", ulf_name)
|
| 41 |
-
|
| 42 |
-
# Path mengikuti HOME baru yaitu /tmp
|
| 43 |
target_dir = "/tmp/.local/share/unity3d/Unity"
|
| 44 |
target_ulf = os.path.join(target_dir, "Unity_lic.ulf")
|
| 45 |
|
|
@@ -47,11 +45,11 @@ def setup_license():
|
|
| 47 |
if os.path.exists(source_ulf):
|
| 48 |
os.makedirs(target_dir, exist_ok=True)
|
| 49 |
shutil.copy2(source_ulf, target_ulf)
|
| 50 |
-
print(f"β
[LICENSE] Lisensi
|
| 51 |
-
elif os.path.exists(ulf_name):
|
| 52 |
os.makedirs(target_dir, exist_ok=True)
|
| 53 |
shutil.copy2(ulf_name, target_ulf)
|
| 54 |
-
print(f"β
[LICENSE] Lisensi ditemukan di root dan
|
| 55 |
except Exception as e:
|
| 56 |
print(f"β [LICENSE] Gagal pasang lisensi: {e}", flush=True)
|
| 57 |
|
|
@@ -62,77 +60,69 @@ def upload_to_r2(local_path, r2_filename):
|
|
| 62 |
s3.upload_file(local_path, R2_CONFIG['bucket_name'], f"packed/{r2_filename}")
|
| 63 |
print(f"β
[R2] Upload Sukses: packed/{r2_filename}", flush=True)
|
| 64 |
except Exception as e:
|
| 65 |
-
print(f"β [R2] Gagal Upload: {e}", flush=True)
|
| 66 |
-
|
| 67 |
-
def make_zepeto_archive(source_dir, output_file):
|
| 68 |
-
"""Membungkus folder InputRaw menjadi file .zepeto (ZIP)"""
|
| 69 |
-
with zipfile.ZipFile(output_file, 'w', zipfile.ZIP_DEFLATED) as z:
|
| 70 |
-
for root, dirs, files in os.walk(source_dir):
|
| 71 |
-
for file in files:
|
| 72 |
-
if file.endswith((".manifest", ".txt")): continue
|
| 73 |
-
file_path = os.path.join(root, file)
|
| 74 |
-
z.write(file_path, os.path.relpath(file_path, source_dir))
|
| 75 |
|
|
|
|
| 76 |
def process_pipeline(req: ConversionRequest):
|
| 77 |
-
# Pasang lisensi di setiap awal pipeline
|
| 78 |
setup_license()
|
| 79 |
|
| 80 |
session_id = str(uuid.uuid4())[:8]
|
| 81 |
SESSION_PATH = f"/tmp/Project_{session_id}"
|
| 82 |
extract_tmp = f"/tmp/extract_{session_id}"
|
| 83 |
-
|
| 84 |
-
# Tambahkan Timestamp agar file di R2 tidak tertimpa
|
| 85 |
-
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 86 |
-
unique_filename = f"{req.filename}_{timestamp}.zepeto"
|
| 87 |
-
final_zepeto = f"/tmp/{unique_filename}"
|
| 88 |
|
| 89 |
try:
|
| 90 |
-
#
|
|
|
|
| 91 |
r = requests.get(req.file_url)
|
| 92 |
-
zip_tmp = f"/tmp/{session_id}.zip"
|
| 93 |
with open(zip_tmp, 'wb') as f: f.write(r.content)
|
| 94 |
os.makedirs(extract_tmp, exist_ok=True)
|
| 95 |
with zipfile.ZipFile(zip_tmp, 'r') as z: z.extractall(extract_tmp)
|
| 96 |
|
| 97 |
-
#
|
|
|
|
| 98 |
shutil.copytree(BASE_PROJECT_PATH, SESSION_PATH, ignore=shutil.ignore_patterns("Library", "Temp"))
|
| 99 |
target_input = os.path.join(SESSION_PATH, "Assets", "InputRaw")
|
| 100 |
os.makedirs(target_input, exist_ok=True)
|
| 101 |
|
| 102 |
for item in os.listdir(extract_tmp):
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
if os.path.isdir(
|
| 106 |
-
else: shutil.copy2(
|
| 107 |
-
|
| 108 |
-
#
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
# 3. Jalankan Unity
|
| 113 |
-
print(f"π [UNITY] Merampingkan Mesh & Membangun Platform...", flush=True)
|
| 114 |
result = subprocess.run([
|
| 115 |
os.environ["UNITY_PATH"], "-batchmode", "-nographics", "-quit",
|
| 116 |
"-projectPath", SESSION_PATH,
|
| 117 |
"-executeMethod", "TestBuilder.ManualConvert",
|
| 118 |
"-logFile", "/dev/stdout"
|
| 119 |
-
],
|
| 120 |
-
|
| 121 |
-
#
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
upload_to_r2(final_zepeto, unique_filename)
|
| 128 |
else:
|
| 129 |
-
print(
|
| 130 |
|
| 131 |
except Exception as e:
|
| 132 |
-
print(f"π₯ [
|
| 133 |
finally:
|
| 134 |
-
#
|
| 135 |
-
|
|
|
|
| 136 |
if os.path.exists(p):
|
| 137 |
shutil.rmtree(p) if os.path.isdir(p) else os.remove(p)
|
| 138 |
|
|
|
|
| 10 |
from fastapi import FastAPI, BackgroundTasks
|
| 11 |
from pydantic import BaseModel
|
| 12 |
|
| 13 |
+
# --- CONFIG (Konsisten dengan isolasi /tmp untuk Docker) ---
|
| 14 |
os.chdir("/tmp")
|
| 15 |
os.environ["HOME"] = "/tmp"
|
| 16 |
os.environ["UNITY_PATH"] = "/opt/unity/Editor/Unity"
|
|
|
|
| 29 |
file_url: str
|
| 30 |
filename: str
|
| 31 |
|
| 32 |
+
# --- 1. HEALTH CHECK (Agar Container tetap sehat di mata Cloud) ---
|
| 33 |
@app.get("/")
|
| 34 |
async def health_check():
|
| 35 |
+
return {"status": "ready", "engine": "Unity 2022.3 Embedded Mesh Mode"}
|
| 36 |
|
| 37 |
+
# --- 2. SETUP LISENSI (Metode Bypass Permission /tmp) ---
|
| 38 |
def setup_license():
|
| 39 |
ulf_name = "Unity_v2022.x.ulf"
|
| 40 |
+
source_ulf = os.path.join("/app", ulf_name)
|
|
|
|
|
|
|
| 41 |
target_dir = "/tmp/.local/share/unity3d/Unity"
|
| 42 |
target_ulf = os.path.join(target_dir, "Unity_lic.ulf")
|
| 43 |
|
|
|
|
| 45 |
if os.path.exists(source_ulf):
|
| 46 |
os.makedirs(target_dir, exist_ok=True)
|
| 47 |
shutil.copy2(source_ulf, target_ulf)
|
| 48 |
+
print(f"β
[LICENSE] Lisensi terpasang di: {target_ulf}", flush=True)
|
| 49 |
+
elif os.path.exists(ulf_name):
|
| 50 |
os.makedirs(target_dir, exist_ok=True)
|
| 51 |
shutil.copy2(ulf_name, target_ulf)
|
| 52 |
+
print(f"β
[LICENSE] Lisensi ditemukan di root dan terpasang.", flush=True)
|
| 53 |
except Exception as e:
|
| 54 |
print(f"β [LICENSE] Gagal pasang lisensi: {e}", flush=True)
|
| 55 |
|
|
|
|
| 60 |
s3.upload_file(local_path, R2_CONFIG['bucket_name'], f"packed/{r2_filename}")
|
| 61 |
print(f"β
[R2] Upload Sukses: packed/{r2_filename}", flush=True)
|
| 62 |
except Exception as e:
|
| 63 |
+
print(f"β [R2] Gagal Upload ke R2: {e}", flush=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
# --- 3. CORE PIPELINE ---
|
| 66 |
def process_pipeline(req: ConversionRequest):
|
|
|
|
| 67 |
setup_license()
|
| 68 |
|
| 69 |
session_id = str(uuid.uuid4())[:8]
|
| 70 |
SESSION_PATH = f"/tmp/Project_{session_id}"
|
| 71 |
extract_tmp = f"/tmp/extract_{session_id}"
|
| 72 |
+
zip_tmp = f"/tmp/{session_id}.zip"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
try:
|
| 75 |
+
# A. Download & Extract Aset User
|
| 76 |
+
print(f"π₯ [STEP 1] Downloading: {req.filename}...", flush=True)
|
| 77 |
r = requests.get(req.file_url)
|
|
|
|
| 78 |
with open(zip_tmp, 'wb') as f: f.write(r.content)
|
| 79 |
os.makedirs(extract_tmp, exist_ok=True)
|
| 80 |
with zipfile.ZipFile(zip_tmp, 'r') as z: z.extractall(extract_tmp)
|
| 81 |
|
| 82 |
+
# B. Setup Project Unity
|
| 83 |
+
print(f"ποΈ [STEP 2] Setup Unity Project...", flush=True)
|
| 84 |
shutil.copytree(BASE_PROJECT_PATH, SESSION_PATH, ignore=shutil.ignore_patterns("Library", "Temp"))
|
| 85 |
target_input = os.path.join(SESSION_PATH, "Assets", "InputRaw")
|
| 86 |
os.makedirs(target_input, exist_ok=True)
|
| 87 |
|
| 88 |
for item in os.listdir(extract_tmp):
|
| 89 |
+
src = os.path.join(extract_tmp, item)
|
| 90 |
+
dst = os.path.join(target_input, item)
|
| 91 |
+
if os.path.isdir(src): shutil.copytree(src, dst)
|
| 92 |
+
else: shutil.copy2(src, dst)
|
| 93 |
+
|
| 94 |
+
# C. Jalankan Unity (Batchmode)
|
| 95 |
+
# Unity sekarang akan melakukan perampingan dan MENANAM mesh ke dalam prefab
|
| 96 |
+
print(f"π [STEP 3] Unity Batchmode (Embedding Mesh)...", flush=True)
|
|
|
|
|
|
|
|
|
|
| 97 |
result = subprocess.run([
|
| 98 |
os.environ["UNITY_PATH"], "-batchmode", "-nographics", "-quit",
|
| 99 |
"-projectPath", SESSION_PATH,
|
| 100 |
"-executeMethod", "TestBuilder.ManualConvert",
|
| 101 |
"-logFile", "/dev/stdout"
|
| 102 |
+
], capture_output=False)
|
| 103 |
+
|
| 104 |
+
# D. Ambil Hasil .zepeto dari Unity
|
| 105 |
+
# C# sudah kita setting untuk membuat file .zepeto di root SESSION_PATH
|
| 106 |
+
zepeto_pattern = os.path.join(SESSION_PATH, "*.zepeto")
|
| 107 |
+
found_files = glob.glob(zepeto_pattern)
|
| 108 |
+
|
| 109 |
+
if found_files:
|
| 110 |
+
# Mengambil file terbaru yang dibuat Unity
|
| 111 |
+
final_zepeto = max(found_files, key=os.path.getctime)
|
| 112 |
+
unique_filename = os.path.basename(final_zepeto)
|
| 113 |
+
print(f"π¦ [STEP 4] Unity Packing Sukses: {unique_filename}", flush=True)
|
| 114 |
+
|
| 115 |
+
# E. Upload ke R2
|
| 116 |
upload_to_r2(final_zepeto, unique_filename)
|
| 117 |
else:
|
| 118 |
+
print("β [ERROR] Unity tidak menghasilkan file .zepeto dengan mode Embedded!", flush=True)
|
| 119 |
|
| 120 |
except Exception as e:
|
| 121 |
+
print(f"π₯ [CRITICAL] Pipeline Error: {e}", flush=True)
|
| 122 |
finally:
|
| 123 |
+
# F. Cleanup folder kerja
|
| 124 |
+
print("π§Ή [CLEANUP] Membersihkan sesi...", flush=True)
|
| 125 |
+
for p in [SESSION_PATH, extract_tmp, zip_tmp]:
|
| 126 |
if os.path.exists(p):
|
| 127 |
shutil.rmtree(p) if os.path.isdir(p) else os.remove(p)
|
| 128 |
|