Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,9 +5,12 @@ import requests
|
|
| 5 |
import zipfile
|
| 6 |
import uuid
|
| 7 |
import boto3
|
|
|
|
| 8 |
from fastapi import FastAPI, BackgroundTasks
|
|
|
|
| 9 |
from pydantic import BaseModel
|
| 10 |
|
|
|
|
| 11 |
os.chdir("/tmp")
|
| 12 |
os.environ["HOME"] = "/tmp"
|
| 13 |
os.environ["UNITY_PATH"] = "/opt/unity/Editor/Unity"
|
|
@@ -26,55 +29,63 @@ class ConversionRequest(BaseModel):
|
|
| 26 |
file_url: str
|
| 27 |
filename: str
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
zip_tmp = f"/tmp/{session_id}.zip"
|
| 40 |
-
final_zepeto = f"/tmp/{req.filename}.zepeto"
|
| 41 |
|
| 42 |
try:
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
subprocess.run([os.environ["UNITY_PATH"], "-batchmode", "-nographics", "-quit",
|
| 59 |
-
"-projectPath", SESSION_PATH, "-executeMethod", "TestBuilder.ManualConvert",
|
| 60 |
-
"-logFile", "/dev/stdout"], capture_output=False)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
s3 = boto3.client('s3', endpoint_url=f"https://{R2_CONFIG['account_id']}.r2.cloudflarestorage.com",
|
| 67 |
-
aws_access_key_id=R2_CONFIG['access_key'], aws_secret_access_key=R2_CONFIG['secret_key'])
|
| 68 |
-
s3.upload_file(final_zepeto, R2_CONFIG['bucket_name'], f"packed/{req.filename}.zepeto")
|
| 69 |
-
print(f"✅ [SUCCESS] Done.", flush=True)
|
| 70 |
-
else:
|
| 71 |
-
print("❌ [ERROR] No bundle found.", flush=True)
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
|
| 78 |
-
async def api_convert(req: ConversionRequest, background_tasks: BackgroundTasks):
|
| 79 |
-
background_tasks.add_task(process_pipeline, req)
|
| 80 |
-
return {"status": "processing"}
|
|
|
|
| 5 |
import zipfile
|
| 6 |
import uuid
|
| 7 |
import boto3
|
| 8 |
+
import glob
|
| 9 |
from fastapi import FastAPI, BackgroundTasks
|
| 10 |
+
from fastapi.responses import FileResponse, JSONResponse
|
| 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 |
file_url: str
|
| 30 |
filename: str
|
| 31 |
|
| 32 |
+
# --- ENDPOINT REQUEST LICENSE (.alf) ---
|
| 33 |
+
@app.get("/lisensi")
|
| 34 |
+
async def get_unity_alf():
|
| 35 |
+
"""Menghasilkan file .alf untuk aktivasi manual Unity"""
|
| 36 |
+
alf_filename = "Unity_v2020.3.9f1.alf"
|
| 37 |
+
alf_path = f"/tmp/{alf_filename}"
|
| 38 |
+
|
| 39 |
+
# Hapus jika file lama masih ada
|
| 40 |
+
if os.path.exists(alf_path):
|
| 41 |
+
os.remove(alf_path)
|
|
|
|
|
|
|
| 42 |
|
| 43 |
try:
|
| 44 |
+
print("🎫 [LICENSE] Menghasilkan file .alf...", flush=True)
|
| 45 |
+
# Perintah Unity untuk membuat Activation Request
|
| 46 |
+
subprocess.run([
|
| 47 |
+
os.environ["UNITY_PATH"],
|
| 48 |
+
"-batchmode",
|
| 49 |
+
"-nographics",
|
| 50 |
+
"-createManualActivationFile",
|
| 51 |
+
"-logfile", "/dev/stdout"
|
| 52 |
+
], cwd="/tmp", timeout=60)
|
| 53 |
|
| 54 |
+
# Unity biasanya menghasilkan file dengan pola *.alf di folder saat ini (cwd)
|
| 55 |
+
found_alf = glob.glob("/tmp/*.alf")
|
| 56 |
+
|
| 57 |
+
if found_alf:
|
| 58 |
+
target_alf = found_alf[0]
|
| 59 |
+
print(f"✅ [LICENSE] File .alf ditemukan: {target_alf}", flush=True)
|
| 60 |
+
return FileResponse(
|
| 61 |
+
path=target_alf,
|
| 62 |
+
filename=os.path.basename(target_alf),
|
| 63 |
+
media_type='application/octet-stream'
|
| 64 |
+
)
|
| 65 |
+
else:
|
| 66 |
+
return JSONResponse(status_code=500, content={"error": "File .alf tidak berhasil dibuat oleh Unity."})
|
| 67 |
|
| 68 |
+
except Exception as e:
|
| 69 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
# --- PIPELINE UTAMA ---
|
| 72 |
+
@app.get("/")
|
| 73 |
+
async def health_check():
|
| 74 |
+
return {"status": "ready", "engine": "Unity 2020.3.9f1 - Manual License Support"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
+
def setup_license():
|
| 77 |
+
# Mencari file .ulf di root aplikasi (hasil upload manual kamu nanti)
|
| 78 |
+
ulf_name = "Unity_lic.ulf" # Sesuaikan dengan nama file yang kamu upload ke repo
|
| 79 |
+
source_ulf = os.path.join("/app", ulf_name)
|
| 80 |
+
target_dir = "/tmp/.local/share/unity3d/Unity"
|
| 81 |
+
target_ulf = os.path.join(target_dir, "Unity_lic.ulf")
|
| 82 |
+
|
| 83 |
+
try:
|
| 84 |
+
if os.path.exists(source_ulf):
|
| 85 |
+
os.makedirs(target_dir, exist_ok=True)
|
| 86 |
+
shutil.copy2(source_ulf, target_ulf)
|
| 87 |
+
print(f"✅ [LICENSE] Lisensi .ulf terpasang dari {source_ulf}", flush=True)
|
| 88 |
+
except Exception as e:
|
| 89 |
+
print(f"❌ [LICENSE] Gagal pasang .ulf: {e}", flush=True)
|
| 90 |
|
| 91 |
+
# ... (sisanya sama seperti app.py sebelumnya: make_zepeto_archive, process_pipeline, dll) ...
|
|
|
|
|
|
|
|
|