Spaces:
Sleeping
Sleeping
| header("Access-Control-Allow-Origin: *"); | |
| header("Access-Control-Allow-Methods: GET, POST, OPTIONS"); | |
| header("Access-Control-Allow-Headers: Content-Type, Authorization"); | |
| if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { http_response_code(200); exit(); } | |
| $hfToken = getenv('HF_TOKEN'); | |
| $repoId = "Emalawi19/php-platform-storage"; | |
| $spaceUrl = "https://emalawi19-advanced-php.hf.space"; | |
| $method = $_SERVER['REQUEST_METHOD']; | |
| // Helper to restore DB from Dataset if it's missing from the temporary container | |
| function restoreDatabase($siteId, $repoId, $hfToken) { | |
| $dbPath = "/tmp/sites/{$siteId}/database.sqlite"; | |
| if (!file_exists($dbPath)) { | |
| $url = "https://huggingface.co/datasets/{$repoId}/resolve/main/sites/{$siteId}_db.sqlite"; | |
| $ch = curl_init($url); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
| curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Bearer {$hfToken}"]); | |
| $data = curl_exec($ch); | |
| if (curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200) file_put_contents($dbPath, $data); | |
| curl_close($ch); | |
| } | |
| } | |
| // --------------------------------------------------------- | |
| // POST: Deployment Engine | |
| // --------------------------------------------------------- | |
| if ($method === 'POST') { | |
| if (!isset($_FILES['site_zip'])) { http_response_code(400); echo json_encode(["error" => "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 '<!DOCTYPE html><html><body><h2>Deploy PHP Website</h2><form method="POST" enctype="multipart/form-data"><input type="file" name="site_zip" required><button type="submit">Deploy</button></form></body></html>'; | |
| } | |