tinyupload/pages/home.html

298 lines
9.5 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{title}}</title>
<link rel="stylesheet" href="/static/common.css">
<style>
/* Home-specific styles */
.upload-form {
display: flex;
flex-direction: column;
gap: 1.5rem;
}
.file-input-container {
position: relative;
border: 2px dashed var(--primary-color);
border-radius: 8px;
padding: 2rem;
text-align: center;
cursor: pointer;
transition: all 0.3s ease;
}
.file-input-container:hover {
border-color: var(--accent-color);
background-color: rgba(79, 195, 247, 0.05);
}
.file-input-container.drag-over {
border-color: var(--accent-color);
background-color: rgba(79, 195, 247, 0.1);
}
.file-input-label {
display: block;
font-weight: 500;
margin-bottom: 0.5rem;
color: var(--secondary-color);
}
.file-input {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
cursor: pointer;
}
.file-name {
margin-top: 1rem;
font-size: 0.9rem;
color: var(--secondary-color);
word-break: break-all;
}
.result-container {
display: none;
margin-top: 2rem;
padding: 1.5rem;
background-color: #e8f5e9;
border-radius: 8px;
border-left: 4px solid var(--success-color);
}
.result-title {
color: var(--success-color);
margin-top: 0;
margin-bottom: 1rem;
}
.file-link {
display: block;
padding: 0.8rem;
color: blue;
background-color: white;
border: 1px solid #ddd;
border-radius: 4px;
word-break: break-all;
margin-bottom: 1rem;
font-family: monospace;
}
.copy-btn {
background-color: var(--secondary-color);
color: white;
border: none;
border-radius: 5px;
padding: 0.5rem 1rem;
font-size: 0.9rem;
cursor: pointer;
transition: background-color 0.3s ease;
width: auto;
}
async fn css_file() -> impl IntoResponse {
// Create headers with content type for CSS
let mut headers = HeaderMap::new();
headers.insert(header::CONTENT_TYPE, "text/css".parse().unwrap());
// Return the CSS content with appropriate headers
(headers, include_str!("../static/common.css"))
}
.copy-btn:hover {
background-color: var(--primary-color);
}
.loading {
display: none;
margin: 1rem auto;
border: 4px solid rgba(0, 0, 0, 0.1);
border-top: 4px solid var(--primary-color);
border-radius: 50%;
width: 30px;
height: 30px;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.error-message {
display: none;
color: #f44336;
background-color: #ffebee;
padding: 1rem;
border-radius: 5px;
margin-top: 1rem;
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<h1>{{title}}</h1>
<form id="uploadForm" class="upload-form" enctype="multipart/form-data">
<div id="fileInputContainer" class="file-input-container">
<span class="file-input-label">Drag & drop a file here or click to browse</span>
<input type="file" id="fileInput" name="file" class="file-input" required>
<div id="fileName" class="file-name"></div>
</div>
<button type="submit" id="uploadBtn" class="upload-btn" disabled>Upload</button>
<div id="loading" class="loading"></div>
<div id="errorMessage" class="error-message"></div>
</form>
<div id="resultContainer" class="result-container">
<h3 class="result-title">Upload Successful!</h3>
<div id="fileLink" class="file-link"></div>
<button id="copyBtn" class="copy-btn">Copy Link</button>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const form = document.getElementById('uploadForm');
const fileInput = document.getElementById('fileInput');
const fileInputContainer = document.getElementById('fileInputContainer');
const fileName = document.getElementById('fileName');
const uploadBtn = document.getElementById('uploadBtn');
const loading = document.getElementById('loading');
const resultContainer = document.getElementById('resultContainer');
const fileLink = document.getElementById('fileLink');
const copyBtn = document.getElementById('copyBtn');
const errorMessage = document.getElementById('errorMessage');
// File input change handler
fileInput.addEventListener('change', function() {
if (this.files.length > 0) {
fileName.textContent = this.files[0].name;
uploadBtn.disabled = false;
} else {
fileName.textContent = '';
uploadBtn.disabled = true;
}
});
// Drag and drop handlers
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
fileInputContainer.addEventListener(eventName, preventDefaults, false);
});
function preventDefaults(e) {
e.preventDefault();
e.stopPropagation();
}
['dragenter', 'dragover'].forEach(eventName => {
fileInputContainer.addEventListener(eventName, highlight, false);
});
['dragleave', 'drop'].forEach(eventName => {
fileInputContainer.addEventListener(eventName, unhighlight, false);
});
function highlight() {
fileInputContainer.classList.add('drag-over');
}
function unhighlight() {
fileInputContainer.classList.remove('drag-over');
}
fileInputContainer.addEventListener('drop', handleDrop, false);
function handleDrop(e) {
const dt = e.dataTransfer;
const files = dt.files;
if (files.length > 0) {
fileInput.files = files;
fileName.textContent = files[0].name;
uploadBtn.disabled = false;
}
}
// Form submission
form.addEventListener('submit', function(e) {
e.preventDefault();
if (!fileInput.files.length) {
return;
}
// Hide any previous error
errorMessage.style.display = 'none';
// Show loading spinner
loading.style.display = 'block';
uploadBtn.disabled = true;
const formData = new FormData();
formData.append('file', fileInput.files[0]);
fetch('/upload', {
method: 'POST',
body: formData
})
.then(response => {
if (!response.ok) {
throw new Error('Upload failed. Please try again.');
}
return response.text();
})
.then(data => {
// Hide loading spinner
loading.style.display = 'none';
// Show result
fileLink.textContent = data;
resultContainer.style.display = 'block';
// Reset form
form.reset();
fileName.textContent = '';
uploadBtn.disabled = true;
})
.catch(error => {
// Hide loading spinner
loading.style.display = 'none';
// Show error message
errorMessage.textContent = error.message;
errorMessage.style.display = 'block';
// Re-enable upload button
uploadBtn.disabled = false;
});
});
// Copy link button
copyBtn.addEventListener('click', function() {
const tempInput = document.createElement('input');
document.body.appendChild(tempInput);
tempInput.value = fileLink.textContent;
tempInput.select();
document.execCommand('copy');
document.body.removeChild(tempInput);
// Change button text temporarily
const originalText = this.textContent;
this.textContent = 'Copied!';
setTimeout(() => {
this.textContent = originalText;
}, 2000);
});
});
</script>
</body>
</html>