import wixUsers from 'wix-users'; const API = "http://node-render-balancer-1196454767.eu-west-2.elb.amazonaws.com/"; $w.onReady(async function () { let user = wixUsers.currentUser; if (!user.loggedIn) { await wixUsers.promptLogin(); } const userId = wixUsers.currentUser.id; // Create user in backend await fetch(`${API}/create-user`, { method: "POST", headers: { "x-user-id": userId } }); $w("Button").onChange(() => handleUpload(userId)); }); async function handleUpload(userId) { const file = $w("Button").value[0]; if (!file) return; // ✅ FILE VALIDATION const allowedTypes = ["model/gltf-binary", "model/gltf+json", "application/octet-stream"]; const maxSize = 50 * 1024 * 1024; // 50MB if (!allowedTypes.includes(file.type)) { $w("#statusText").text = "Invalid file type"; return; } if (file.size > maxSize) { $w("#statusText").text = "File too large (max 50MB)"; return; } $w("#statusText").text = "Getting upload URL..."; // ✅ GET PRESIGNED URL const res = await fetch(`${API}/get-presigned-url`, { method: "POST", headers: { "Content-Type": "application/json", "x-user-id": userId }, body: JSON.stringify({ filename: file.name, filetype: file.type }) }); if (!res.ok) { $w("#statusText").text = "Failed to get upload URL"; return; } const data = await res.json(); $w("#statusText").text = "Uploading file..."; // ✅ UPLOAD TO S3 const upload = await fetch(data.url, { method: "PUT", headers: { "Content-Type": file.type }, body: file }); if (!upload.ok) { $w("#statusText").text = "Upload failed"; return; } $w("#statusText").text = "Processing..."; // ✅ POLL FOR RESULT pollResult(data.jobId); } function pollResult(jobId) { let attempts = 0; const interval = setInterval(async () => { attempts++; if (attempts > 60) { clearInterval(interval); $w("#statusText").text = "Timed out"; return; } const res = await fetch(`${API}/job-status?jobId=${jobId}`); const data = await res.json(); // ✅ USING PublicURL AS COMPLETION SIGNAL if (data.PublicURL && data.PublicURL !== "") { clearInterval(interval); $w("#statusText").text = "Ready! Opening..."; // Open in new tab window.open(data.PublicURL, "_blank"); } }, 5000); }