Explorar o código

Uploads improve

GeoShaPoh hai 5 meses
pai
achega
33b6909be3
Modificáronse 3 ficheiros con 45 adicións e 7 borrados
  1. 4 4
      app.py
  2. 21 3
      static/js/main.js
  3. 20 0
      templates/file_not_found.html

+ 4 - 4
app.py

@@ -9,7 +9,7 @@ import string
 import argparse
 from datetime import datetime, timedelta
 from werkzeug.utils import secure_filename
-from flask import Flask, request, jsonify, render_template, send_from_directory, abort
+from flask import Flask, request, jsonify, render_template, send_from_directory, abort, make_response
 from werkzeug.security import generate_password_hash
 import re
 
@@ -226,18 +226,18 @@ def download_file(file_id):
     conn.close()
     
     if not file_data:
-        abort(404)
+        return make_response(render_template('file_not_found.html'), 404)
     
     original_filename, file_path, expiration_date = file_data
     
     # Check if file has expired
     if datetime.fromisoformat(expiration_date) < datetime.now():
-        abort(410)  # Gone
+        return make_response(render_template('file_not_found.html'), 410)
     
     # Check if file exists
     full_path = os.path.join(app.config['UPLOAD_FOLDER'], file_path)
     if not os.path.exists(full_path):
-        abort(404)
+        return make_response(render_template('file_not_found.html'), 404)
     
     return send_from_directory(app.config['UPLOAD_FOLDER'], file_path, 
                               as_attachment=True, download_name=original_filename)

+ 21 - 3
static/js/main.js

@@ -108,14 +108,19 @@ function getTimeRemaining(expirationDate) {
 // Progreso de subida
 let uploadStartTime = 0;
 let uploadedBytes = 0;
-function updateProgress(loaded, total) {
-    const percent = Math.round((loaded / total) * 100);
+function updateProgress(loaded, total, isDone = false) {
+    const percent = isDone ? 100 : Math.round((loaded / total) * 100);
     const progressBar = document.getElementById('progressBar');
     const progressPercent = document.getElementById('progressPercent');
     const uploadSpeed = document.getElementById('uploadSpeed');
     const timeRemaining = document.getElementById('timeRemaining');
     progressBar.style.width = percent + '%';
     progressPercent.textContent = percent + '%';
+    if (isDone) {
+        uploadSpeed.textContent = '';
+        timeRemaining.textContent = '';
+        return;
+    }
     const elapsed = (Date.now() - uploadStartTime) / 1000;
     const speed = loaded / elapsed;
     const speedMB = (speed / (1024 * 1024)).toFixed(2);
@@ -190,9 +195,12 @@ async function loadUserFiles() {
                             <div class="text-right">
                                 <p class="text-xs text-gray-500 dark:text-gray-400 file-remaining-time" data-expiration="${file.expiration_date}">${getTimeRemaining(file.expiration_date)}</p>
                             </div>
-                            <a href="${file.download_url}" class="inline-flex items-center px-3 py-1 text-xs font-medium text-blue-600 dark:text-blue-400 bg-blue-100 dark:bg-blue-900/30 hover:bg-blue-200 dark:hover:bg-blue-900/50 rounded-full transition-colors">
+                            <a href="${file.download_url}" class="inline-flex items-center px-3 py-1 text-xs font-medium text-blue-600 dark:text-blue-400 bg-blue-100 dark:bg-blue-900/30 hover:bg-blue-200 dark:hover:bg-blue-900/50 rounded-full transition-colors" target="_blank">
                                 Download
                             </a>
+                            <button onclick="copyDownloadUrl('${file.download_url}')" class="inline-flex items-center px-3 py-1 text-xs font-medium text-gray-700 dark:text-gray-200 bg-gray-100 dark:bg-gray-700 hover:bg-gray-200 dark:hover:bg-gray-600 rounded-full transition-colors">
+                                Copy Link
+                            </button>
                             <button onclick="deleteFile('${file.id}')" class="inline-flex items-center px-3 py-1 text-xs font-medium text-red-600 dark:text-red-400 bg-red-100 dark:bg-red-900/30 hover:bg-red-200 dark:hover:bg-red-900/50 rounded-full transition-colors">
                                 Delete
                             </button>
@@ -310,6 +318,8 @@ function setupEventListeners() {
                 }
             });
             xhr.addEventListener('load', function() {
+                // Solo marcar 100% cuando el servidor responde
+                updateProgress(file.size, file.size, true);
                 if (xhr.status === 200) {
                     const result = JSON.parse(xhr.responseText);
                     if (result.success) {
@@ -355,4 +365,12 @@ function updateRemainingTimes() {
         const expiration = el.getAttribute('data-expiration');
         el.textContent = getTimeRemaining(expiration);
     });
+} 
+
+window.copyDownloadUrl = function(url) {
+    navigator.clipboard.writeText(url).then(() => {
+        showToast('Download link copied!');
+    }, () => {
+        showToast('Failed to copy link', 'error');
+    });
 } 

+ 20 - 0
templates/file_not_found.html

@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>File Not Found - HokoriTemp</title>
+    <link rel="icon" type="image/svg+xml" href="/static/favicon.svg">
+    <link rel="stylesheet" href="/static/css/styles.css">
+    <script src="https://cdn.tailwindcss.com"></script>
+</head>
+<body class="bg-gray-50 dark:bg-gray-900 min-h-screen flex items-center justify-center">
+    <div class="bg-white dark:bg-gray-800 rounded-lg shadow-lg p-8 max-w-md text-center">
+        <svg class="w-16 h-16 mx-auto mb-4 text-red-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
+            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
+        </svg>
+        <h1 class="text-2xl font-bold text-gray-900 dark:text-white mb-2">File Not Found</h1>
+        <p class="text-gray-600 dark:text-gray-300 mb-4">The file you are trying to access does not exist or has expired.</p>
+        <a href="/" class="inline-block mt-2 px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700 transition-colors">Go to Home</a>
+    </div>
+</body>
+</html>