Files
beam/index.html.tmpl
Nils O. Selåsdal 82d04ad7cd Pull out index.html
2026-05-29 15:50:09 +02:00

198 lines
6.1 KiB
Cheetah

<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Beam - File Upload Service</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: monospace;
max-width: 800px;
margin: 40px auto;
padding: 20px;
line-height: 1.6;
}
h1 { margin-bottom: 30px; font-size: 2em; }
h2 { margin: 30px 0 15px 0; font-size: 1.3em; }
.section {
background: #f5f5f5;
padding: 20px;
margin: 20px 0;
border-radius: 4px;
}
code {
background: #333;
color: #0f0;
padding: 15px;
display: block;
margin: 10px 0;
border-radius: 4px;
overflow-x: auto;
}
.upload-area {
border: 3px dashed #ccc;
padding: 40px;
text-align: center;
cursor: pointer;
transition: all 0.3s;
}
.upload-area.dragover {
border-color: #333;
background: #f0f0f0;
}
.upload-area:hover {
border-color: #666;
}
input[type="file"] { display: none; }
button {
background: #333;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
font-family: monospace;
font-size: 1em;
border-radius: 4px;
margin-top: 10px;
}
button:hover { background: #555; }
.progress-container {
width: 100%%;
background: #ddd;
border-radius: 4px;
margin: 10px 0;
height: 20px;
display: none;
}
.progress-bar {
height: 100%%;
background: #333;
border-radius: 4px;
width: 0;
transition: width 0.3s;
}
.results {
margin-top: 20px;
}
.result-item {
background: white;
padding: 10px;
margin: 5px 0;
border-radius: 4px;
word-break: break-all;
}
.result-item a {
color: #0066cc;
}
</style>
</head>
<body>
<h1>🚀 Beam - File Upload Service</h1>
<div class="section">
<h2>Upload with curl</h2>
<code>curl --upload-file myfile.txt {{ .Proto }}://{{ .Host }}/myfile.txt</code>
<p style="margin-top: 10px;">The server will respond with a download URL.</p>
</div>
<div class="section">
<h2>Download a file</h2>
<code>curl {{ .Proto }}://{{ .Host }}/x123abcx/myfile.txt -o myfile.txt</code>
<p style="margin-top: 10px;">Or simply open the URL in your browser.</p>
</div>
<div class="section">
<h2>Web Upload</h2>
<div class="upload-area" id="uploadArea">
<p>📁 Click to select files or drag and drop here</p>
<input type="file" id="fileInput" multiple>
<button onclick="document.getElementById('fileInput').click()">Select Files</button>
</div>
<div class="progress-container" id="progressContainer">
<div class="progress-bar" id="progressBar"></div>
</div>
<div class="results" id="results"></div>
</div>
<script>
const uploadArea = document.getElementById('uploadArea');
const fileInput = document.getElementById('fileInput');
const progressContainer = document.getElementById('progressContainer');
const progressBar = document.getElementById('progressBar');
const results = document.getElementById('results');
uploadArea.addEventListener('click', () => fileInput.click());
uploadArea.addEventListener('dragover', (e) => {
e.preventDefault();
uploadArea.classList.add('dragover');
});
uploadArea.addEventListener('dragleave', () => {
uploadArea.classList.remove('dragover');
});
uploadArea.addEventListener('drop', (e) => {
e.preventDefault();
uploadArea.classList.remove('dragover');
const files = e.dataTransfer.files;
uploadFiles(files);
});
fileInput.addEventListener('change', (e) => {
uploadFiles(e.target.files);
});
function uploadFiles(files) {
if (files.length === 0) return;
const formData = new FormData();
for (let file of files) {
formData.append('files', file);
}
progressContainer.style.display = 'block';
progressBar.style.width = '0%%';
results.innerHTML = '';
const xhr = new XMLHttpRequest();
xhr.upload.addEventListener('progress', (e) => {
if (e.lengthComputable) {
const percent = (e.loaded / e.total) * 100;
progressBar.style.width = percent + '%%';
}
});
xhr.addEventListener('load', () => {
if (xhr.status === 200) {
const response = JSON.parse(xhr.responseText);
displayResults(response.urls);
} else {
results.innerHTML = '<div class="result-item">Upload failed: ' + xhr.statusText + '</div>';
}
progressBar.style.width = '100%%';
});
xhr.addEventListener('error', () => {
results.innerHTML = '<div class="result-item">Upload failed: Network error</div>';
});
xhr.open('POST', '/upload');
xhr.send(formData);
}
function displayResults(urls) {
results.innerHTML = '<h3>Download URLs:</h3>';
urls.forEach(url => {
const div = document.createElement('div');
div.className = 'result-item';
div.innerHTML = '<a href="' + url + '" target="_blank">' + url + '</a>';
results.appendChild(div);
});
}
</script>
</body>
</html>