Update app/static/script.js
Browse files- app/static/script.js +35 -9
app/static/script.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
document.addEventListener('DOMContentLoaded', () => {
|
| 3 |
// --- State ---
|
| 4 |
let currentFile = null;
|
| 5 |
-
let currentXHR = null;
|
| 6 |
|
| 7 |
// --- DOM Elements ---
|
| 8 |
const states = {
|
|
@@ -22,6 +22,10 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 22 |
const uploadAnotherBtn = document.getElementById('upload-another-btn');
|
| 23 |
const errorMessage = document.getElementById('error-message');
|
| 24 |
const retryBtn = document.getElementById('retry-btn');
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
// --- UI State Management ---
|
| 27 |
function switchState(state) {
|
|
@@ -50,6 +54,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 50 |
|
| 51 |
uploadAnotherBtn.addEventListener('click', resetUploader);
|
| 52 |
retryBtn.addEventListener('click', resetUploader);
|
|
|
|
| 53 |
|
| 54 |
// --- Core Functions ---
|
| 55 |
function handleFileSelect(event) {
|
|
@@ -71,8 +76,6 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 71 |
const formData = new FormData();
|
| 72 |
formData.append('file', file);
|
| 73 |
|
| 74 |
-
// We use XMLHttpRequest instead of fetch because it provides
|
| 75 |
-
// a native 'onprogress' event for tracking upload progress.
|
| 76 |
const xhr = new XMLHttpRequest();
|
| 77 |
currentXHR = xhr;
|
| 78 |
|
|
@@ -87,8 +90,14 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 87 |
xhr.addEventListener('load', () => {
|
| 88 |
if (xhr.status >= 200 && xhr.status < 300) {
|
| 89 |
const response = JSON.parse(xhr.responseText);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
downloadLink.href = response.download_url;
|
| 91 |
-
downloadLink.download = response.filename;
|
|
|
|
|
|
|
| 92 |
switchState('complete');
|
| 93 |
} else {
|
| 94 |
handleUploadError(xhr);
|
|
@@ -101,6 +110,25 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 101 |
xhr.open('POST', '/upload', true);
|
| 102 |
xhr.send(formData);
|
| 103 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
function handleUploadError(xhr) {
|
| 106 |
let message = 'An unknown network error occurred.';
|
|
@@ -108,7 +136,6 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 108 |
const errorData = JSON.parse(xhr.responseText);
|
| 109 |
message = errorData.detail || 'Upload failed.';
|
| 110 |
} catch (e) {
|
| 111 |
-
// Response was not JSON, use status text
|
| 112 |
if (xhr.statusText) {
|
| 113 |
message = xhr.statusText;
|
| 114 |
}
|
|
@@ -119,15 +146,15 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 119 |
|
| 120 |
function resetUploader() {
|
| 121 |
if (currentXHR) {
|
| 122 |
-
currentXHR.abort();
|
| 123 |
currentXHR = null;
|
| 124 |
}
|
| 125 |
currentFile = null;
|
| 126 |
-
fileInput.value = '';
|
|
|
|
| 127 |
switchState('initial');
|
| 128 |
}
|
| 129 |
|
| 130 |
-
// --- Helper ---
|
| 131 |
function formatBytes(bytes, decimals = 2) {
|
| 132 |
if (bytes === 0) return '0 Bytes';
|
| 133 |
const k = 1024;
|
|
@@ -137,6 +164,5 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 137 |
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
|
| 138 |
}
|
| 139 |
|
| 140 |
-
// --- Initial State ---
|
| 141 |
switchState('initial');
|
| 142 |
});
|
|
|
|
| 2 |
document.addEventListener('DOMContentLoaded', () => {
|
| 3 |
// --- State ---
|
| 4 |
let currentFile = null;
|
| 5 |
+
let currentXHR = null;
|
| 6 |
|
| 7 |
// --- DOM Elements ---
|
| 8 |
const states = {
|
|
|
|
| 22 |
const uploadAnotherBtn = document.getElementById('upload-another-btn');
|
| 23 |
const errorMessage = document.getElementById('error-message');
|
| 24 |
const retryBtn = document.getElementById('retry-btn');
|
| 25 |
+
|
| 26 |
+
// New elements for copy functionality
|
| 27 |
+
const downloadUrlInput = document.getElementById('download-url-input');
|
| 28 |
+
const copyLinkBtn = document.getElementById('copy-link-btn');
|
| 29 |
|
| 30 |
// --- UI State Management ---
|
| 31 |
function switchState(state) {
|
|
|
|
| 54 |
|
| 55 |
uploadAnotherBtn.addEventListener('click', resetUploader);
|
| 56 |
retryBtn.addEventListener('click', resetUploader);
|
| 57 |
+
copyLinkBtn.addEventListener('click', handleCopyLink);
|
| 58 |
|
| 59 |
// --- Core Functions ---
|
| 60 |
function handleFileSelect(event) {
|
|
|
|
| 76 |
const formData = new FormData();
|
| 77 |
formData.append('file', file);
|
| 78 |
|
|
|
|
|
|
|
| 79 |
const xhr = new XMLHttpRequest();
|
| 80 |
currentXHR = xhr;
|
| 81 |
|
|
|
|
| 90 |
xhr.addEventListener('load', () => {
|
| 91 |
if (xhr.status >= 200 && xhr.status < 300) {
|
| 92 |
const response = JSON.parse(xhr.responseText);
|
| 93 |
+
|
| 94 |
+
// Construct the full, absolute URL for sharing
|
| 95 |
+
const fullUrl = `${window.location.origin}${response.download_url}`;
|
| 96 |
+
|
| 97 |
downloadLink.href = response.download_url;
|
| 98 |
+
downloadLink.download = response.filename;
|
| 99 |
+
downloadUrlInput.value = fullUrl;
|
| 100 |
+
|
| 101 |
switchState('complete');
|
| 102 |
} else {
|
| 103 |
handleUploadError(xhr);
|
|
|
|
| 110 |
xhr.open('POST', '/upload', true);
|
| 111 |
xhr.send(formData);
|
| 112 |
}
|
| 113 |
+
|
| 114 |
+
// NEW: Function to handle copying the link
|
| 115 |
+
function handleCopyLink() {
|
| 116 |
+
// Use the modern Navigator Clipboard API
|
| 117 |
+
navigator.clipboard.writeText(downloadUrlInput.value).then(() => {
|
| 118 |
+
// Provide visual feedback
|
| 119 |
+
copyLinkBtn.textContent = 'Copied!';
|
| 120 |
+
copyLinkBtn.classList.add('copied');
|
| 121 |
+
|
| 122 |
+
// Revert back after a couple of seconds
|
| 123 |
+
setTimeout(() => {
|
| 124 |
+
copyLinkBtn.textContent = 'Copy';
|
| 125 |
+
copyLinkBtn.classList.remove('copied');
|
| 126 |
+
}, 2000);
|
| 127 |
+
}).catch(err => {
|
| 128 |
+
console.error('Failed to copy text: ', err);
|
| 129 |
+
// You could add an error state to the button here if needed
|
| 130 |
+
});
|
| 131 |
+
}
|
| 132 |
|
| 133 |
function handleUploadError(xhr) {
|
| 134 |
let message = 'An unknown network error occurred.';
|
|
|
|
| 136 |
const errorData = JSON.parse(xhr.responseText);
|
| 137 |
message = errorData.detail || 'Upload failed.';
|
| 138 |
} catch (e) {
|
|
|
|
| 139 |
if (xhr.statusText) {
|
| 140 |
message = xhr.statusText;
|
| 141 |
}
|
|
|
|
| 146 |
|
| 147 |
function resetUploader() {
|
| 148 |
if (currentXHR) {
|
| 149 |
+
currentXHR.abort();
|
| 150 |
currentXHR = null;
|
| 151 |
}
|
| 152 |
currentFile = null;
|
| 153 |
+
fileInput.value = '';
|
| 154 |
+
downloadUrlInput.value = ''; // Clear the URL input
|
| 155 |
switchState('initial');
|
| 156 |
}
|
| 157 |
|
|
|
|
| 158 |
function formatBytes(bytes, decimals = 2) {
|
| 159 |
if (bytes === 0) return '0 Bytes';
|
| 160 |
const k = 1024;
|
|
|
|
| 164 |
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
|
| 165 |
}
|
| 166 |
|
|
|
|
| 167 |
switchState('initial');
|
| 168 |
});
|