Add fotos.carloselugo.com photo-upload service
Event photo upload app with OAuth2 (Google Drive), Caddy route, and env-var-driven compose config. Refresh token persists to token.json so re-auth isn't needed after container restarts. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,345 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="es">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Admin · Fotos del Evento</title>
|
||||
|
||||
<!--
|
||||
============================================================
|
||||
VISTA DE ADMINISTRACIÓN
|
||||
Permite activar/desactivar la subida de fotos sin tocar el
|
||||
servidor. Protegida por password (ADMIN_PASSWORD en .env).
|
||||
No hay sesión persistente: la contraseña se envía en cada
|
||||
toggle, igual de simple que el host password de NI Quiz.
|
||||
============================================================
|
||||
-->
|
||||
|
||||
<style>
|
||||
:root {
|
||||
--accent: #ec3013;
|
||||
--bg: #faf9f5;
|
||||
--text: #1a1a1a;
|
||||
--muted: #6b6b6b;
|
||||
--success: #1e8e3e;
|
||||
--error: #d93025;
|
||||
--radius: 16px;
|
||||
}
|
||||
|
||||
* { box-sizing: border-box; }
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
background: var(--bg);
|
||||
color: var(--text);
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
padding: 24px;
|
||||
}
|
||||
|
||||
.card {
|
||||
width: 100%;
|
||||
max-width: 380px;
|
||||
background: #fff;
|
||||
border-radius: var(--radius);
|
||||
padding: 28px 24px;
|
||||
box-shadow: 0 4px 24px rgba(0,0,0,0.08);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 1.2rem;
|
||||
margin: 0 0 4px 0;
|
||||
}
|
||||
|
||||
input[type="password"] {
|
||||
width: 100%;
|
||||
padding: 14px;
|
||||
border: 1.5px solid #e0e0e0;
|
||||
border-radius: 10px;
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
button {
|
||||
width: 100%;
|
||||
padding: 14px;
|
||||
border: none;
|
||||
border-radius: 10px;
|
||||
font-size: 1rem;
|
||||
font-weight: 700;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
#loginBtn {
|
||||
background: var(--text);
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.toggle-row {
|
||||
display: none;
|
||||
flex-direction: column;
|
||||
gap: 14px;
|
||||
align-items: center;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.toggle-row.active { display: flex; }
|
||||
|
||||
.switch {
|
||||
position: relative;
|
||||
width: 72px;
|
||||
height: 40px;
|
||||
}
|
||||
|
||||
.switch input { display: none; }
|
||||
|
||||
.slider {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
background: #d93025;
|
||||
border-radius: 999px;
|
||||
cursor: pointer;
|
||||
transition: background 0.25s ease;
|
||||
}
|
||||
|
||||
.slider::before {
|
||||
content: '';
|
||||
position: absolute;
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
left: 4px;
|
||||
top: 4px;
|
||||
background: #fff;
|
||||
border-radius: 50%;
|
||||
transition: transform 0.25s ease;
|
||||
}
|
||||
|
||||
input:checked + .slider {
|
||||
background: var(--success);
|
||||
}
|
||||
|
||||
input:checked + .slider::before {
|
||||
transform: translateX(32px);
|
||||
}
|
||||
|
||||
.state-label {
|
||||
font-weight: 700;
|
||||
font-size: 1rem;
|
||||
}
|
||||
.state-label.on { color: var(--success); }
|
||||
.state-label.off { color: var(--error); }
|
||||
|
||||
#msg {
|
||||
font-size: 0.85rem;
|
||||
min-height: 18px;
|
||||
text-align: center;
|
||||
}
|
||||
#msg.error { color: var(--error); }
|
||||
#msg.success { color: var(--success); }
|
||||
|
||||
.hint {
|
||||
font-size: 0.8rem;
|
||||
color: var(--muted);
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.divider {
|
||||
border: none;
|
||||
border-top: 1px solid #eee;
|
||||
margin: 4px 0;
|
||||
}
|
||||
|
||||
.token-section {
|
||||
display: none;
|
||||
flex-direction: column;
|
||||
gap: 10px;
|
||||
align-items: center;
|
||||
text-align: center;
|
||||
}
|
||||
.token-section.active { display: flex; }
|
||||
|
||||
#tokenBtn {
|
||||
background: #fff;
|
||||
color: var(--text);
|
||||
border: 1.5px solid var(--text);
|
||||
}
|
||||
|
||||
#tokenInfo {
|
||||
font-size: 0.78rem;
|
||||
color: var(--muted);
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div class="card">
|
||||
<h1>🔐 Panel de administración</h1>
|
||||
|
||||
<div id="loginView">
|
||||
<input type="password" id="passwordInput" placeholder="Contraseña" autocomplete="off">
|
||||
<button id="loginBtn" style="margin-top:10px;">Entrar</button>
|
||||
</div>
|
||||
|
||||
<div class="toggle-row" id="toggleRow">
|
||||
<p class="hint">Controla si los asistentes pueden subir fotos ahora mismo.</p>
|
||||
<label class="switch">
|
||||
<input type="checkbox" id="toggleInput">
|
||||
<span class="slider"></span>
|
||||
</label>
|
||||
<div class="state-label" id="stateLabel"></div>
|
||||
|
||||
<hr class="divider">
|
||||
|
||||
<div class="token-section active" id="tokenSection">
|
||||
<p class="hint">Si las subidas empiezan a fallar por autenticación con Google, genera un token nuevo.</p>
|
||||
<button id="tokenBtn">🔄 Generar nuevo token de Google</button>
|
||||
<div id="tokenInfo">Verificando estado del token...</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="msg"></div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
const loginView = document.getElementById('loginView');
|
||||
const passwordInput = document.getElementById('passwordInput');
|
||||
const loginBtn = document.getElementById('loginBtn');
|
||||
const toggleRow = document.getElementById('toggleRow');
|
||||
const toggleInput = document.getElementById('toggleInput');
|
||||
const stateLabel = document.getElementById('stateLabel');
|
||||
const msgEl = document.getElementById('msg');
|
||||
const tokenBtn = document.getElementById('tokenBtn');
|
||||
const tokenInfo = document.getElementById('tokenInfo');
|
||||
|
||||
let currentPassword = '';
|
||||
|
||||
function setMsg(type, text) {
|
||||
msgEl.className = type;
|
||||
msgEl.textContent = text;
|
||||
}
|
||||
|
||||
function renderState(enabled) {
|
||||
toggleInput.checked = enabled;
|
||||
stateLabel.textContent = enabled ? '✅ Subidas ACTIVADAS' : '⛔ Subidas DESACTIVADAS';
|
||||
stateLabel.className = 'state-label ' + (enabled ? 'on' : 'off');
|
||||
}
|
||||
|
||||
function formatDate(iso) {
|
||||
if (!iso) return null;
|
||||
try {
|
||||
return new Date(iso).toLocaleString('es-PR', { dateStyle: 'medium', timeStyle: 'short' });
|
||||
} catch (e) {
|
||||
return iso;
|
||||
}
|
||||
}
|
||||
|
||||
async function loadTokenInfo() {
|
||||
try {
|
||||
const res = await fetch('/admin/oauth/token-info', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ password: currentPassword })
|
||||
});
|
||||
const data = await res.json();
|
||||
|
||||
if (!res.ok || !data.success) {
|
||||
tokenInfo.textContent = 'No se pudo verificar el estado del token.';
|
||||
return;
|
||||
}
|
||||
|
||||
if (!data.hasToken) {
|
||||
tokenInfo.textContent = '⚠️ No hay ningún token configurado todavía.';
|
||||
} else if (data.updatedAt) {
|
||||
tokenInfo.textContent = 'Último generado desde este panel: ' + formatDate(data.updatedAt);
|
||||
} else {
|
||||
tokenInfo.textContent = 'Usando el token inicial configurado en el servidor (.env).';
|
||||
}
|
||||
} catch (err) {
|
||||
tokenInfo.textContent = 'No se pudo verificar el estado del token.';
|
||||
}
|
||||
}
|
||||
|
||||
loginBtn.addEventListener('click', async () => {
|
||||
const password = passwordInput.value.trim();
|
||||
if (!password) return;
|
||||
|
||||
setMsg('', '');
|
||||
loginBtn.disabled = true;
|
||||
loginBtn.textContent = 'Verificando...';
|
||||
|
||||
try {
|
||||
const res = await fetch('/admin/status-check', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ password })
|
||||
});
|
||||
const data = await res.json();
|
||||
|
||||
if (!res.ok || !data.success) {
|
||||
setMsg('error', '❌ ' + (data.error || 'Contraseña incorrecta.'));
|
||||
loginBtn.disabled = false;
|
||||
loginBtn.textContent = 'Entrar';
|
||||
return;
|
||||
}
|
||||
|
||||
currentPassword = password;
|
||||
loginView.style.display = 'none';
|
||||
toggleRow.classList.add('active');
|
||||
renderState(data.enabled);
|
||||
loadTokenInfo();
|
||||
} catch (err) {
|
||||
setMsg('error', '❌ Error de conexión.');
|
||||
loginBtn.disabled = false;
|
||||
loginBtn.textContent = 'Entrar';
|
||||
}
|
||||
});
|
||||
|
||||
passwordInput.addEventListener('keydown', (e) => {
|
||||
if (e.key === 'Enter') loginBtn.click();
|
||||
});
|
||||
|
||||
toggleInput.addEventListener('change', async () => {
|
||||
const desired = toggleInput.checked;
|
||||
toggleInput.disabled = true;
|
||||
setMsg('', '');
|
||||
|
||||
try {
|
||||
const res = await fetch('/admin/toggle', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ password: currentPassword, enabled: desired })
|
||||
});
|
||||
const data = await res.json();
|
||||
|
||||
if (!res.ok || !data.success) {
|
||||
setMsg('error', '❌ ' + (data.error || 'No se pudo actualizar.'));
|
||||
toggleInput.checked = !desired; // revertir visualmente
|
||||
} else {
|
||||
renderState(data.enabled);
|
||||
setMsg('success', '✅ Estado actualizado.');
|
||||
}
|
||||
} catch (err) {
|
||||
setMsg('error', '❌ Error de conexión.');
|
||||
toggleInput.checked = !desired;
|
||||
} finally {
|
||||
toggleInput.disabled = false;
|
||||
}
|
||||
});
|
||||
|
||||
// Redirige a Google para reautorizar. El servidor valida la contraseña
|
||||
// antes de generar la URL de consentimiento (ver /admin/oauth/start).
|
||||
tokenBtn.addEventListener('click', () => {
|
||||
window.location.href = '/admin/oauth/start?password=' + encodeURIComponent(currentPassword);
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 1.2 MiB |
@@ -0,0 +1,392 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="es">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
|
||||
<title>Fotos del Evento</title>
|
||||
|
||||
<!--
|
||||
============================================================
|
||||
LANDING PAGE "CERO FRICCIÓN" - SUBIDA DE FOTOS DEL EVENTO
|
||||
============================================================
|
||||
- Sin login, sin registro, sin email.
|
||||
- Un solo botón grande: "Añadir fotos".
|
||||
- Subida automática en segundo plano vía fetch() + FormData.
|
||||
- Feedback visual claro en cada estado.
|
||||
============================================================
|
||||
-->
|
||||
|
||||
<style>
|
||||
:root {
|
||||
/* Paleta tomada del escudo "War Hawks": azul marino + dorado */
|
||||
--navy: #0d1b3e;
|
||||
--navy-dark: #081127;
|
||||
--gold: #d4af5f;
|
||||
--gold-light: #e8cd8f;
|
||||
--bg: #f5f2e9;
|
||||
--text: #0d1b3e;
|
||||
--muted: #5c6172;
|
||||
--success: #1e8e3e;
|
||||
--error: #d93025;
|
||||
--radius: 18px;
|
||||
}
|
||||
|
||||
* { box-sizing: border-box; }
|
||||
|
||||
html, body {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
min-height: 100%;
|
||||
background: var(--bg);
|
||||
color: var(--text);
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
|
||||
-webkit-tap-highlight-color: transparent;
|
||||
}
|
||||
|
||||
body {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
min-height: 100vh;
|
||||
text-align: center;
|
||||
padding: 24px;
|
||||
background:
|
||||
radial-gradient(circle at 50% 0%, rgba(212, 175, 95, 0.10), transparent 55%),
|
||||
var(--bg);
|
||||
}
|
||||
|
||||
.container {
|
||||
width: 100%;
|
||||
max-width: 420px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 20px;
|
||||
}
|
||||
|
||||
.card {
|
||||
width: 100%;
|
||||
background: #fff;
|
||||
border-radius: 22px;
|
||||
padding: 36px 22px 44px;
|
||||
box-shadow: 0 10px 30px rgba(13, 27, 62, 0.15);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 22px;
|
||||
}
|
||||
|
||||
.card img.logo {
|
||||
width: 260px;
|
||||
height: auto;
|
||||
margin-top: -4px;
|
||||
}
|
||||
|
||||
p.subtitle {
|
||||
margin: 0;
|
||||
color: var(--muted);
|
||||
font-size: 0.95rem;
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
/* Input real de archivo, oculto. Usamos un <label> como botón visual
|
||||
para poder estilizarlo libremente en iOS/Android. */
|
||||
#fileInput {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.upload-btn {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 10px;
|
||||
width: 100%;
|
||||
padding: 20px 18px;
|
||||
background: linear-gradient(135deg, var(--gold-light) 0%, var(--gold) 100%);
|
||||
color: var(--navy-dark);
|
||||
font-size: 1.1rem;
|
||||
font-weight: 800;
|
||||
border: none;
|
||||
border-radius: var(--radius);
|
||||
cursor: pointer;
|
||||
user-select: none;
|
||||
transition: transform 0.12s ease, filter 0.2s ease, opacity 0.2s ease;
|
||||
box-shadow: 0 8px 22px rgba(212, 175, 95, 0.35);
|
||||
}
|
||||
|
||||
.upload-btn:active {
|
||||
transform: scale(0.97);
|
||||
filter: brightness(0.95);
|
||||
}
|
||||
|
||||
.upload-btn.disabled {
|
||||
opacity: 0.6;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
/* ---- Estado / feedback ---- */
|
||||
#status {
|
||||
min-height: 28px;
|
||||
font-size: 0.95rem;
|
||||
font-weight: 600;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
#status.uploading { color: var(--muted); }
|
||||
#status.success { color: var(--success); }
|
||||
#status.error { color: var(--error); }
|
||||
|
||||
.spinner {
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
border: 2.5px solid rgba(13, 27, 62, 0.15);
|
||||
border-top-color: var(--navy);
|
||||
border-radius: 50%;
|
||||
animation: spin 0.8s linear infinite;
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
to { transform: rotate(360deg); }
|
||||
}
|
||||
|
||||
/* Barra de progreso simple (número de archivos subidos / total) */
|
||||
.progress-wrap {
|
||||
width: 100%;
|
||||
height: 8px;
|
||||
background: rgba(13, 27, 62, 0.08);
|
||||
border-radius: 999px;
|
||||
overflow: hidden;
|
||||
display: none;
|
||||
}
|
||||
.progress-wrap.active { display: block; }
|
||||
|
||||
.progress-bar {
|
||||
height: 100%;
|
||||
width: 0%;
|
||||
background: linear-gradient(90deg, var(--navy), var(--gold));
|
||||
transition: width 0.25s ease;
|
||||
}
|
||||
|
||||
footer {
|
||||
margin-top: 8px;
|
||||
margin-bottom: 24px;
|
||||
font-size: 0.75rem;
|
||||
color: var(--muted);
|
||||
letter-spacing: 0.04em;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
/* Preview miniaturas opcional (pequeñas, solo confirmación visual) */
|
||||
.thumbs {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 6px;
|
||||
justify-content: center;
|
||||
max-width: 100%;
|
||||
}
|
||||
.thumbs img {
|
||||
width: 52px;
|
||||
height: 52px;
|
||||
object-fit: cover;
|
||||
border-radius: 8px;
|
||||
opacity: 0.9;
|
||||
border: 2px solid var(--gold-light);
|
||||
}
|
||||
|
||||
.disabled-msg {
|
||||
background: rgba(217, 48, 37, 0.08);
|
||||
color: var(--error);
|
||||
padding: 16px;
|
||||
border-radius: var(--radius);
|
||||
font-size: 0.95rem;
|
||||
font-weight: 600;
|
||||
line-height: 1.4;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
/* Ajustes finos para pantallas muy pequeñas (iPhone SE y similares) */
|
||||
@media (max-width: 360px) {
|
||||
.card { padding: 28px 16px 34px; border-radius: 18px; }
|
||||
.card img.logo { width: 220px; }
|
||||
}
|
||||
|
||||
/* Pantallas grandes: un poco más de aire */
|
||||
@media (min-width: 600px) {
|
||||
.container { max-width: 460px; }
|
||||
.card img.logo { width: 290px; }
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div class="container">
|
||||
<div class="card">
|
||||
<img class="logo" src="/assets/logo.png" alt="War Hawks - Reencuentro de Clase Graduada - Sube tus fotos">
|
||||
<p class="subtitle">Selecciona las fotos o videos de tu galería.<br>Se suben automáticamente, sin necesidad de registrarte.</p>
|
||||
|
||||
<!-- Label estilizado como botón, dispara el input real -->
|
||||
<label for="fileInput" id="uploadBtn" class="upload-btn">
|
||||
➕ Añadir fotos
|
||||
</label>
|
||||
<input
|
||||
type="file"
|
||||
id="fileInput"
|
||||
accept="image/*,video/*"
|
||||
multiple
|
||||
>
|
||||
|
||||
<!-- Mensaje mostrado cuando el admin desactiva las subidas -->
|
||||
<div id="disabledMsg" class="disabled-msg" style="display:none;">
|
||||
🚫 La subida de fotos está desactivada por el momento.<br>Vuelve a intentarlo más tarde.
|
||||
</div>
|
||||
|
||||
<div class="progress-wrap" id="progressWrap">
|
||||
<div class="progress-bar" id="progressBar"></div>
|
||||
</div>
|
||||
|
||||
<div id="status"></div>
|
||||
<div class="thumbs" id="thumbs"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<footer>War Hawks · Reencuentro de Clase Graduada</footer>
|
||||
|
||||
|
||||
<script>
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// CONFIGURACIÓN
|
||||
// ------------------------------------------------------------------
|
||||
// URL del backend. Si el frontend y backend se sirven desde el mismo
|
||||
// origen (ej. Express sirviendo /public), puede dejarse como ruta
|
||||
// relativa '/upload'. Si se despliega por separado, poner la URL
|
||||
// completa, ej: 'https://mi-api.onrender.com/upload'
|
||||
const UPLOAD_ENDPOINT = '/upload';
|
||||
|
||||
const fileInput = document.getElementById('fileInput');
|
||||
const uploadBtn = document.getElementById('uploadBtn');
|
||||
const statusEl = document.getElementById('status');
|
||||
const progressWrap = document.getElementById('progressWrap');
|
||||
const progressBar = document.getElementById('progressBar');
|
||||
const thumbsEl = document.getElementById('thumbs');
|
||||
const disabledMsg = document.getElementById('disabledMsg');
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Verifica al cargar si las subidas están activadas (panel /admin).
|
||||
// Si el admin las desactivó, ocultamos el botón y mostramos el aviso.
|
||||
// ------------------------------------------------------------------
|
||||
async function checkUploadStatus() {
|
||||
try {
|
||||
const res = await fetch('/status');
|
||||
const data = await res.json();
|
||||
if (!data.enabled) {
|
||||
uploadBtn.style.display = 'none';
|
||||
disabledMsg.style.display = 'block';
|
||||
}
|
||||
} catch (err) {
|
||||
// Si /status falla, dejamos el botón visible por defecto
|
||||
// para no bloquear al usuario por un error de red transitorio.
|
||||
console.warn('No se pudo verificar el estado de subidas:', err);
|
||||
}
|
||||
}
|
||||
|
||||
checkUploadStatus();
|
||||
|
||||
function setStatus(type, html) {
|
||||
statusEl.className = type;
|
||||
statusEl.innerHTML = html;
|
||||
}
|
||||
|
||||
function resetStatusAfterDelay(ms) {
|
||||
setTimeout(() => {
|
||||
setStatus('', '');
|
||||
progressWrap.classList.remove('active');
|
||||
progressBar.style.width = '0%';
|
||||
thumbsEl.innerHTML = '';
|
||||
}, ms);
|
||||
}
|
||||
|
||||
function showThumbs(files) {
|
||||
thumbsEl.innerHTML = '';
|
||||
Array.from(files).slice(0, 8).forEach((file) => {
|
||||
if (!file.type.startsWith('image/')) return;
|
||||
const img = document.createElement('img');
|
||||
img.src = URL.createObjectURL(file);
|
||||
img.onload = () => URL.revokeObjectURL(img.src);
|
||||
thumbsEl.appendChild(img);
|
||||
});
|
||||
}
|
||||
|
||||
fileInput.addEventListener('change', async () => {
|
||||
const files = fileInput.files;
|
||||
if (!files || files.length === 0) return;
|
||||
|
||||
showThumbs(files);
|
||||
uploadBtn.classList.add('disabled');
|
||||
progressWrap.classList.add('active');
|
||||
progressBar.style.width = '5%';
|
||||
setStatus('uploading', '<span class="spinner"></span> Subiendo ' + files.length + ' archivo(s)...');
|
||||
|
||||
const formData = new FormData();
|
||||
// El backend espera el campo 'photos' (array, múltiples archivos)
|
||||
Array.from(files).forEach((file) => {
|
||||
formData.append('photos', file);
|
||||
});
|
||||
|
||||
try {
|
||||
// Usamos XMLHttpRequest en vez de fetch puro para poder mostrar
|
||||
// progreso real de subida (fetch no expone upload progress nativo).
|
||||
await uploadWithProgress(formData);
|
||||
|
||||
progressBar.style.width = '100%';
|
||||
setStatus('success', '✅ ¡Fotos subidas con éxito!');
|
||||
resetStatusAfterDelay(4000);
|
||||
} catch (err) {
|
||||
console.error('Error subiendo archivos:', err);
|
||||
setStatus('error', '❌ Error al subir. Intenta de nuevo.');
|
||||
progressWrap.classList.remove('active');
|
||||
} finally {
|
||||
uploadBtn.classList.remove('disabled');
|
||||
fileInput.value = ''; // permite volver a seleccionar los mismos archivos
|
||||
}
|
||||
});
|
||||
|
||||
function uploadWithProgress(formData) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open('POST', UPLOAD_ENDPOINT, true);
|
||||
|
||||
xhr.upload.onprogress = (event) => {
|
||||
if (event.lengthComputable) {
|
||||
const pct = Math.max(10, Math.round((event.loaded / event.total) * 100));
|
||||
progressBar.style.width = pct + '%';
|
||||
}
|
||||
};
|
||||
|
||||
xhr.onload = () => {
|
||||
if (xhr.status >= 200 && xhr.status < 300) {
|
||||
resolve(xhr.responseText);
|
||||
} else if (xhr.status === 403) {
|
||||
// Las subidas fueron desactivadas por el admin justo ahora
|
||||
uploadBtn.style.display = 'none';
|
||||
disabledMsg.style.display = 'block';
|
||||
reject(new Error('Subidas desactivadas'));
|
||||
} else {
|
||||
reject(new Error('HTTP ' + xhr.status + ': ' + xhr.responseText));
|
||||
}
|
||||
};
|
||||
|
||||
xhr.onerror = () => reject(new Error('Error de red'));
|
||||
xhr.send(formData);
|
||||
});
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user