Spaces:
Sleeping
Sleeping
File size: 3,495 Bytes
c0eae55 f45ce34 8550d83 3018cf0 8550d83 f45ce34 3018cf0 8550d83 3018cf0 04234c4 f45ce34 04234c4 3018cf0 04234c4 3018cf0 8550d83 3018cf0 04234c4 3018cf0 f45ce34 04234c4 3018cf0 04234c4 f45ce34 3018cf0 04234c4 3018cf0 04234c4 3018cf0 04234c4 f45ce34 3018cf0 8550d83 3018cf0 04234c4 8550d83 3018cf0 c0eae55 | 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 | <?php
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>';
}
?> |