File size: 22,116 Bytes
8851dfe 4465ff2 8851dfe 4465ff2 8851dfe 4465ff2 8851dfe 4465ff2 8851dfe 4465ff2 8851dfe d63622b 8851dfe d63622b 4465ff2 d63622b 8851dfe 4465ff2 8851dfe 4465ff2 f2932a1 4465ff2 f2932a1 4465ff2 f2932a1 4465ff2 8851dfe 4465ff2 8851dfe 4465ff2 f2932a1 8851dfe 4465ff2 8851dfe 08539dc 8851dfe 4a09dab 8851dfe 4465ff2 4a09dab 4465ff2 4a09dab 4465ff2 d63622b 4465ff2 8851dfe 4465ff2 8851dfe | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 | #!/usr/bin/env python3
"""
LocalSpace Deployer β Hugging Face Space (Vanilla)
Drag a DMG. It gets OPENED on the server: extracted, inspected, and its
app bundle metadata is displayed. The Space itself hosts the DMG contents.
Pure FastAPI + HTML/JS. No Gradio, no Streamlit, no API keys.
Public by default.
"""
from __future__ import annotations
import hashlib
import json
import os
import plistlib
import re
import shutil
import subprocess
import time
import uuid
from pathlib import Path
from typing import Any
from fastapi import FastAPI, File, Request, UploadFile
from fastapi.responses import FileResponse, HTMLResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
# Try biplist for binary plists, fall back to plistlib
try:
import biplist
HAS_BIPLIST = True
except ImportError:
HAS_BIPLIST = False
DATA_DIR = Path("data")
DATA_DIR.mkdir(exist_ok=True)
UPLOAD_DIR = DATA_DIR / "uploads"
UPLOAD_DIR.mkdir(exist_ok=True)
EXTRACT_DIR = DATA_DIR / "extracted"
EXTRACT_DIR.mkdir(exist_ok=True)
DB_PATH = DATA_DIR / "apps.json"
app = FastAPI(title="LocalSpace Deployer")
app.mount("/static", StaticFiles(directory="static"), name="static")
_store: dict[str, dict[str, Any]] = {}
def _load_db() -> None:
global _store
if DB_PATH.exists():
with open(DB_PATH) as f:
_store = json.load(f)
else:
_store = {}
def _save_db() -> None:
with open(DB_PATH, "w") as f:
json.dump(_store, f, indent=2)
def _slugify(name: str) -> str:
return re.sub(r"[^a-z0-9]+", "-", name.lower().replace(".dmg", "")).strip("-")
def _hash_file(path: Path) -> str:
h = hashlib.sha256()
with open(path, "rb") as f:
for chunk in iter(lambda: f.read(65536), b""):
h.update(chunk)
return h.hexdigest()
def _read_plist(path: Path) -> dict[str, Any]:
"""Read a plist file (XML or binary)."""
try:
with open(path, "rb") as f:
data = f.read()
try:
return plistlib.loads(data)
except Exception:
pass
if HAS_BIPLIST:
try:
return biplist.readPlistFromString(data)
except Exception:
pass
except Exception:
pass
return {}
def _find_app_bundles(root: Path) -> list[Path]:
"""Find all .app directories under root."""
apps = []
for p in root.rglob("*.app"):
if p.is_dir():
apps.append(p)
return apps
def _extract_dmg(dmg_path: Path, dest: Path) -> bool:
"""Extract DMG using 7z. Returns True on success."""
try:
dest.mkdir(parents=True, exist_ok=True)
result = subprocess.run(
["7z", "x", "-y", "-o" + str(dest), str(dmg_path)],
capture_output=True,
text=True,
timeout=120,
)
return result.returncode == 0
except Exception:
return False
def _inspect_app_bundle(app_path: Path) -> dict[str, Any]:
"""Read Info.plist and extract metadata from an .app bundle."""
info_plist = app_path / "Contents" / "Info.plist"
if not info_plist.exists():
info_plist = app_path / "Info.plist"
info = _read_plist(info_plist) if info_plist.exists() else {}
icon_name = info.get("CFBundleIconFile", "")
icon_path = None
if icon_name:
icons_dir = app_path / "Contents" / "Resources"
if icons_dir.exists():
icns = icons_dir / (icon_name if icon_name.endswith(".icns") else icon_name + ".icns")
if icns.exists():
icon_path = str(icns)
files = []
if app_path.exists():
for f in sorted(app_path.rglob("*")):
if f.is_file():
try:
rel = str(f.relative_to(app_path))
files.append(rel)
except ValueError:
pass
return {
"bundle_name": app_path.name,
"bundle_id": info.get("CFBundleIdentifier", ""),
"display_name": info.get("CFBundleDisplayName", "") or info.get("CFBundleName", ""),
"version": info.get("CFBundleShortVersionString", "") or info.get("CFBundleVersion", ""),
"min_os_version": info.get("LSMinimumSystemVersion", ""),
"executable": info.get("CFBundleExecutable", ""),
"icon_path": icon_path,
"info_plist": info,
"file_count": len(files),
"files": files[:200],
}
def _open_dmg(app_id: str, dmg_path: Path) -> dict[str, Any]:
"""Open a DMG: extract it, find apps, inspect them."""
extract_to = EXTRACT_DIR / app_id
if extract_to.exists():
shutil.rmtree(extract_to)
success = _extract_dmg(dmg_path, extract_to)
if not success:
return {"opened": False, "error": "Extraction failed. DMG may be encrypted or use an unsupported format."}
apps = _find_app_bundles(extract_to)
inspected = [_inspect_app_bundle(a) for a in apps]
# Build top-level tree
tree = []
for item in sorted(extract_to.iterdir()):
tree.append({
"name": item.name,
"type": "directory" if item.is_dir() else "file",
"size": item.stat().st_size if item.is_file() else 0,
})
# Discover all HTML files recursively for iframe preview
html_files: list[dict[str, Any]] = []
for f in extract_to.rglob("*.html"):
rel = f.relative_to(extract_to)
depth = len(rel.parts)
html_files.append({"path": str(rel), "depth": depth, "size": f.stat().st_size})
# Prefer shallowest HTML, then shortest path, then largest
html_files.sort(key=lambda x: (x["depth"], len(x["path"]), -x["size"]))
return {
"opened": True,
"extracted_path": str(extract_to),
"apps_found": len(inspected),
"apps": inspected,
"tree": tree,
"html_files": html_files[:50],
"has_preview": bool(html_files),
"preview_entry": html_files[0]["path"] if html_files else None,
}
_load_db()
@app.get("/", response_class=HTMLResponse)
def index(request: Request) -> HTMLResponse:
with open("static/index.html") as f:
return HTMLResponse(content=f.read())
@app.post("/api/upload")
async def upload_file(file: UploadFile = File(...)) -> JSONResponse:
if not file.filename or not file.filename.lower().endswith(".dmg"):
return JSONResponse({"error": "Only .dmg files are accepted."}, status_code=400)
app_id = str(uuid.uuid4())
slug = _slugify(file.filename)
base_slug = slug
counter = 1
while any(a.get("slug") == slug for a in _store.values()):
slug = f"{base_slug}-{counter}"
counter += 1
dest = UPLOAD_DIR / f"{app_id}.dmg"
with open(dest, "wb") as f:
while True:
chunk = await file.read(65536)
if not chunk:
break
f.write(chunk)
sha256 = _hash_file(dest)
size = dest.stat().st_size
opened = _open_dmg(app_id, dest)
entry = {
"app_id": app_id,
"slug": slug,
"filename": file.filename,
"size": size,
"sha256": sha256,
"download_url": f"/api/download/{app_id}",
"opened": opened.get("opened", False),
"apps_found": opened.get("apps_found", 0),
"apps": opened.get("apps", []),
"tree": opened.get("tree", []),
"html_files": opened.get("html_files", []),
"has_preview": opened.get("has_preview", False),
"preview_entry": opened.get("preview_entry", None),
"created_at": time.time(),
}
_store[app_id] = entry
_save_db()
return JSONResponse({
"app": entry,
"message": "DMG uploaded and opened." if entry["opened"] else "DMG uploaded but could not be opened.",
}, status_code=201)
@app.get("/api/apps")
def list_apps() -> JSONResponse:
apps = sorted(_store.values(), key=lambda a: a["created_at"], reverse=True)
return JSONResponse({"apps": apps})
@app.get("/app/{app_id}", response_class=HTMLResponse)
def serve_app(app_id: str) -> HTMLResponse:
"""Serve a published app as a full-page iframe viewer."""
entry = _store.get(app_id)
if not entry:
return HTMLResponse("<h1>Not found</h1>", status_code=404)
if not entry.get("has_preview"):
return HTMLResponse("<h1>This app has no web preview</h1>", status_code=404)
entry_path = entry.get("preview_entry", "")
iframe_src = f"/api/preview/{app_id}/{entry_path}" if entry_path else f"/api/preview/{app_id}"
app_name = entry.get("filename", "App")
display_name = ""
if entry.get("apps") and entry["apps"]:
display_name = entry["apps"][0].get("display_name", "") or entry["apps"][0].get("bundle_name", "")
if not display_name:
display_name = app_name.replace(".dmg", "").replace(".zip", "")
html = f'''<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{display_name} β LocalSpace</title>
<style>
* {{ box-sizing: border-box; margin: 0; }}
body {{ background: #0a0a0f; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; height: 100vh; display: flex; flex-direction: column; overflow: hidden; }}
header {{ background: #14141a; border-bottom: 1px solid #2a2a35; padding: 12px 20px; display: flex; align-items: center; justify-content: space-between; flex-shrink: 0; }}
header .title {{ color: #e2e2e8; font-weight: 600; font-size: 1rem; display: flex; align-items: center; gap: 10px; }}
header .title .badge {{ background: rgba(34,197,94,0.15); color: #22c55e; padding: 2px 8px; border-radius: 999px; font-size: 0.72rem; font-weight: 500; }}
header .actions {{ display: flex; gap: 10px; }}
header .actions a, header .actions button {{ background: #1c1c24; border: 1px solid #2a2a35; color: #e2e2e8; padding: 6px 14px; border-radius: 8px; font-size: 0.85rem; text-decoration: none; cursor: pointer; transition: background 0.15s; }}
header .actions a:hover, header .actions button:hover {{ background: #2a2a35; }}
.iframe-wrap {{ flex: 1; position: relative; overflow: hidden; }}
iframe {{ width: 100%; height: 100%; border: none; display: block; }}
.loading {{ position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; background: #0a0a0f; color: #888898; font-size: 0.95rem; z-index: 1; }}
</style>
</head>
<body>
<header>
<div class="title">
⬑ {display_name}
<span class="badge">Live</span>
</div>
<div class="actions">
<a href="/" title="Back to deployer">β Back</a>
<a href="{iframe_src}" target="_blank" title="Open in new tab">β Open</a>
<button onclick="document.querySelector('iframe').requestFullscreen()">βΆ Fullscreen</button>
</div>
</header>
<div class="iframe-wrap">
<div class="loading" id="loader">Loading app...</div>
<iframe src="{iframe_src}" onload="document.getElementById('loader').style.display='none'"></iframe>
</div>
</body>
</html>'''
return HTMLResponse(content=html)
@app.get("/api/apps/{app_id}")
def get_app(app_id: str) -> JSONResponse:
entry = _store.get(app_id)
if not entry:
return JSONResponse({"error": "Not found"}, status_code=404)
return JSONResponse({"app": entry})
@app.get("/api/download/{app_id}")
def download_app(app_id: str):
entry = _store.get(app_id)
if not entry:
return JSONResponse({"error": "Not found"}, status_code=404)
path = UPLOAD_DIR / f"{app_id}.dmg"
if not path.exists():
return JSONResponse({
"error": "File not found on disk",
"detail": "This upload was stored in temporary storage that was cleared during a Space restart. Please re-upload the DMG."
}, status_code=404)
return FileResponse(
path=path,
filename=entry["filename"],
media_type="application/x-apple-diskimage",
)
@app.get("/api/browse/{app_id}/{path:path}")
def browse_extracted(app_id: str, path: str):
entry = _store.get(app_id)
if not entry:
return JSONResponse({"error": "Not found"}, status_code=404)
safe_path = Path(path).name if not path else path
base = EXTRACT_DIR / app_id
target = base / safe_path
if not base.exists():
return JSONResponse({
"error": "Extracted files not found",
"detail": "This upload was stored in temporary storage that was cleared during a Space restart. Please re-upload the DMG."
}, status_code=404)
try:
target.resolve().relative_to(base.resolve())
except ValueError:
return JSONResponse({"error": "Access denied"}, status_code=403)
if not target.exists():
return JSONResponse({"error": "Not found"}, status_code=404)
if target.is_dir():
items = []
for item in sorted(target.iterdir()):
items.append({
"name": item.name,
"type": "directory" if item.is_dir() else "file",
"size": item.stat().st_size if item.is_file() else 0,
})
return JSONResponse({"items": items})
return FileResponse(path=target)
# βββ Preview extracted HTML content in iframe ββββββββββββββββββββββββββββββ
@app.get("/api/preview/{app_id}/{path:path}")
def preview_content(app_id: str, path: str):
"""Serve extracted DMG content for iframe preview."""
entry = _store.get(app_id)
if not entry:
return JSONResponse({"error": "Not found"}, status_code=404)
base = EXTRACT_DIR / app_id
if not base.exists():
return JSONResponse({
"error": "Extracted files not found",
"detail": "This upload was stored in temporary storage that was cleared during a Space restart. Please re-upload the DMG."
}, status_code=404)
if path:
target = base / path
else:
# Default to index.html if no path
target = base / "index.html"
if not target.exists():
# Find any HTML file
for f in base.rglob("*.html"):
target = f
break
if not target.exists():
return JSONResponse({"error": "Not found"}, status_code=404)
# Security check
try:
target.resolve().relative_to(base.resolve())
except ValueError:
return JSONResponse({"error": "Access denied"}, status_code=403)
if target.is_dir():
# Look for index.html in directory
idx = target / "index.html"
if idx.exists():
target = idx
else:
return JSONResponse({"error": "No index.html"}, status_code=404)
mime_types = {
".html": "text/html",
".htm": "text/html",
".js": "application/javascript",
".css": "text/css",
".png": "image/png",
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".gif": "image/gif",
".svg": "image/svg+xml",
".json": "application/json",
".woff2": "font/woff2",
".woff": "font/woff",
".ttf": "font/ttf",
}
suffix = target.suffix.lower()
media_type = mime_types.get(suffix, "application/octet-stream")
# For HTML, inject base tag to handle relative paths
if media_type == "text/html":
content = target.read_text(errors="replace")
# Inject base tag after <head>
if "<base" not in content:
rel_dir = str(target.parent.relative_to(base)) if target.parent != base else ""
base_tag = f'<base href="/api/preview/{app_id}/{rel_dir}/">' if rel_dir else f'<base href="/api/preview/{app_id}/">'
content = content.replace("<head>", f"<head>{base_tag}", 1)
content = content.replace("<HEAD>", f"<HEAD>{base_tag}", 1)
return HTMLResponse(content=content, media_type=media_type)
return FileResponse(path=target, media_type=media_type)
# βββ Create DMG from iframe URL (Web App β macOS App) βββββββββββββββββββββ
WEBAPP_DIR = DATA_DIR / "webapps"
WEBAPP_DIR.mkdir(exist_ok=True)
def _create_app_bundle(url: str, app_name: str, bundle_id: str, version: str) -> Path:
"""Create a minimal macOS .app bundle that opens a URL."""
bundle_root = WEBAPP_DIR / f"{app_name}.app"
if bundle_root.exists():
shutil.rmtree(bundle_root)
contents = bundle_root / "Contents"
macos = contents / "MacOS"
resources = contents / "Resources"
macos.mkdir(parents=True)
resources.mkdir(parents=True)
# Info.plist
plist = {
"CFBundleDevelopmentRegion": "en",
"CFBundleExecutable": app_name.replace(" ", ""),
"CFBundleIdentifier": bundle_id,
"CFBundleInfoDictionaryVersion": "6.0",
"CFBundleName": app_name,
"CFBundlePackageType": "APPL",
"CFBundleShortVersionString": version,
"CFBundleVersion": version,
"LSMinimumSystemVersion": "10.15",
"LSUIElement": False,
}
with open(contents / "Info.plist", "wb") as f:
plistlib.dump(plist, f)
# Wrapper script that opens URL
script_path = macos / app_name.replace(" ", "")
script_content = f'''#!/bin/bash
# Auto-generated web app wrapper
URL="{url}"
if command -v open >/dev/null 2>&1; then
open "$URL"
else
# Fallback for Linux testing
xdg-open "$URL" 2>/dev/null || python3 -m webbrowser "$URL"
fi
'''
script_path.write_text(script_content)
script_path.chmod(0o755)
# Create a local HTML file as backup/embedded view
html_path = resources / "index.html"
html_path.write_text(f'''<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>{app_name}</title>
<style>body{{margin:0;height:100vh;display:flex;flex-direction:column;align-items:center;justify-content:center;font-family:sans-serif;background:#0a0a0f;color:#e2e2e8}}iframe{{width:95%;height:85%;border:1px solid #333;border-radius:8px}}</style></head>
<body><h2>{app_name}</h2><p>Loading {url}...</p><iframe src="{url}" sandbox="allow-scripts allow-same-origin allow-popups allow-forms"></iframe></body></html>
''')
return bundle_root
def _create_dmg_from_app(app_bundle: Path, output_name: str) -> Path | None:
"""Best-effort DMG creation. Returns path to DMG or None."""
dmg_path = WEBAPP_DIR / f"{output_name}.dmg"
# Try genisoimage + dmg if available (Linux)
try:
iso_path = WEBAPP_DIR / f"{output_name}.iso"
result = subprocess.run(
["genisoimage", "-D", "-V", output_name, "-no-pad", "-r", "-apple",
"-o", str(iso_path), str(app_bundle)],
capture_output=True, text=True, timeout=30,
)
if result.returncode == 0:
# Try converting ISO to DMG
dmg_result = subprocess.run(
["dmg", "iso", str(iso_path), str(dmg_path)],
capture_output=True, text=True, timeout=30,
)
if dmg_result.returncode == 0 and dmg_path.exists():
iso_path.unlink(missing_ok=True)
return dmg_path
except FileNotFoundError:
pass
except Exception:
pass
# Fallback: create a ZIP that user can extract on Mac and run hdiutil
zip_path = WEBAPP_DIR / f"{output_name}.zip"
shutil.make_archive(
base_name=str(WEBAPP_DIR / output_name),
format="zip",
root_dir=str(app_bundle.parent),
base_dir=app_bundle.name,
)
if zip_path.exists():
return zip_path
return None
@app.post("/api/create-webapp")
async def create_webapp(request: Request) -> JSONResponse:
"""Create a macOS app bundle + DMG from a URL."""
try:
data = await request.json()
except Exception:
return JSONResponse({"error": "Invalid JSON"}, status_code=400)
url = data.get("url", "").strip()
app_name = data.get("app_name", "WebApp").strip()
bundle_id = data.get("bundle_id", "app.localspace.webapp").strip()
version = data.get("version", "1.0.0").strip()
if not url:
return JSONResponse({"error": "URL is required"}, status_code=400)
if not app_name:
return JSONResponse({"error": "App name is required"}, status_code=400)
# Sanitize
app_name_safe = re.sub(r'[^a-zA-Z0-9 ]+', '', app_name).strip()
if not app_name_safe:
app_name_safe = "WebApp"
try:
bundle = _create_app_bundle(url, app_name_safe, bundle_id, version)
pkg = _create_dmg_from_app(bundle, app_name_safe.replace(" ", "-"))
if pkg is None:
return JSONResponse({"error": "Failed to create package"}, status_code=500)
pkg_id = str(uuid.uuid4())
dest = UPLOAD_DIR / f"{pkg_id}{pkg.suffix}"
shutil.copy2(pkg, dest)
entry = {
"app_id": pkg_id,
"slug": _slugify(app_name_safe),
"filename": dest.name,
"size": dest.stat().st_size,
"sha256": _hash_file(dest),
"download_url": f"/api/download/{pkg_id}",
"source_url": url,
"app_name": app_name_safe,
"bundle_id": bundle_id,
"version": version,
"is_webapp": True,
"created_at": time.time(),
}
_store[pkg_id] = entry
_save_db()
return JSONResponse({
"app": entry,
"message": f"'{app_name_safe}' packaged. Download and extract on macOS, then run 'hdiutil create -srcfolder {app_name_safe}.app {app_name_safe}.dmg' to convert to DMG." if pkg.suffix == ".zip" else f"'{app_name_safe}' DMG created.",
}, status_code=201)
except Exception as exc:
return JSONResponse({"error": str(exc)}, status_code=500)
@app.delete("/api/apps/{app_id}")
def delete_app(app_id: str) -> JSONResponse:
entry = _store.pop(app_id, None)
if entry:
dmg = UPLOAD_DIR / f"{app_id}.dmg"
if dmg.exists():
dmg.unlink()
extracted = EXTRACT_DIR / app_id
if extracted.exists():
shutil.rmtree(extracted)
_save_db()
return JSONResponse({"ok": True})
|