"No file"]); exit; } $siteId = substr(md5(uniqid()), 0, 8); $extractPath = "/tmp/sites/" . $siteId; mkdir($extractPath, 0777, true); $zip = new ZipArchive; if ($zip->open($_FILES['site_zip']['tmp_name']) === TRUE) { $zip->extractTo($extractPath); $zip->close(); } // Backup ZIP $zipContent = file_get_contents($_FILES['site_zip']['tmp_name']); $ch = curl_init("https://huggingface.co/api/datasets/{$repoId}/upload/main/sites/{$siteId}.zip"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_POSTFIELDS, $zipContent); curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Bearer {$hfToken}"]); curl_exec($ch); curl_close($ch); // Backup SQLite if it exists if (file_exists($extractPath . "/database.sqlite")) { $dbContent = file_get_contents($extractPath . "/database.sqlite"); $ch = curl_init("https://huggingface.co/api/datasets/{$repoId}/upload/main/sites/{$siteId}_db.sqlite"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_POSTFIELDS, $dbContent); curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Bearer {$hfToken}"]); curl_exec($ch); curl_close($ch); } echo json_encode(["status" => "success", "site_id" => $siteId, "live_url" => $spaceUrl . "/sites/" . $siteId . "/"]); exit; } // --------------------------------------------------------- // GET: UI & Database Hydration // --------------------------------------------------------- if ($method === 'GET') { // If the request is for a site, ensure DB exists if (preg_match('/\/sites\/([a-zA-Z0-9]+)/', $_SERVER['REQUEST_URI'], $matches)) { restoreDatabase($matches[1], $repoId, $hfToken); } // Serve the Upload UI echo '