feat: functional frontend for uploading api

This commit is contained in:
kossLAN 2025-03-01 21:39:56 -05:00
parent 7f8d940ef5
commit c423174110
Signed by: kossLAN
SSH key fingerprint: SHA256:bdV0x+wdQHGJ6LgmstH3KV8OpWY+OOFmJcPcB0wQPV8
6 changed files with 580 additions and 38 deletions

298
pages/home.html Normal file
View file

@ -0,0 +1,298 @@
<!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>

22
pages/login.html Normal file
View file

@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{title}} - Login</title>
<link rel="stylesheet" href="/static/common.css">
</head>
<body>
<div class="container">
<h1>{{title}} login</h1>
<form action="/login" method="post">
<div class="form-group">
<label for="key">Enter Access Key:</label>
<input type="password" id="key" name="key" required autofocus>
</div>
<button type="submit">Login</button>
<div id="error" class="error"></div>
</form>
</div>
</body>
</html>

97
pages/static/common.css Normal file
View file

@ -0,0 +1,97 @@
:root {
--primary-color: lightblue;
--secondary-color: lightgrey;
--accent-color: #4fc3f7;
--background-color: #2A2E32;
--text-color: white;
--success-color: #4caf50;
--container-bg: #40464C;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: var(--background-color);
color: var(--text-color);
line-height: 1.6;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
}
.container {
width: 90%;
max-width: 600px;
background-color: var(--container-bg);
border-radius: 10px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
padding: 2rem;
margin: 2rem 0;
text-align: center;
}
h1 {
color: var(--primary-color);
margin-bottom: 1.5rem;
}
button {
background-color: var(--primary-color);
color: white;
border: none;
border-radius: 5px;
padding: 0.8rem 1.5rem;
font-size: 1rem;
font-weight: 500;
cursor: pointer;
transition: background-color 0.3s ease;
width: 100%;
}
button:hover {
background-color: var(--secondary-color);
}
button:disabled {
background-color: #cccccc;
cursor: not-allowed;
}
.form-group {
margin-bottom: 1.5rem;
}
label {
display: block;
font-weight: 500;
margin-bottom: 0.5rem;
color: var(--secondary-color);
text-align: left;
}
input {
width: 100%;
padding: 0.8rem;
border: 2px solid var(--secondary-color);
border-radius: 5px;
background-color: rgba(255, 255, 255, 0.1);
color: var(--text-color);
font-size: 1rem;
box-sizing: border-box;
}
input:focus {
border-color: var(--accent-color);
outline: none;
}
.error {
color: #f44336;
text-align: center;
margin-top: 1rem;
font-size: 0.9rem;
}