(() => { const form = document.getElementById('private-download-form'); const message = document.getElementById('private-download-message'); const results = document.getElementById('private-download-results'); let token = ''; const download = async (item, button) => { button.disabled = true; message.textContent = 'Downloading…'; try { const response = await fetch(item.download_path, { headers: {'Authorization': 'Bearer ' + token}, }); if (!response.ok) throw new Error(response.status === 401 ? 'unauthorized' : 'download'); const blob = await response.blob(); const url = URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; link.download = item.file_name; document.body.appendChild(link); link.click(); link.remove(); setTimeout(() => URL.revokeObjectURL(url), 30000); message.textContent = 'Download complete.'; } catch (error) { message.textContent = error.message === 'unauthorized' ? 'The delivery token is invalid or expired.' : 'The download could not be completed.'; } finally { button.disabled = false; } }; const render = (items) => { results.replaceChildren(); items.forEach((item) => { const card = document.createElement('article'); card.className = 'panel release-card'; const target = document.createElement('p'); target.className = 'eyebrow'; target.textContent = [item.operating_system, item.architecture, item.linux_abi] .filter(Boolean).join(' · '); const title = document.createElement('h2'); title.textContent = item.product_name + ' ' + item.version; const detail = document.createElement('p'); detail.textContent = item.channel_name + ' · ' + item.format + ' · ' + item.file_name; const button = document.createElement('button'); button.type = 'button'; button.textContent = 'Download'; button.addEventListener('click', () => download(item, button)); card.append(target, title, detail, button); results.appendChild(card); }); }; form.addEventListener('submit', async (event) => { event.preventDefault(); const product = document.getElementById('private-product').value.trim(); const channel = document.getElementById('private-channel').value.trim(); token = document.getElementById('delivery-token').value.trim(); results.replaceChildren(); message.textContent = 'Opening channel…'; try { const response = await fetch( '/api/public/v1/products/' + encodeURIComponent(product) + '/channels/' + encodeURIComponent(channel) + '/downloads', {headers: {'Authorization': 'Bearer ' + token}}, ); if (!response.ok) { message.textContent = response.status === 401 ? 'The delivery token is invalid or expired.' : 'The private channel was not found.'; return; } const payload = await response.json(); render(payload.items || []); message.textContent = payload.items && payload.items.length ? 'Private channel opened.' : 'This channel has no published artifacts.'; } catch (_) { message.textContent = 'Could not reach the server.'; } }); })();