File size: 21,291 Bytes
df4a9b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
import express from 'express';
import cors from 'cors';
import fs from 'fs';
import path from 'path';
import axios from 'axios';
import archiver from 'archiver';
import PDFDocument from 'pdfkit';
import { spawn } from 'child_process';
import { fileURLToPath } from 'url';
import { initializeApp } from 'firebase/app';
import sharp from 'sharp';
import { getFirestore, collection, query, where, getDocs, orderBy } from 'firebase/firestore';

sharp.cache(false); // Disable cache to prevent memory growth during batch processing

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const app = express();
const PORT = process.env.PORT || 7860;

app.use(cors());
app.use(express.json({ limit: '50mb' }));

// Serve static files from the dist directory
app.use(express.static(path.join(__dirname, 'dist')));

// --- Firebase Config ---
const firebaseConfig = {
  apiKey: "AIzaSyAH3jvbe0_QLzIuuv5kTMK8246HNvohvfE",
  authDomain: "twistedbrody-9d163.firebaseapp.com",
  projectId: "twistedbrody-9d163",
  storageBucket: "twistedbrody-9d163.firebasestorage.app",
  messagingSenderId: "733213514129",
  appId: "1:733213514129:web:e9694684f5c3994ed06230",
  measurementId: "G-N8TQ7MY42W"
};

const firebaseApp = initializeApp(firebaseConfig);
const db = getFirestore(firebaseApp);

// --- 1. Almacén global de progreso ---
const progresoDescargas = {};

// --- Helpers ---
const downloadsDir = path.join(__dirname, 'downloads');
if (!fs.existsSync(downloadsDir)) {
  fs.mkdirSync(downloadsDir);
}

// --- Cleanup on Startup ---
function cleanDownloadsFolder() {
  console.log(`[INIT] Limpiando carpeta de descargas: ${downloadsDir}`);
  try {
    const files = fs.readdirSync(downloadsDir);
    for (const file of files) {
      if (file === '.gitkeep') continue; // Optional preservation
      fs.unlinkSync(path.join(downloadsDir, file));
    }
    if (files.length > 0) {
      console.log(`[INIT] 🧹 Se eliminaron ${files.length} archivos temporales antiguos.`);
    } else {
      console.log(`[INIT] ✨ Carpeta de descargas limpia.`);
    }
  } catch (err) {
    console.error(`[INIT] ❌ Error limpiando descargas: ${err.message}`);
  }
}
cleanDownloadsFolder();

function sanitizeFilename(name) {
  return name.replace(/[^a-z0-9]/gi, '_');
}

async function downloadImage(url) {
  try {
    const response = await axios({
      url,
      method: 'GET',
      responseType: 'arraybuffer',
      headers: {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
        'Referer': 'https://google.com'
      }
    });

    if (response.status !== 200) throw new Error(`Status ${response.status}`);
    return response.data;
  } catch (error) {
    console.error(`[DEBUG] Download failed: ${error.message} (${url})`);
    throw error;
  }
}

// --- 2. Ruta para que el Frontend escuche el progreso (SSE) ---
app.get('/api/download/progress/:jobId', (req, res) => {
  res.setHeader('Content-Type', 'text/event-stream');
  res.setHeader('Cache-Control', 'no-cache');
  res.setHeader('Connection', 'keep-alive');

  const { jobId } = req.params;

  // Enviamos un latido cada 500ms
  const interval = setInterval(() => {
    const p = progresoDescargas[jobId] || 0;
    res.write(`data: ${JSON.stringify({ progress: p })}\n\n`);
    if (p >= 100 || p === 'error') {
      clearInterval(interval);
      if (p >= 100) delete progresoDescargas[jobId]; // Limpiar memoria
    }
  }, 500);

  req.on('close', () => clearInterval(interval));
});

// --- Firebase Fetch Helper ---
async function getAllChapters(mangaId) {
  console.log(`[FIREBASE] Buscando capítulos para manga_id: ${mangaId} (Estrategia Bypass de Índice)`);

  // HACKER FIX: Consultamos solo por 'where' para evitar el error de "Index Required".
  // Ordenamos los datos nosotros mismos en la memoria RAM del servidor.
  const q = query(
    collection(db, "chapters"),
    where("manga_id", "==", mangaId)
  );

  const querySnapshot = await getDocs(q);

  if (querySnapshot.empty) {
    // Fallback: Try with 'mangaId' just in case the field name varies
    console.log("[FIREBASE] Intento secundario con campo 'mangaId'...");
    const q2 = query(collection(db, "chapters"), where("mangaId", "==", mangaId));
    const snapshot2 = await getDocs(q2);

    if (snapshot2.empty) {
      throw new Error('No se encontraron capítulos para este mangaId (ni por manga_id ni mangaId).');
    }
    return processSnapshot(snapshot2);
  }

  return processSnapshot(querySnapshot);
}

function processSnapshot(snapshot) {
  console.log(`[FIREBASE] Encontrados ${snapshot.size} capítulos. Procesando y ordenando en memoria...`);

  const chapters = snapshot.docs.map(doc => {
    const data = doc.data();
    return {
      id: doc.id,
      number: Number(data.number) || 0, // Ensure number for sorting
      original_pages: data.original_pages || [],
      pages: data.pages || []
    };
  });

  // Ordenamiento manual (Bypass del índice compuesto)
  return chapters.sort((a, b) => a.number - b.number);
}



// --- Image Proxy & Optimization ---
app.get('/api/image-proxy', async (req, res) => {
  const { url, width, height, quality, format } = req.query;

  if (!url) {
    return res.status(400).send('URL is required');
  }

  try {
    // 1. Fetch the original image
    const response = await axios({
      url: url,
      method: 'GET',
      responseType: 'arraybuffer',
      headers: {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
        'Referer': 'https://google.com'
      }
    });

    let imageBuffer = response.data;

    // 2. Set aggressive cache headers (1 day)
    res.setHeader('Cache-Control', 'public, max-age=86400, mutable');

    // 3. Resize if requested (Sharp)
    if (width || height || (format && format !== 'original')) {
      const pipeline = sharp(imageBuffer);

      if (width || height) {
        pipeline.resize(
          width ? parseInt(width) : null,
          height ? parseInt(height) : null,
          {
            fit: 'inside',
            withoutEnlargement: true
          }
        );
      }

      // Format conversion (default to webp for performance check)
      const outFormat = format || 'webp';
      const outQuality = quality ? parseInt(quality) : 85;

      if (outFormat === 'webp') {
        pipeline.webp({ quality: outQuality });
        res.setHeader('Content-Type', 'image/webp');
      } else if (outFormat === 'jpeg' || outFormat === 'jpg') {
        pipeline.jpeg({ quality: outQuality });
        res.setHeader('Content-Type', 'image/jpeg');
      } else if (outFormat === 'png') {
        pipeline.png({ quality: outQuality });
        res.setHeader('Content-Type', 'image/png');
      }

      imageBuffer = await pipeline.toBuffer();
    } else {
      // Pass-through original content type if not converting
      res.setHeader('Content-Type', response.headers['content-type']);
    }

    res.send(imageBuffer);

  } catch (error) {
    console.error(`[PROXY ERROR] ${url}: ${error.message}`);
    // Fallback: Redirect to original URL if proxy fails
    res.redirect(url);
  }
});

// --- Routes ---

app.get('/api/progress/:jobId', (req, res) => {
  const { jobId } = req.params;
  res.writeHead(200, {
    'Content-Type': 'text/event-stream',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive'
  });
  jobs[jobId] = { res, progress: 0 };
  console.log(`[SERVIDOR] Cliente conectado (Job: ${jobId})`);
  req.on('close', () => {
    if (jobs[jobId]) delete jobs[jobId];
  });
});

app.post('/api/download/zip', async (req, res) => {
  console.log("[DEBUG] Body recibido ZIP:", JSON.stringify(req.body).substring(0, 500));
  const { mangaTitle, format } = req.body; // format: 'png', 'jpg', 'webp', 'original'
  const mangaId = req.body.mangaId || req.body.manga_id;

  if (!mangaId || mangaId === 'undefined') {
    console.error("[SERVIDOR] ❌ Error: MangaId indefinido");
    return res.status(400).json({ error: "ID requerido" });
  }

  console.log(`🚀 [SERVIDOR] Iniciando Stream ZIP para: ${mangaTitle} (${mangaId}) - Formato: ${format}`);

  try {
    const chapters = await getAllChapters(mangaId);

    // Set headers for download
    res.setHeader('Content-Type', 'application/zip');
    res.setHeader('Content-Disposition', `attachment; filename="${sanitizeFilename(mangaTitle)}.zip"`);

    // OPTIMIZATION: Use level 1 (Fastest) instead of 9 (Best) to avoid CPU bottleneck
    const archive = archiver('zip', { zlib: { level: 1 } });

    archive.on('error', (err) => {
      console.error('[SERVIDOR] ❌ Error ZIP:', err);
      if (!res.headersSent) res.status(500).send({ error: err.message });
    });

    archive.pipe(res);

    // Collect all pages
    let totalPagesToDownload = 0;
    const chaptersToProcess = [];
    for (const chap of chapters) {
      let pages = [];
      if (format === 'original') {
        pages = (chap.original_pages && chap.original_pages.length > 0) ? chap.original_pages : chap.pages;
      } else {
        pages = (chap.pages && chap.pages.length > 0) ? chap.pages : chap.original_pages;
      }

      if (pages && pages.length > 0) {
        chaptersToProcess.push({ number: chap.number, pages });
        totalPagesToDownload += pages.length;
      }
    }

    // Process chapters in parallel (limit parallelism to avoid memory exploded with sharp)
    let processedTotal = 0;

    // Helper for sequential processing within chapters to manage concurrency better
    for (const chap of chaptersToProcess) {
      console.log(`[STREAM] Procesando Cap ${chap.number}`);

      // OPTIMIZATION: Increased batch size from 20 to 50 for faster downloads
      const chunkSize = 50;
      for (let i = 0; i < chap.pages.length; i += chunkSize) {
        const batch = chap.pages.slice(i, i + chunkSize);

        await Promise.all(batch.map(async (pageUrl, batchIndex) => {
          const pageIndex = i + batchIndex;
          try {
            let buffer = await downloadImage(pageUrl);

            // --- CONVERSION LOGIC ---
            let ext = 'jpg';
            let finalBuffer = buffer;

            // Determine current extension from URL roughly
            if (pageUrl.toLowerCase().includes('.png')) ext = 'png';
            else if (pageUrl.toLowerCase().includes('.webp')) ext = 'webp';

            // IF format is NOT original, we convert
            if (format && format !== 'original') {
              try {
                // Format mappings for sharp
                let sharpFormat = format;
                if (format === 'jpg') sharpFormat = 'jpeg';

                // Perform conversion
                finalBuffer = await sharp(buffer)
                  .toFormat(sharpFormat, { quality: 90 })
                  .toBuffer();

                ext = format; // Update extension to requested format
              } catch (sharpErr) {
                console.warn(`[WARN] Falló conversión a ${format}, usanda original.`, sharpErr.message);
              }
            }

            const cleanChapterNum = String(chap.number).padStart(2, '0');
            const archivePath = `Capitulo_${cleanChapterNum}/pagina_${String(pageIndex + 1).padStart(3, '0')}.${ext}`;

            archive.append(finalBuffer, { name: archivePath });

            // --- UPDATE PROGRESS ---
            processedTotal++;
            const porcentaje = Math.round((processedTotal / totalPagesToDownload) * 100);
            progresoDescargas[mangaId] = porcentaje;

          } catch (e) {
            console.error(`[!] Error Cap ${chap.number} pág ${pageIndex}: ${e.message}`);
          }
        }));
      }
    }

    // Force 100% on complete
    progresoDescargas[mangaId] = 100;
    await archive.finalize();

  } catch (error) {
    console.error(`[SERVIDOR] ❌ Error Global: ${error.message}`);
    fs.writeFileSync(path.join(__dirname, 'server_error.log'), `[${new Date().toISOString()}] Error ZIP: ${error.stack}\n`, { flag: 'a' });
    progresoDescargas[mangaId] = 'error';
    if (!res.headersSent) res.status(500).json({ error: error.message });
  }
});

// const { spawn } = require('child_process'); // Removed: using import at top

app.post('/api/download/pdf', async (req, res) => {
  console.log("[DEBUG] Body recibido PDF:", JSON.stringify(req.body).substring(0, 500));
  const { mangaTitle, format, jobId } = req.body;
  const mangaId = req.body.mangaId || req.body.manga_id;

  if (!mangaId || mangaId === 'undefined') {
    console.error("[SERVIDOR] ❌ Error: Se recibió un mangaId indefinido para PDF");
    return res.status(400).json({ error: "El ID del manga es obligatorio" });
  }

  console.log(`\n[SERVIDOR] Solicitud PDF (Python Bridge) para: ${mangaTitle} (ID: ${mangaId})`);

  // Notify client PDF is starting (optional, but good if we kept SSE)
  // sendProgress(jobId, 10, 'starting'); // We might not be able to track detailed progress easily w/o advanced python ipc

  try {
    // 1. Fetch URLs using existing helper
    const chapters = await getAllChapters(mangaId);
    let allUrls = [];

    // Sort chapters just to be safe, though getAllChapters does it
    chapters.sort((a, b) => a.number - b.number);

    for (const chap of chapters) {
      let pages = [];
      if (format === 'original') pages = (chap.original_pages && chap.original_pages.length > 0) ? chap.original_pages : chap.pages;
      else pages = (chap.pages && chap.pages.length > 0) ? chap.pages : chap.original_pages;

      if (pages && pages.length > 0) {
        allUrls = allUrls.concat(pages);
      }
    }

    if (allUrls.length === 0) {
      throw new Error("No extracted pages found to convert.");
    }

    console.log(`[PYTHON] Delegando ${allUrls.length} páginas al script converter.py...`);

    const safeTitle = sanitizeFilename(mangaTitle);
    const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
    const pdfName = `${safeTitle}_HackerMode_${timestamp}.pdf`;
    const pdfPath = path.join(downloadsDir, pdfName); // Use full path

    // 2. Call Python Script in Unbuffered mode
    // HACKER FIX: En Docker/Linux a menudo es 'python3', en Windows 'python'
    const pythonCmd = process.env.PYTHON_CMD || (process.platform === 'win32' ? 'python' : 'python3');
    console.log(`[SERVIDOR] Usando comando Python: ${pythonCmd}`);

    const scriptPath = path.join(__dirname, 'converter.py');
    const pythonProcess = spawn(pythonCmd, ['-u', scriptPath]);

    // Send data to Python via STDIN
    pythonProcess.stdin.write(JSON.stringify({
      urls: allUrls,
      output: pdfPath
    }));
    pythonProcess.stdin.end();

    // 🚩 Logic to sync progress with SSE
    let imagenesTerminadas = 0;
    const totalPaginasTarget = allUrls.length;

    pythonProcess.stdout.on('data', (data) => {
      const lineas = data.toString().split('\n');

      lineas.forEach(line => {
        const mensaje = line.trim();
        if (mensaje.includes("PROGRESS:IMAGE_DONE")) {
          imagenesTerminadas++;
          const porcentaje = Math.round((imagenesTerminadas / totalPaginasTarget) * 100);

          // Update global store for SSE
          progresoDescargas[mangaId] = porcentaje;
          process.stdout.write(`\r[PYTHON] ⚡ Descarga Paralela: ${imagenesTerminadas}/${totalPaginasTarget} (${porcentaje}%)`);

        } else if (mensaje === "STATUS:SUCCESS") {
          console.log("\n[PYTHON] ✅ PDF Creado con éxito.");
          progresoDescargas[mangaId] = 100; // Ensure 100%
        } else if (mensaje.startsWith("DEBUG:")) {
          // console.log(`\n[PYTHON] ${mensaje}`);
        }
      });
    });

    pythonProcess.stderr.on('data', (data) => {
      console.error(`[PYTHON STDERR]: ${data}`);
    });

    pythonProcess.on('close', (code) => {
      if (code === 0) {
        console.log(`[SERVIDOR] ✅ PDF creado por Python: ${pdfPath}`);
        // 3. Send file to client
        res.download(pdfPath, (err) => {
          if (err) {
            console.error("Error sending file:", err);
            if (!res.headersSent) res.status(500).send("Error downloading file");
          }
          // Cleanup temp file after sending
          if (fs.existsSync(pdfPath)) {
            // small timeout to ensure stream closed
            setTimeout(() => fs.unlinkSync(pdfPath), 1000);
          }
        });
      } else {
        console.error(`[SERVIDOR] ❌ Python exited with code ${code}`);
        if (!res.headersSent) res.status(500).send("Error en la conversión a PDF (Python Code " + code + ")");
      }
    });

  } catch (error) {
    console.error(`[SERVIDOR] ❌ Error Global PDF: ${error.message}`);
    if (!res.headersSent) res.status(500).send({ error: error.message });
  }
});

// --- Image Processing Endpoint ---
app.post('/api/process-image', async (req, res) => {
  const startTime = Date.now();
  const { name, index, total } = req.body; // Extraemos metadatos

  try {
    const { image } = req.body;
    if (!image) {
      return res.status(400).json({ error: 'Image data is required' });
    }

    // Log inicial del lote (solo si es la primera imagen y viene la info)
    if (index === 1 && total) {
      console.log(`\n[PROCESAMIENTO] � Iniciando lote: procesando ${total} imagenes`);
    }

    // Extract base64
    const base64Data = image.replace(/^data:image\/\w+;base64,/, "");
    const buffer = Buffer.from(base64Data, 'base64');

    // Procesamiento en Paralelo
    const [webpBuffer, thumbnailBuffer] = await Promise.all([
      sharp(buffer)
        .resize(1200, null, { fit: 'inside', withoutEnlargement: true })
        .webp({ quality: 90 })
        .toBuffer(),
      sharp(buffer)
        .resize(400, null, { fit: 'inside', withoutEnlargement: true })
        .jpeg({ quality: 80, leaflet: true })
        .toBuffer()
    ]);

    // Log de finalización estilo solicitado
    if (index && total) {
      console.log(`finalizado ${index} paginas`); // "finalizado 1 paginas", "finalizado 2 paginas"...
      if (index === total) {
        console.log(`\n[PROCESAMIENTO] ✅ Lote de ${total} imágenes completado.`);
      }
    } else {
      // Fallback para subidas individuales (portada, etc)
      const duration = Date.now() - startTime;
      console.log(`[PROCESAMIENTO] 🟢 Completado: ${name} (${duration}ms)`);
    }

    res.json({
      success: true,
      data: {
        webp: `data:image/webp;base64,${webpBuffer.toString('base64')}`,
        thumbnail: `data:image/jpeg;base64,${thumbnailBuffer.toString('base64')}`
      }
    });

  } catch (error) {
    console.error(`[PROCESAMIENTO] 🔴 Error con ${name}:`, error.message);
    res.status(500).json({ error: 'Failed to process image', details: error.message });
  }
});

// --- Health Check ---
app.get('/health', (req, res) => {
  res.status(200).send('OK');
});

// --- Catch-all to serve index.html for client-side routing (Express 5 fix) ---
app.get(/.*/, (req, res) => {
  if (req.path.startsWith('/api')) {
    return res.status(404).json({ error: 'Not Found' });
  }

  const indexPath = path.join(__dirname, 'dist', 'index.html');

  if (fs.existsSync(indexPath)) {
    res.sendFile(indexPath);
  } else {
    // Fallback si no existe el build del frontend
    res.status(200).send(`

      <html>

        <head><title>Server Running</title></head>

        <body style="font-family: monospace; background: #1a1a1a; color: #0f0; padding: 2rem;">

          <h1>✅ Server Status: ONLINE (Hacker Mode)</h1>

          <p>El servidor Backend está funcionando correctamente.</p>

          <hr style="border-color: #333;">

          <p>⚠️ <strong>Frontend no encontrado:</strong> No se encontró el archivo <code>dist/index.html</code>.</p>

          <p>Posibles causas:</p>

          <ul>

            <li>El proceso de build (<code>npm run build</code>) falló o no se ejecutó en el Dockerfile.</li>

            <li>La carpeta <code>dist</code> está en .dockerignore pero no se generó dentro de la imagen.</li>

          </ul>

          <p>Revisa los logs de "Build" en Hugging Face.</p>

        </body>

      </html>

    `);
  }
});

app.listen(PORT, '0.0.0.0', () => {
  console.log(`\n[SERVIDOR] 🚀 Listo en http://0.0.0.0:${PORT} - Integración Firebase HACKER MODE Activa`);
  console.log(`[INIT] ⚡ Sharp Concurrency: ${sharp.concurrency()} hilos activos para procesamiento de imágenes.`);
  console.log(`\n--- ENLACES DE ACCESO ---`);
  console.log(`👀 Visor (Solo Lectura): http://localhost:3000/`);
  console.log(`🔑 Admin (Control Total): http://localhost:3000/?admin=true`);
  console.log(`-------------------------\n`);
});