2025-03-12 12:47:51 +01:00
|
|
|
|
const express = require('express');
|
|
|
|
|
|
const session = require('express-session');
|
2025-08-11 13:43:53 +02:00
|
|
|
|
const FileStore = require('session-file-store')(session);
|
2025-03-12 12:47:51 +01:00
|
|
|
|
const path = require('path');
|
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
|
const AnsiToHtml = require('ansi-to-html');
|
|
|
|
|
|
const convert = new AnsiToHtml();
|
|
|
|
|
|
const { exec } = require('child_process');
|
|
|
|
|
|
const os = require('os');
|
|
|
|
|
|
const config = require('./config');
|
2025-06-23 14:27:18 +02:00
|
|
|
|
const db = require('./db');
|
|
|
|
|
|
|
2025-08-10 11:49:16 +02:00
|
|
|
|
|
2025-06-23 14:27:18 +02:00
|
|
|
|
db.testConnection(); // vérification au démarrage
|
2025-03-12 12:47:51 +01:00
|
|
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
|
const port = config.port;
|
|
|
|
|
|
|
|
|
|
|
|
// Middleware pour parser les formulaires POST
|
|
|
|
|
|
app.use(express.urlencoded({ extended: true }));
|
|
|
|
|
|
|
|
|
|
|
|
app.use(session({
|
2025-08-11 13:43:53 +02:00
|
|
|
|
store: new FileStore({
|
|
|
|
|
|
path: './sessions', // dossier où stocker les fichiers
|
|
|
|
|
|
ttl: 24 * 60 * 60, // durée de vie en secondes (ici 1 jour)
|
|
|
|
|
|
retries: 0
|
|
|
|
|
|
}),
|
2025-06-23 14:27:18 +02:00
|
|
|
|
secret: config.sessionSecret,
|
|
|
|
|
|
resave: false,
|
2025-08-11 13:43:53 +02:00
|
|
|
|
saveUninitialized: false,
|
|
|
|
|
|
cookie: {
|
|
|
|
|
|
maxAge: 24 * 60 * 60 * 1000 // 1 jour en ms
|
|
|
|
|
|
}
|
2025-03-12 12:47:51 +01:00
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
app.use(express.static('public'));
|
|
|
|
|
|
|
|
|
|
|
|
const autopostRouter = express.Router();
|
|
|
|
|
|
|
2025-08-10 11:49:16 +02:00
|
|
|
|
app.use('/js', express.static(
|
|
|
|
|
|
path.join(__dirname, '../node_modules/@tailwindcss/browser/dist'),
|
|
|
|
|
|
{ setHeaders: (res) => res.type('application/javascript') } // optionnel
|
|
|
|
|
|
));
|
|
|
|
|
|
app.use('/jquery', express.static(
|
|
|
|
|
|
path.join(__dirname, '../node_modules/jquery/dist'),
|
|
|
|
|
|
{ setHeaders: (res) => res.type('application/javascript') } // optionnel
|
|
|
|
|
|
));
|
2025-03-15 15:26:12 +01:00
|
|
|
|
|
2025-06-23 14:27:18 +02:00
|
|
|
|
// --------------------------- Auth non protégée -----------------------------
|
2025-03-12 12:47:51 +01:00
|
|
|
|
autopostRouter.get('/login', (req, res) => {
|
|
|
|
|
|
res.send(`
|
|
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
|
<html lang="fr">
|
|
|
|
|
|
<head>
|
|
|
|
|
|
<meta charset="UTF-8">
|
2025-08-11 21:29:42 +02:00
|
|
|
|
<title>Login ${config.name}</title>
|
2025-03-12 12:47:51 +01:00
|
|
|
|
<!-- Inclusion de Tailwind CSS via le CDN -->
|
2025-08-10 11:49:16 +02:00
|
|
|
|
<script src="/js/index.global.js"></script>
|
2025-03-12 12:47:51 +01:00
|
|
|
|
</head>
|
|
|
|
|
|
<body class="bg-slate-900 flex items-center justify-center min-h-screen">
|
|
|
|
|
|
<div class="bg-slate-700 p-8 rounded-lg shadow-md w-80">
|
|
|
|
|
|
<h2 class="text-center text-2xl font-bold text-white mb-4">Authentification</h2>
|
|
|
|
|
|
<form method="POST" action="/autopost/login">
|
|
|
|
|
|
<input type="text" name="username" placeholder="Identifiant" required
|
|
|
|
|
|
class="w-full p-2 mb-4 rounded border border-gray-300"/>
|
|
|
|
|
|
<input type="password" name="password" placeholder="Mot de passe" required
|
|
|
|
|
|
class="w-full p-2 mb-4 rounded border border-gray-300"/>
|
|
|
|
|
|
<button type="submit"
|
|
|
|
|
|
class="w-full p-2 bg-blue-600 text-white rounded hover:bg-blue-700">
|
|
|
|
|
|
Se connecter
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</form>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</body>
|
|
|
|
|
|
</html>
|
|
|
|
|
|
`);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
autopostRouter.post('/login', (req, res) => {
|
2025-06-23 14:27:18 +02:00
|
|
|
|
const { username, password } = req.body;
|
|
|
|
|
|
if (username === config.auth.username && password === config.auth.password) {
|
|
|
|
|
|
req.session.authenticated = true;
|
|
|
|
|
|
res.redirect('/autopost');
|
|
|
|
|
|
} else {
|
|
|
|
|
|
res.send('Identifiants invalides. <a href="/autopost/login">Réessayer</a>');
|
|
|
|
|
|
}
|
2025-03-12 12:47:51 +01:00
|
|
|
|
});
|
2025-08-10 11:49:16 +02:00
|
|
|
|
|
2025-03-12 12:47:51 +01:00
|
|
|
|
autopostRouter.get('/logout', (req, res) => {
|
2025-06-23 14:27:18 +02:00
|
|
|
|
req.session.destroy();
|
|
|
|
|
|
res.redirect('/autopost/login');
|
2025-03-12 12:47:51 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
function checkAuth(req, res, next) {
|
2025-06-23 14:27:18 +02:00
|
|
|
|
if (req.session && req.session.authenticated) {
|
|
|
|
|
|
next();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
res.redirect(req.baseUrl + '/login');
|
|
|
|
|
|
}
|
2025-03-12 12:47:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
autopostRouter.use(checkAuth);
|
|
|
|
|
|
|
2025-06-23 14:27:18 +02:00
|
|
|
|
// --------------------------- PAGE PRINCIPALE -----------------------------
|
|
|
|
|
|
autopostRouter.get('/', async (req, res) => {
|
2025-03-12 12:47:51 +01:00
|
|
|
|
const limit = 100; // enregistrements par page
|
|
|
|
|
|
const page = parseInt(req.query.page) || 1;
|
|
|
|
|
|
const offset = (page - 1) * limit;
|
|
|
|
|
|
|
2025-06-23 14:27:18 +02:00
|
|
|
|
try {
|
2025-08-11 13:43:53 +02:00
|
|
|
|
const [stats] = await db.query(`
|
|
|
|
|
|
SELECT
|
|
|
|
|
|
COUNT(*) AS total,
|
|
|
|
|
|
SUM(status = 0) AS attente,
|
|
|
|
|
|
SUM(status = 1) AS termine,
|
|
|
|
|
|
SUM(status = 2) AS erreur,
|
|
|
|
|
|
SUM(status = 3) AS deja,
|
|
|
|
|
|
SUM(status = 4) AS encours
|
|
|
|
|
|
FROM \`${config.DB_TABLE}\`
|
|
|
|
|
|
`);
|
2025-06-23 14:27:18 +02:00
|
|
|
|
// Récupérer le nombre total d'enregistrements
|
2025-08-08 23:07:13 +02:00
|
|
|
|
const [countResult] = await db.query(`SELECT COUNT(*) as total FROM \`${config.DB_TABLE}\``);
|
2025-06-23 14:27:18 +02:00
|
|
|
|
const totalRecords = countResult[0].total;
|
2025-03-12 12:47:51 +01:00
|
|
|
|
const totalPages = Math.ceil(totalRecords / limit);
|
|
|
|
|
|
|
2025-06-23 14:27:18 +02:00
|
|
|
|
const [rows] = await db.query(
|
2025-08-08 23:07:13 +02:00
|
|
|
|
`SELECT nom, status, id FROM \`${config.DB_TABLE}\` ORDER BY (status = 2) DESC, id DESC LIMIT ? OFFSET ?`,
|
2025-06-23 14:27:18 +02:00
|
|
|
|
[limit, offset]
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
let html = `
|
2025-08-12 10:28:54 +02:00
|
|
|
|
<!DOCTYPE html>
|
2025-06-23 14:27:18 +02:00
|
|
|
|
<html lang="fr">
|
|
|
|
|
|
<head>
|
2025-08-12 10:28:54 +02:00
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
|
|
<title>Suivi Autopost ${config.name}</title>
|
|
|
|
|
|
<script src="js/index.global.js"></script>
|
|
|
|
|
|
<script src="jquery/jquery.min.js"></script>
|
|
|
|
|
|
<link href="https://fonts.googleapis.com/css?family=Roboto:400,700&display=swap" rel="stylesheet">
|
2025-06-23 14:27:18 +02:00
|
|
|
|
</head>
|
2025-08-12 10:28:54 +02:00
|
|
|
|
<body class="bg-sky-950 text-white font-sans p-4">
|
|
|
|
|
|
<div class="w-full px-4">
|
|
|
|
|
|
<!-- Header -->
|
|
|
|
|
|
<div class="mb-6">
|
|
|
|
|
|
<div class="flex items-center justify-between">
|
|
|
|
|
|
<h1 class="text-2xl md:text-3xl font-extrabold tracking-tight bg-clip-text text-transparent bg-gradient-to-r from-blue-400 to-cyan-300">
|
|
|
|
|
|
Suivi Autopost ${config.name}
|
|
|
|
|
|
</h1>
|
|
|
|
|
|
<a href="/autopost/logout"
|
|
|
|
|
|
class="inline-flex items-center gap-2 px-3 py-2 rounded-xl bg-red-600/90 hover:bg-red-600 text-white shadow-lg shadow-red-900/20 transition">
|
|
|
|
|
|
<svg class="w-4 h-4" viewBox="0 0 24 24" fill="none" stroke="currentColor">
|
|
|
|
|
|
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
|
|
|
|
|
d="M17 16l4-4m0 0l-4-4m4 4H7m6 4v1a2 2 0 01-2 2H6a2 2 0 01-2-2V7a2 2 0 012-2h5a2 2 0 012 2v1"/>
|
|
|
|
|
|
</svg>
|
|
|
|
|
|
Déconnexion
|
|
|
|
|
|
</a>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<p class="mt-1 text-sm text-gray-400">Vue d’ensemble des traitements en cours et de l’état des envois.</p>
|
2025-08-11 13:43:53 +02:00
|
|
|
|
</div>
|
2025-08-12 10:28:54 +02:00
|
|
|
|
|
|
|
|
|
|
<!-- Stat cards -->
|
|
|
|
|
|
<div class="grid grid-cols-2 md:grid-cols-4 gap-4 mb-6">
|
|
|
|
|
|
<!-- En attente -->
|
|
|
|
|
|
<div class="group rounded-2xl p-3 md:p-5 bg-gradient-to-br from-cyan-500 to-cyan-400 text-black shadow-xl ring-1 ring-white/10 transition transform hover:-translate-y-0.5 hover:shadow-2xl cursor-pointer filter-card" data-status="0">
|
|
|
|
|
|
<div class="flex items-center gap-3">
|
|
|
|
|
|
<div class="rounded-xl bg-black/10 p-2">
|
|
|
|
|
|
<svg class="w-5 h-5" viewBox="0 0 24 24" fill="none" stroke="currentColor">
|
|
|
|
|
|
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
|
|
|
|
|
d="M12 6v6l4 2"/>
|
|
|
|
|
|
</svg>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="text-sm/5 font-medium">En attente</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="mt-3 text-3xl font-extrabold tabular-nums">${stats[0].attente}</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<!-- Terminé -->
|
|
|
|
|
|
<div class="group rounded-2xl p-4 md:p-5 bg-gradient-to-br from-green-300 to-emerald-300 text-black shadow-xl ring-1 ring-white/10 transition transform hover:-translate-y-0.5 hover:shadow-2xl cursor-pointer filter-card" data-status="1">
|
|
|
|
|
|
<div class="flex items-center gap-3">
|
|
|
|
|
|
<div class="rounded-xl bg-black/10 p-2">
|
|
|
|
|
|
<svg class="w-5 h-5" viewBox="0 0 24 24" fill="none" stroke="currentColor">
|
|
|
|
|
|
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
|
|
|
|
|
d="M5 13l4 4L19 7"/>
|
|
|
|
|
|
</svg>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="text-sm/5 font-medium">Terminé</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="mt-3 text-3xl font-extrabold tabular-nums">${stats[0].termine}</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<!-- Erreur -->
|
|
|
|
|
|
<div class="group rounded-2xl p-4 md:p-5 bg-gradient-to-br from-rose-300 to-red-300 text-black shadow-xl ring-1 ring-white/10 transition transform hover:-translate-y-0.5 hover:shadow-2xl cursor-pointer filter-card" data-status="2">
|
|
|
|
|
|
<div class="flex items-center gap-3">
|
|
|
|
|
|
<div class="rounded-xl bg-black/10 p-2">
|
|
|
|
|
|
<svg class="w-5 h-5" viewBox="0 0 24 24" fill="none" stroke="currentColor">
|
|
|
|
|
|
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
|
|
|
|
|
d="M12 9v3m0 4h.01M10.29 3.86L1.82 18a2 2 0 001.71 3h16.94a2 2 0 001.71-3L13.71 3.86a2 2 0 00-3.42 0z"/>
|
|
|
|
|
|
</svg>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="text-sm/5 font-medium">Erreur</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="mt-3 text-3xl font-extrabold tabular-nums">${stats[0].erreur}</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<!-- Déjà dispo -->
|
|
|
|
|
|
<div class="group rounded-2xl p-4 md:p-5 bg-gradient-to-br from-pink-300 to-fuchsia-300 text-black shadow-xl ring-1 ring-white/10 transition transform hover:-translate-y-0.5 hover:shadow-2xl cursor-pointer filter-card" data-status="3">
|
|
|
|
|
|
<div class="flex items-center gap-3">
|
|
|
|
|
|
<div class="rounded-xl bg-black/10 p-2">
|
|
|
|
|
|
<svg class="w-5 h-5" viewBox="0 0 24 24" fill="none" stroke="currentColor">
|
|
|
|
|
|
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
|
|
|
|
|
d="M8 7h8M8 11h8M8 15h6"/>
|
|
|
|
|
|
</svg>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="text-sm/5 font-medium">Déjà dispo</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div class="mt-3 text-3xl font-extrabold tabular-nums">${stats[0].deja}</div>
|
|
|
|
|
|
</div>
|
2025-08-11 13:43:53 +02:00
|
|
|
|
</div>
|
2025-08-12 10:28:54 +02:00
|
|
|
|
|
|
|
|
|
|
<!-- Bouton réinitialiser -->
|
|
|
|
|
|
<div class="mb-4 text-center">
|
|
|
|
|
|
<button id="showAll"
|
|
|
|
|
|
class="hidden px-5 py-2 rounded-xl bg-gradient-to-br from-gray-600 to-gray-800 text-white font-semibold shadow-lg hover:from-gray-500 hover:to-gray-700 transition transform hover:-translate-y-0.5">
|
|
|
|
|
|
Tout afficher
|
|
|
|
|
|
</button>
|
2025-08-11 13:43:53 +02:00
|
|
|
|
</div>
|
2025-08-12 10:28:54 +02:00
|
|
|
|
|
|
|
|
|
|
<!-- Search -->
|
|
|
|
|
|
<div class="mb-4">
|
|
|
|
|
|
<label for="searchInput" class="sr-only">Rechercher</label>
|
|
|
|
|
|
<div class="relative">
|
|
|
|
|
|
<span class="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3">
|
|
|
|
|
|
<svg class="w-5 h-5 text-gray-400" viewBox="0 0 24 24" fill="none" stroke="currentColor">
|
|
|
|
|
|
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
|
|
|
|
|
d="M21 21l-4.35-4.35M10 18a8 8 0 1 1 0-16 8 8 0 0 1 0 16z"/>
|
|
|
|
|
|
</svg>
|
|
|
|
|
|
</span>
|
|
|
|
|
|
<input id="searchInput" type="text" placeholder="Rechercher…"
|
|
|
|
|
|
class="w-full pl-10 pr-3 py-2 rounded-xl bg-gray-800/80 border border-gray-700 focus:border-blue-500 focus:ring-4 focus:ring-blue-500/20 outline-none transition placeholder:text-gray-400" />
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<nav id="pagination" aria-label="Page navigation" class="mb-4">
|
|
|
|
|
|
<ul class="inline-flex items-center -space-x-px">
|
|
|
|
|
|
<li>
|
|
|
|
|
|
${
|
|
|
|
|
|
page > 1
|
|
|
|
|
|
? `<a href="/autopost/?page=${page - 1}" data-page="${page - 1}" class="px-3 py-2 ml-0 leading-tight text-gray-500 bg-white border border-gray-300 rounded-l-lg hover:bg-gray-100 hover:text-gray-700">Précédent</a>`
|
|
|
|
|
|
: `<span class="px-3 py-2 ml-0 leading-tight text-gray-500 bg-gray-200 border border-gray-300 rounded-l-lg">Précédent</span>`
|
|
|
|
|
|
}
|
|
|
|
|
|
</li>
|
|
|
|
|
|
<li>
|
|
|
|
|
|
<span class="px-4 py-2 leading-tight text-gray-700 bg-white border border-gray-300">Page ${page} sur ${totalPages}</span>
|
|
|
|
|
|
</li>
|
|
|
|
|
|
<li>
|
|
|
|
|
|
${
|
|
|
|
|
|
page < totalPages
|
|
|
|
|
|
? `<a href="/autopost/?page=${page + 1}" data-page="${page + 1}" class="px-3 py-2 leading-tight text-gray-500 bg-white border border-gray-300 rounded-r-lg hover:bg-gray-100 hover:text-gray-700">Suivant</a>`
|
|
|
|
|
|
: `<span class="px-3 py-2 leading-tight text-gray-500 bg-gray-200 border border-gray-300 rounded-r-lg">Suivant</span>`
|
|
|
|
|
|
}
|
|
|
|
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
|
|
|
|
</nav>
|
|
|
|
|
|
|
|
|
|
|
|
<div class="overflow-x-auto">
|
|
|
|
|
|
<table class="min-w-full border border-gray-700 shadow-lg rounded-lg overflow-hidden">
|
|
|
|
|
|
<thead class="bg-gray-900 text-gray-300 uppercase text-sm">
|
|
|
|
|
|
<tr>
|
|
|
|
|
|
<th class="px-4 py-2">Name</th>
|
|
|
|
|
|
<th class="px-4 py-2">Status</th>
|
|
|
|
|
|
<th class="px-4 py-2">ID</th>
|
|
|
|
|
|
<th class="px-4 py-2">Actions</th>
|
|
|
|
|
|
</tr>
|
|
|
|
|
|
</thead>
|
|
|
|
|
|
<tbody class="divide-y divide-gray-700">`;
|
|
|
|
|
|
|
|
|
|
|
|
rows.forEach(row => {
|
|
|
|
|
|
let statusText = '';
|
|
|
|
|
|
let statusClass = '';
|
|
|
|
|
|
switch (parseInt(row.status)) {
|
|
|
|
|
|
case 0:
|
|
|
|
|
|
statusText = 'EN ATTENTE';
|
|
|
|
|
|
statusClass = 'bg-cyan-500 text-black font-bold';
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
statusText = 'ENVOI TERMINÉ';
|
|
|
|
|
|
statusClass = 'bg-green-300 text-black font-bold';
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 2:
|
|
|
|
|
|
statusText = 'ERREUR';
|
|
|
|
|
|
statusClass = 'bg-red-300 text-black font-bold';
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 3:
|
|
|
|
|
|
statusText = 'DEJA DISPONIBLE';
|
|
|
|
|
|
statusClass = 'bg-pink-300 text-black font-bold';
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 4:
|
|
|
|
|
|
statusText = 'EN COURS';
|
|
|
|
|
|
statusClass = 'bg-yellow-300 text-black font-bold';
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
statusText = 'INCONNU';
|
|
|
|
|
|
}
|
|
|
|
|
|
let logLink = (parseInt(row.status) === 1 || parseInt(row.status) === 2)
|
|
|
|
|
|
? ' | <a href="#" class="log-link text-blue-400 hover:underline" data-filename="' + row.nom + '">Log</a>'
|
|
|
|
|
|
: '';
|
|
|
|
|
|
let mediainfoLink = (parseInt(row.status) === 0 || parseInt(row.status) === 1 || parseInt(row.status) === 2)
|
|
|
|
|
|
? ' | <a href="#" class="mediainfo-link text-blue-400 hover:underline" data-filename="' + row.nom + '">MediaInfo/BDInfo</a>'
|
|
|
|
|
|
: '';
|
|
|
|
|
|
let dlLink = row.status === 1
|
|
|
|
|
|
? ' | <a href="/autopost/dl?name=' + encodeURIComponent(row.nom) + '" class="dl-link text-blue-400 hover:underline">DL</a>'
|
|
|
|
|
|
: '';
|
|
|
|
|
|
|
|
|
|
|
|
html += `
|
|
|
|
|
|
<tr id="row-${row.id}" data-status="${row.status}" class="odd:bg-gray-800 even:bg-gray-700">
|
|
|
|
|
|
<td class="px-4 py-2 border border-gray-700">${row.nom}</td>
|
|
|
|
|
|
<td class="px-4 py-2 border border-gray-700 status-text whitespace-nowrap ${statusClass}">${statusText}</td>
|
|
|
|
|
|
<td class="px-4 py-2 border border-gray-700">${row.id}</td>
|
|
|
|
|
|
<td class="px-4 py-2 border border-gray-700 whitespace-nowrap">
|
|
|
|
|
|
<a href="#" class="edit-link text-blue-400 hover:underline" data-id="${row.id}" data-status="${row.status}">Editer</a> |
|
|
|
|
|
|
<a href="#" class="delete-link text-blue-400 hover:underline" data-id="${row.id}">Supprimer</a>
|
|
|
|
|
|
${logLink}
|
|
|
|
|
|
${mediainfoLink}
|
|
|
|
|
|
${dlLink}
|
|
|
|
|
|
</td>
|
|
|
|
|
|
</tr>
|
|
|
|
|
|
`;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
html += `
|
|
|
|
|
|
</tbody>
|
|
|
|
|
|
</table>
|
2025-08-11 13:43:53 +02:00
|
|
|
|
</div>
|
2025-06-23 14:27:18 +02:00
|
|
|
|
</div>
|
2025-08-12 10:28:54 +02:00
|
|
|
|
|
|
|
|
|
|
<!-- Modale d'édition -->
|
|
|
|
|
|
<div id="editModal" class="hidden fixed inset-0 bg-black bg-opacity-60 backdrop-blur-sm flex items-center justify-center z-50">
|
|
|
|
|
|
<div class="bg-gray-900 p-6 rounded-2xl shadow-2xl w-full max-w-md transform transition-all scale-95">
|
|
|
|
|
|
<!-- Bouton fermer -->
|
|
|
|
|
|
<button type="button" class="absolute top-4 right-4 text-gray-400 hover:text-white text-2xl font-bold close">
|
|
|
|
|
|
×
|
|
|
|
|
|
</button>
|
|
|
|
|
|
|
|
|
|
|
|
<!-- Titre -->
|
|
|
|
|
|
<h2 class="text-2xl font-bold mb-6 text-center text-white">
|
|
|
|
|
|
✏️ Éditer Release <span id="modalReleaseId" class="text-blue-400"></span>
|
|
|
|
|
|
</h2>
|
|
|
|
|
|
|
|
|
|
|
|
<!-- Formulaire -->
|
|
|
|
|
|
<form id="editForm">
|
|
|
|
|
|
<!-- Label + Select -->
|
|
|
|
|
|
<label for="statusSelect" class="block mb-2 text-gray-300 font-medium">Status :</label>
|
|
|
|
|
|
<select id="statusSelect" name="status"
|
|
|
|
|
|
class="w-full p-3 mb-6 rounded-lg border border-gray-700 bg-gray-800 text-white focus:ring-2 focus:ring-blue-500 focus:outline-none">
|
|
|
|
|
|
<option value="0" class="text-cyan-400">EN ATTENTE</option>
|
|
|
|
|
|
<option value="1" class="text-green-400">ENVOI TERMINÉ</option>
|
|
|
|
|
|
<option value="2" class="text-red-400">ERREUR</option>
|
|
|
|
|
|
<option value="3" class="text-pink-400">DEJA DISPONIBLE</option>
|
|
|
|
|
|
</select>
|
|
|
|
|
|
|
|
|
|
|
|
<!-- Hidden ID -->
|
|
|
|
|
|
<input type="hidden" id="releaseId" name="id" value=""/>
|
|
|
|
|
|
|
|
|
|
|
|
<!-- Bouton -->
|
|
|
|
|
|
<button type="submit"
|
|
|
|
|
|
class="w-full p-3 bg-gradient-to-r from-blue-600 to-blue-500 text-white font-semibold rounded-lg shadow hover:from-blue-500 hover:to-blue-400 transform hover:-translate-y-0.5 transition">
|
|
|
|
|
|
💾 Mettre à jour
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</form>
|
|
|
|
|
|
</div>
|
2025-06-23 14:27:18 +02:00
|
|
|
|
</div>
|
2025-08-12 10:28:54 +02:00
|
|
|
|
|
|
|
|
|
|
<!-- Modale pour afficher le log -->
|
2025-08-11 15:22:45 +02:00
|
|
|
|
<div id="logModal" class="hidden fixed inset-0 bg-black bg-opacity-60 backdrop-blur-sm flex items-center justify-center z-50">
|
2025-08-12 10:28:54 +02:00
|
|
|
|
<div class="bg-gray-900 p-6 rounded-2xl shadow-2xl w-full max-w-7xl relative"> <!-- élargi max-w -->
|
|
|
|
|
|
<!-- Bouton fermer -->
|
|
|
|
|
|
<button type="button" class="absolute top-4 right-4 text-gray-400 hover:text-white text-2xl font-bold close log-close">
|
|
|
|
|
|
×
|
|
|
|
|
|
</button>
|
|
|
|
|
|
|
|
|
|
|
|
<!-- Titre -->
|
|
|
|
|
|
<h2 class="text-2xl font-bold mb-6 text-center text-white">
|
|
|
|
|
|
📄 Contenu du log
|
|
|
|
|
|
</h2>
|
|
|
|
|
|
|
|
|
|
|
|
<!-- Contenu du log -->
|
|
|
|
|
|
<pre id="logContent"
|
|
|
|
|
|
class="max-h-[90vh] overflow-y-auto p-6 rounded-lg bg-gray-800 text-green-400 font-mono text-sm leading-relaxed border border-gray-700 shadow-inner whitespace-pre-wrap">
|
|
|
|
|
|
</pre>
|
|
|
|
|
|
</div>
|
2025-06-23 14:27:18 +02:00
|
|
|
|
</div>
|
2025-08-12 10:28:54 +02:00
|
|
|
|
|
|
|
|
|
|
<!-- Modale pour afficher le mediainfo -->
|
2025-08-11 15:42:08 +02:00
|
|
|
|
<div id="mediainfoModal" class="hidden fixed inset-0 bg-black bg-opacity-60 backdrop-blur-sm flex items-center justify-center z-50">
|
2025-08-12 10:28:54 +02:00
|
|
|
|
<div class="bg-gray-900 p-6 rounded-2xl shadow-2xl w-full max-w-6xl relative">
|
|
|
|
|
|
<!-- Bouton fermer -->
|
|
|
|
|
|
<button type="button"
|
|
|
|
|
|
class="absolute top-4 right-4 text-gray-400 hover:text-white text-2xl font-bold close mediainfo-close"
|
|
|
|
|
|
title="Fermer">
|
|
|
|
|
|
×
|
|
|
|
|
|
</button>
|
|
|
|
|
|
|
|
|
|
|
|
<!-- Titre -->
|
|
|
|
|
|
<h2 class="text-2xl font-bold text-white mb-4 pr-16">📄 Contenu du mediainfo</h2>
|
|
|
|
|
|
|
|
|
|
|
|
<!-- Bouton copier -->
|
|
|
|
|
|
<div class="mb-6">
|
|
|
|
|
|
<button id="copyMediainfoBtn"
|
|
|
|
|
|
class="bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-lg text-sm font-semibold shadow-md transition">
|
|
|
|
|
|
📋 Copier JSON
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<!-- Contenu mediainfo -->
|
|
|
|
|
|
<pre id="mediainfoContent"
|
|
|
|
|
|
class="max-h-[80vh] overflow-y-auto p-4 rounded-lg bg-gray-800 text-yellow-300 font-mono text-sm leading-relaxed border border-gray-700 shadow-inner whitespace-pre-wrap">
|
|
|
|
|
|
</pre>
|
2025-08-11 15:42:08 +02:00
|
|
|
|
</div>
|
2025-06-23 14:27:18 +02:00
|
|
|
|
</div>
|
2025-08-12 10:28:54 +02:00
|
|
|
|
|
2025-08-11 15:42:08 +02:00
|
|
|
|
<!-- Modale de confirmation de suppression -->
|
2025-08-12 10:28:54 +02:00
|
|
|
|
<div id="confirmDeleteModal" class="hidden fixed inset-0 bg-black bg-opacity-60 backdrop-blur-sm flex items-center justify-center z-50">
|
2025-08-11 15:53:25 +02:00
|
|
|
|
<div class="bg-gray-900 p-6 rounded-2xl shadow-2xl w-full max-w-3xl md:max-w-4xl relative">
|
2025-08-12 10:28:54 +02:00
|
|
|
|
<button type="button" class="absolute top-3 right-3 text-gray-400 hover:text-white text-xl font-bold" id="confirmDeleteClose" aria-label="Fermer">
|
|
|
|
|
|
×
|
2025-08-11 15:42:08 +02:00
|
|
|
|
</button>
|
2025-08-12 10:28:54 +02:00
|
|
|
|
|
|
|
|
|
|
<h3 class="text-xl font-bold text-white mb-2">Supprimer cet enregistrement ?</h3>
|
|
|
|
|
|
<p class="text-gray-300 text-sm mb-6">
|
|
|
|
|
|
Vous êtes sur le point de supprimer <span id="confirmItemName" class="text-white font-semibold"></span>. Cette action est irréversible.
|
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
|
|
<div class="flex justify-end gap-3">
|
|
|
|
|
|
<button type="button" id="cancelDeleteBtn" class="px-4 py-2 rounded-lg bg-gray-700 hover:bg-gray-600 text-white">
|
|
|
|
|
|
Annuler
|
|
|
|
|
|
</button>
|
|
|
|
|
|
<button type="button" id="confirmDeleteBtn" class="px-4 py-2 rounded-lg bg-red-600 hover:bg-red-700 text-white font-semibold">
|
|
|
|
|
|
Supprimer
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
2025-08-11 15:42:08 +02:00
|
|
|
|
</div>
|
2025-08-12 10:28:54 +02:00
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<!-- Toast -->
|
|
|
|
|
|
<div id="toast" class="hidden fixed bottom-6 left-1/2 -translate-x-1/2 px-4 py-2 rounded-lg bg-gray-900/95 text-white shadow-lg z-[60]">
|
2025-08-11 15:42:08 +02:00
|
|
|
|
<span id="toastMsg">Supprimé.</span>
|
2025-08-12 10:28:54 +02:00
|
|
|
|
</div>
|
|
|
|
|
|
<script>
|
|
|
|
|
|
// --- State ---
|
|
|
|
|
|
const INITIAL_PAGE = ${page};
|
|
|
|
|
|
const INITIAL_TOTAL_PAGES = ${totalPages};
|
|
|
|
|
|
const PAGE_LIMIT = ${limit};
|
|
|
|
|
|
|
|
|
|
|
|
let currentPage = INITIAL_PAGE;
|
|
|
|
|
|
let currentTotalPages = INITIAL_TOTAL_PAGES;
|
|
|
|
|
|
let activeFilter = null; // number | null
|
|
|
|
|
|
let activeQuery = ''; // string
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// --- Rendering: table ---
|
|
|
|
|
|
function updateTable(rows) {
|
|
|
|
|
|
var tbody = $('table tbody');
|
|
|
|
|
|
tbody.empty();
|
|
|
|
|
|
|
|
|
|
|
|
rows.forEach(function(row) {
|
|
|
|
|
|
var statusText = '';
|
|
|
|
|
|
var statusClass = '';
|
|
|
|
|
|
|
|
|
|
|
|
switch (parseInt(row.status)) {
|
|
|
|
|
|
case 0:
|
|
|
|
|
|
statusText = 'EN ATTENTE';
|
|
|
|
|
|
statusClass = 'bg-cyan-500 text-black font-bold';
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
statusText = 'ENVOI TERMINÉ';
|
|
|
|
|
|
statusClass = 'bg-green-300 text-black font-bold';
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 2:
|
|
|
|
|
|
statusText = 'ERREUR';
|
|
|
|
|
|
statusClass = 'bg-red-300 text-black font-bold';
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 3:
|
|
|
|
|
|
statusText = 'DEJA DISPONIBLE';
|
|
|
|
|
|
statusClass = 'bg-pink-300 text-black font-bold';
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 4:
|
|
|
|
|
|
statusText = 'EN COURS';
|
|
|
|
|
|
statusClass = 'bg-yellow-300 text-black font-bold';
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
statusText = 'INCONNU';
|
2025-08-11 14:03:41 +02:00
|
|
|
|
}
|
2025-08-12 10:28:54 +02:00
|
|
|
|
|
|
|
|
|
|
var logLink = (parseInt(row.status) === 1 || parseInt(row.status) === 2)
|
|
|
|
|
|
? ' | <a href="#" class="log-link text-blue-400 hover:underline" data-filename="' + row.nom + '">Log</a>'
|
|
|
|
|
|
: '';
|
|
|
|
|
|
|
|
|
|
|
|
var mediainfoLink = (parseInt(row.status) === 0 || parseInt(row.status) === 1 || parseInt(row.status) === 2)
|
|
|
|
|
|
? ' | <a href="#" class="mediainfo-link text-blue-400 hover:underline" data-filename="' + row.nom + '">Mediainfo</a>'
|
|
|
|
|
|
: '';
|
|
|
|
|
|
|
|
|
|
|
|
var dlLink = (parseInt(row.status) === 1)
|
|
|
|
|
|
? ' | <a href="/autopost/dl?name=' + encodeURIComponent(row.nom) + '" class="dl-link text-blue-400 hover:underline">DL</a>'
|
|
|
|
|
|
: '';
|
|
|
|
|
|
|
|
|
|
|
|
var tr =
|
|
|
|
|
|
'<tr id="row-' + row.id + '" data-status="' + row.status + '" class="odd:bg-gray-800 even:bg-gray-700">' +
|
|
|
|
|
|
'<td class="px-4 py-2 border border-gray-700 whitespace-nowrap">' + row.nom + '</td>' +
|
|
|
|
|
|
'<td class="px-4 py-2 border border-gray-700 status-text whitespace-nowrap ' + statusClass + '">' + statusText + '</td>' +
|
|
|
|
|
|
'<td class="px-4 py-2 border border-gray-700">' + row.id + '</td>' +
|
|
|
|
|
|
'<td class="px-4 py-2 border border-gray-700">' +
|
|
|
|
|
|
'<a href="#" class="edit-link text-blue-400 hover:underline" data-id="' + row.id + '" data-status="' + row.status + '">Editer</a> | ' +
|
|
|
|
|
|
'<a href="#" class="delete-link text-blue-400 hover:underline" data-id="' + row.id + '">Supprimer</a>' +
|
|
|
|
|
|
logLink + mediainfoLink + dlLink +
|
|
|
|
|
|
'</td>' +
|
|
|
|
|
|
'</tr>';
|
|
|
|
|
|
|
|
|
|
|
|
tbody.append(tr);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// --- Rendering: pagination ---
|
|
|
|
|
|
function renderPaginationHTML(page, totalPages) {
|
|
|
|
|
|
var prevDisabled = page <= 1;
|
|
|
|
|
|
var nextDisabled = page >= totalPages;
|
|
|
|
|
|
|
|
|
|
|
|
var h = '';
|
|
|
|
|
|
h += '<ul class="inline-flex items-center -space-x-px">';
|
|
|
|
|
|
h += '<li>';
|
|
|
|
|
|
if (prevDisabled) {
|
|
|
|
|
|
h += '<span class="px-3 py-2 ml-0 leading-tight text-gray-500 bg-gray-200 border border-gray-300 rounded-l-lg">Précédent</span>';
|
|
|
|
|
|
} else {
|
|
|
|
|
|
h += '<a href="/autopost/?page=' + (page - 1) + '" data-page="' + (page - 1) + '" class="px-3 py-2 ml-0 leading-tight text-gray-500 bg-white border border-gray-300 rounded-l-lg hover:bg-gray-100 hover:text-gray-700">Précédent</a>';
|
2025-08-12 10:15:05 +02:00
|
|
|
|
}
|
2025-08-12 10:28:54 +02:00
|
|
|
|
h += '</li>';
|
|
|
|
|
|
h += '<li><span class="px-4 py-2 leading-tight text-gray-700 bg-white border border-gray-300">Page ' + page + ' sur ' + totalPages + '</span></li>';
|
|
|
|
|
|
h += '<li>';
|
|
|
|
|
|
if (nextDisabled) {
|
|
|
|
|
|
h += '<span class="px-3 py-2 leading-tight text-gray-500 bg-gray-200 border border-gray-300 rounded-r-lg">Suivant</span>';
|
2025-08-11 18:43:12 +02:00
|
|
|
|
} else {
|
2025-08-12 10:28:54 +02:00
|
|
|
|
h += '<a href="/autopost/?page=' + (page + 1) + '" data-page="' + (page + 1) + '" class="px-3 py-2 leading-tight text-gray-500 bg-white border border-gray-300 rounded-r-lg hover:bg-gray-100 hover:text-gray-700">Suivant</a>';
|
2025-08-11 18:43:12 +02:00
|
|
|
|
}
|
2025-08-12 10:28:54 +02:00
|
|
|
|
h += '</li>';
|
|
|
|
|
|
h += '</ul>';
|
|
|
|
|
|
|
|
|
|
|
|
return h;
|
2025-08-12 10:15:05 +02:00
|
|
|
|
}
|
2025-08-12 10:28:54 +02:00
|
|
|
|
|
|
|
|
|
|
function updatePagination(page, totalPages) {
|
|
|
|
|
|
$('#pagination').html(renderPaginationHTML(page, totalPages));
|
2025-08-12 10:15:05 +02:00
|
|
|
|
}
|
2025-08-12 10:28:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// --- Data loading ---
|
|
|
|
|
|
function loadPage(p) {
|
|
|
|
|
|
const targetPage = Math.max(1, parseInt(p, 10) || 1);
|
|
|
|
|
|
|
|
|
|
|
|
const isFilter = activeFilter !== null;
|
|
|
|
|
|
const url = isFilter ? '/autopost/filter' : '/autopost/search';
|
|
|
|
|
|
const data = isFilter
|
|
|
|
|
|
? { status: activeFilter, page: targetPage, limit: PAGE_LIMIT }
|
|
|
|
|
|
: { q: activeQuery || '', page: targetPage, limit: PAGE_LIMIT };
|
|
|
|
|
|
|
2025-08-11 13:43:53 +02:00
|
|
|
|
$.ajax({
|
2025-08-12 10:28:54 +02:00
|
|
|
|
url: url,
|
2025-08-12 10:15:05 +02:00
|
|
|
|
type: 'GET',
|
2025-08-12 10:28:54 +02:00
|
|
|
|
data: data,
|
2025-08-12 10:15:05 +02:00
|
|
|
|
dataType: 'json',
|
2025-08-12 10:28:54 +02:00
|
|
|
|
success: function(resp) {
|
|
|
|
|
|
updateTable(resp.rows);
|
|
|
|
|
|
currentPage = resp.page;
|
|
|
|
|
|
currentTotalPages = resp.totalPages;
|
|
|
|
|
|
updatePagination(currentPage, currentTotalPages);
|
2025-08-12 10:15:05 +02:00
|
|
|
|
},
|
2025-08-12 10:28:54 +02:00
|
|
|
|
error: function(jqXHR, textStatus, errorThrown) {
|
|
|
|
|
|
if (textStatus !== 'abort') {
|
|
|
|
|
|
console.error('Erreur AJAX :', textStatus, errorThrown);
|
|
|
|
|
|
alert('Erreur lors du chargement de la page.');
|
|
|
|
|
|
}
|
2025-08-12 10:15:05 +02:00
|
|
|
|
}
|
2025-08-11 13:43:53 +02:00
|
|
|
|
});
|
2025-08-12 10:28:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// --- DOM bindings ---
|
|
|
|
|
|
$(document).ready(function() {
|
|
|
|
|
|
// Recherche
|
|
|
|
|
|
let searchTimer = null;
|
|
|
|
|
|
$('#searchInput').on('keyup', function() {
|
|
|
|
|
|
clearTimeout(searchTimer);
|
|
|
|
|
|
let q = $(this).val();
|
|
|
|
|
|
|
|
|
|
|
|
searchTimer = setTimeout(function() {
|
|
|
|
|
|
activeQuery = q || '';
|
|
|
|
|
|
activeFilter = null; // on sort du mode filtre
|
|
|
|
|
|
$('.filter-card').removeClass('ring-4 ring-white/40');
|
|
|
|
|
|
loadPage(1); // charge page 1 avec pagination AJAX
|
|
|
|
|
|
toggleShowAllButton();
|
|
|
|
|
|
}, 300);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Pagination: intercepter et paginer en AJAX (liste, recherche ou filtre)
|
|
|
|
|
|
$(document).on('click', '#pagination a[data-page]', function(e) {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
const p = parseInt($(this).data('page'), 10);
|
|
|
|
|
|
if (!isNaN(p)) loadPage(p);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Affichage/masquage du bouton "Tout afficher"
|
|
|
|
|
|
function toggleShowAllButton() {
|
|
|
|
|
|
if ($('.filter-card.ring-4').length === 0) {
|
|
|
|
|
|
$('#showAll').addClass('hidden');
|
|
|
|
|
|
} else {
|
|
|
|
|
|
$('#showAll').removeClass('hidden');
|
2025-08-12 10:15:05 +02:00
|
|
|
|
}
|
2025-08-12 10:28:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Filtrage par clic sur une card
|
|
|
|
|
|
$(document).on('click', '.filter-card', function() {
|
|
|
|
|
|
const status = parseInt($(this).data('status'), 10);
|
|
|
|
|
|
|
|
|
|
|
|
$('.filter-card').removeClass('ring-4 ring-white/40');
|
|
|
|
|
|
$(this).addClass('ring-4 ring-white/40');
|
|
|
|
|
|
|
|
|
|
|
|
activeFilter = status;
|
|
|
|
|
|
activeQuery = ''; // on sort du mode recherche
|
|
|
|
|
|
loadPage(1); // page 1 du filtre
|
|
|
|
|
|
toggleShowAllButton();
|
2025-08-11 13:43:53 +02:00
|
|
|
|
});
|
2025-08-12 10:28:54 +02:00
|
|
|
|
|
|
|
|
|
|
// Bouton tout afficher
|
|
|
|
|
|
$(document).on('click', '#showAll', function() {
|
|
|
|
|
|
$('.filter-card').removeClass('ring-4 ring-white/40');
|
|
|
|
|
|
activeFilter = null;
|
|
|
|
|
|
activeQuery = $('#searchInput').val() || '';
|
|
|
|
|
|
loadPage(1); // si recherche vide => liste complète, sinon recherche paginée
|
|
|
|
|
|
toggleShowAllButton();
|
2025-08-11 13:43:53 +02:00
|
|
|
|
});
|
2025-08-12 10:28:54 +02:00
|
|
|
|
|
|
|
|
|
|
// Edition (ouvrir modale)
|
|
|
|
|
|
$(document).on('click', '.edit-link', function(e) {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
var releaseId = $(this).data('id');
|
|
|
|
|
|
var currentStatus = $(this).data('status');
|
|
|
|
|
|
$('#releaseId').val(releaseId);
|
|
|
|
|
|
$('#modalReleaseId').text(releaseId);
|
|
|
|
|
|
$('#statusSelect').val(currentStatus);
|
|
|
|
|
|
$('#editModal').removeClass('hidden').fadeIn();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// ---------- Confirmation de suppression (modale) ----------
|
|
|
|
|
|
var pendingDeleteId = null;
|
|
|
|
|
|
|
|
|
|
|
|
function openConfirmModal(id, name) {
|
|
|
|
|
|
pendingDeleteId = id;
|
|
|
|
|
|
$('#confirmItemName').text(name || ('ID ' + id));
|
|
|
|
|
|
$('#confirmDeleteModal').removeClass('hidden').fadeIn(120);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function closeConfirmModal() {
|
|
|
|
|
|
$('#confirmDeleteModal').fadeOut(120, function() {
|
2025-08-12 10:15:05 +02:00
|
|
|
|
$(this).addClass('hidden');
|
|
|
|
|
|
});
|
2025-08-12 10:28:54 +02:00
|
|
|
|
pendingDeleteId = null;
|
2025-08-11 13:43:53 +02:00
|
|
|
|
}
|
2025-08-12 10:28:54 +02:00
|
|
|
|
|
|
|
|
|
|
function showToast(msg) {
|
|
|
|
|
|
$('#toastMsg').text(msg || 'Opération effectuée');
|
|
|
|
|
|
$('#toast').removeClass('hidden').fadeIn(120);
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
|
|
$('#toast').fadeOut(150, function() { $(this).addClass('hidden'); });
|
|
|
|
|
|
}, 1800);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Ouvrir la modale au clic sur "Supprimer"
|
|
|
|
|
|
$(document).on('click', '.delete-link', function(e) {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
var releaseId = $(this).data('id');
|
|
|
|
|
|
var name = $(this).closest('tr').find('td:first').text().trim();
|
|
|
|
|
|
openConfirmModal(releaseId, name);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Boutons de la modale
|
|
|
|
|
|
$('#cancelDeleteBtn, #confirmDeleteClose').on('click', function() {
|
|
|
|
|
|
closeConfirmModal();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Clic sur l’overlay pour fermer
|
|
|
|
|
|
$('#confirmDeleteModal').on('click', function(e) {
|
|
|
|
|
|
if (e.target === this) { closeConfirmModal(); }
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Accessibilité clavier: Esc = fermer, Enter = confirmer
|
|
|
|
|
|
$(document).on('keydown', function(e) {
|
|
|
|
|
|
var modalVisible = !$('#confirmDeleteModal').hasClass('hidden');
|
|
|
|
|
|
if (!modalVisible) return;
|
|
|
|
|
|
if (e.key === 'Escape') { closeConfirmModal(); }
|
|
|
|
|
|
if (e.key === 'Enter') { $('#confirmDeleteBtn').click(); }
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Confirmer et supprimer via AJAX
|
|
|
|
|
|
$('#confirmDeleteBtn').on('click', function() {
|
|
|
|
|
|
if (!pendingDeleteId) return;
|
|
|
|
|
|
var releaseId = pendingDeleteId;
|
|
|
|
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
|
|
url: '/autopost/delete/' + releaseId,
|
|
|
|
|
|
type: 'POST',
|
|
|
|
|
|
success: function() {
|
|
|
|
|
|
$('#row-' + releaseId)
|
|
|
|
|
|
.css('outline', '2px solid rgba(239,68,68,0.6)')
|
|
|
|
|
|
.fadeOut('300', function(){ $(this).remove(); });
|
|
|
|
|
|
showToast('Enregistrement supprimé');
|
|
|
|
|
|
},
|
|
|
|
|
|
error: function() {
|
|
|
|
|
|
alert('Erreur lors de la suppression.');
|
|
|
|
|
|
},
|
|
|
|
|
|
complete: function() {
|
|
|
|
|
|
closeConfirmModal();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Affichage log
|
|
|
|
|
|
$(document).on('click', '.log-link', function(e) {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
var filename = $(this).data('filename');
|
|
|
|
|
|
$.ajax({
|
|
|
|
|
|
url: '/autopost/log',
|
|
|
|
|
|
type: 'GET',
|
|
|
|
|
|
data: { name: filename },
|
|
|
|
|
|
dataType: 'json',
|
|
|
|
|
|
success: function(data) {
|
|
|
|
|
|
$('#logContent').html(data.content);
|
|
|
|
|
|
$('#logModal').removeClass('hidden').fadeIn();
|
|
|
|
|
|
},
|
|
|
|
|
|
error: function() {
|
|
|
|
|
|
alert('Erreur lors du chargement du fichier log.');
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Affichage mediainfo
|
|
|
|
|
|
$(document).on('click', '.mediainfo-link', function(e) {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
var filename = $(this).data('filename');
|
|
|
|
|
|
$.ajax({
|
|
|
|
|
|
url: '/autopost/mediainfo',
|
|
|
|
|
|
type: 'GET',
|
|
|
|
|
|
data: { name: filename },
|
|
|
|
|
|
dataType: 'json',
|
|
|
|
|
|
success: function(data) {
|
|
|
|
|
|
$('#mediainfoContent').text(data.content);
|
|
|
|
|
|
$('#mediainfoModal').removeClass('hidden').fadeIn();
|
|
|
|
|
|
},
|
|
|
|
|
|
error: function() {
|
|
|
|
|
|
alert('Erreur lors du chargement du fichier mediainfo.');
|
2025-08-12 10:15:05 +02:00
|
|
|
|
}
|
2025-08-12 10:28:54 +02:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Fermeture modales (croix + clic overlay)
|
|
|
|
|
|
$('.close').click(function() {
|
|
|
|
|
|
$(this).closest('.fixed').fadeOut(function() {
|
|
|
|
|
|
$(this).addClass('hidden');
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
$('.fixed').click(function(e) {
|
|
|
|
|
|
if (e.target === this) {
|
|
|
|
|
|
$(this).fadeOut(function() {
|
2025-08-12 10:15:05 +02:00
|
|
|
|
$(this).addClass('hidden');
|
|
|
|
|
|
});
|
2025-08-11 13:43:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
});
|
2025-08-12 10:28:54 +02:00
|
|
|
|
|
|
|
|
|
|
// Edition: submit
|
|
|
|
|
|
$('#editForm').submit(function(e) {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
var releaseId = $('#releaseId').val();
|
|
|
|
|
|
var newStatus = $('#statusSelect').val();
|
|
|
|
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
|
|
url: '/autopost/edit/' + releaseId,
|
|
|
|
|
|
type: 'POST',
|
|
|
|
|
|
data: { status: newStatus },
|
|
|
|
|
|
success: function() {
|
|
|
|
|
|
var statusText = '';
|
|
|
|
|
|
var statusClass = '';
|
|
|
|
|
|
switch (parseInt(newStatus)) {
|
|
|
|
|
|
case 0:
|
|
|
|
|
|
statusText = 'EN ATTENTE';
|
|
|
|
|
|
statusClass = 'bg-cyan-500 text-black font-bold';
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
statusText = 'ENVOI TERMINÉ';
|
|
|
|
|
|
statusClass = 'bg-green-300 text-black font-bold';
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 2:
|
|
|
|
|
|
statusText = 'ERREUR';
|
|
|
|
|
|
statusClass = 'bg-red-300 text-black font-bold';
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 3:
|
|
|
|
|
|
statusText = 'DEJA DISPONIBLE';
|
|
|
|
|
|
statusClass = 'bg-pink-300 text-black font-bold';
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 4:
|
|
|
|
|
|
statusText = 'EN COURS';
|
|
|
|
|
|
statusClass = 'bg-yellow-300 text-black font-bold';
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
statusText = 'INCONNU';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var row = $('#row-' + releaseId);
|
|
|
|
|
|
row.find('.status-text')
|
|
|
|
|
|
.removeClass('bg-cyan-500 bg-green-300 bg-red-300 bg-pink-300 bg-yellow-300')
|
|
|
|
|
|
.addClass(statusClass)
|
|
|
|
|
|
.text(statusText);
|
|
|
|
|
|
|
|
|
|
|
|
$('#editModal').fadeOut(function() {
|
|
|
|
|
|
$(this).addClass('hidden');
|
|
|
|
|
|
});
|
|
|
|
|
|
},
|
|
|
|
|
|
error: function() {
|
|
|
|
|
|
alert('Erreur lors de la mise à jour.');
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
2025-03-12 12:47:51 +01:00
|
|
|
|
});
|
2025-08-12 10:28:54 +02:00
|
|
|
|
|
|
|
|
|
|
// --- Copy Mediainfo JSON button ---
|
|
|
|
|
|
document.getElementById('copyMediainfoBtn').addEventListener('click', function() {
|
|
|
|
|
|
const content = document.getElementById('mediainfoContent').textContent;
|
|
|
|
|
|
navigator.clipboard.writeText(content).then(() => {
|
|
|
|
|
|
this.textContent = '✅ Copié !';
|
|
|
|
|
|
setTimeout(() => this.textContent = '📋 Copier JSON', 2000);
|
|
|
|
|
|
}).catch(function(err) {
|
|
|
|
|
|
alert('Erreur lors de la copie : ' + err);
|
|
|
|
|
|
});
|
2025-08-12 10:15:05 +02:00
|
|
|
|
});
|
2025-08-12 10:28:54 +02:00
|
|
|
|
</script>
|
2025-06-23 14:27:18 +02:00
|
|
|
|
</body>
|
|
|
|
|
|
</html>
|
|
|
|
|
|
`;
|
|
|
|
|
|
res.send(html);
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.error(err.message);
|
|
|
|
|
|
res.status(500).send("Erreur lors de la requête DB.");
|
|
|
|
|
|
}
|
2025-03-12 12:47:51 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
2025-06-23 14:27:18 +02:00
|
|
|
|
// --------------------------- Recherche -----------------------------
|
|
|
|
|
|
autopostRouter.get('/search', async (req, res) => {
|
2025-08-12 10:15:05 +02:00
|
|
|
|
const q = req.query.q || "";
|
|
|
|
|
|
const page = parseInt(req.query.page, 10) || 1;
|
|
|
|
|
|
const limit = parseInt(req.query.limit, 10) || 100;
|
|
|
|
|
|
const offset = (page - 1) * limit;
|
|
|
|
|
|
const searchQuery = `%${q}%`;
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
const [countRows] = await db.query(
|
|
|
|
|
|
`SELECT COUNT(*) AS total FROM \`${config.DB_TABLE}\` WHERE nom LIKE ?`,
|
|
|
|
|
|
[searchQuery]
|
|
|
|
|
|
);
|
|
|
|
|
|
const total = countRows[0].total;
|
|
|
|
|
|
const totalPages = Math.max(1, Math.ceil(total / limit));
|
|
|
|
|
|
|
|
|
|
|
|
const [rows] = await db.query(
|
|
|
|
|
|
`SELECT nom, status, id
|
|
|
|
|
|
FROM \`${config.DB_TABLE}\`
|
|
|
|
|
|
WHERE nom LIKE ?
|
|
|
|
|
|
ORDER BY (status = 2) DESC, id DESC
|
|
|
|
|
|
LIMIT ? OFFSET ?`,
|
|
|
|
|
|
[searchQuery, limit, offset]
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
res.json({ rows, page, limit, total, totalPages });
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.error(err.message);
|
|
|
|
|
|
res.status(500).json({ error: "Erreur lors de la requête." });
|
|
|
|
|
|
}
|
2025-03-12 12:47:51 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
2025-08-12 10:15:05 +02:00
|
|
|
|
|
2025-06-23 14:27:18 +02:00
|
|
|
|
// --------------------------- Log -----------------------------
|
2025-03-12 12:47:51 +01:00
|
|
|
|
autopostRouter.get('/log', (req, res) => {
|
|
|
|
|
|
const filename = req.query.name;
|
|
|
|
|
|
if (!filename) {
|
|
|
|
|
|
return res.status(400).json({ error: "Nom du fichier non spécifié." });
|
|
|
|
|
|
}
|
|
|
|
|
|
let base = filename.includes('.') ? filename.split('.').slice(0, -1).join('.') : filename;
|
2025-03-12 15:46:32 +01:00
|
|
|
|
const logFilePath = path.join(config.logdirectory, base + '.log');
|
2025-03-12 12:47:51 +01:00
|
|
|
|
fs.readFile(logFilePath, 'utf8', (err, data) => {
|
|
|
|
|
|
if (err) {
|
|
|
|
|
|
console.error(err.message);
|
|
|
|
|
|
return res.status(500).json({ error: "Erreur lors du chargement du fichier log." });
|
|
|
|
|
|
}
|
2025-08-11 15:22:45 +02:00
|
|
|
|
|
|
|
|
|
|
// 1) enlever les codes curseur (début de ligne/clear) mais garder les couleurs (…m)
|
|
|
|
|
|
// ex: \x1b[0G, \x1b[0K, etc. — ce sont EUX qui “collent” les blocs
|
|
|
|
|
|
let s = data.replace(/\x1b\[[0-9;]*[GK]/g, '');
|
|
|
|
|
|
|
|
|
|
|
|
// 2) supprimer TOUT segment de progression, même s’ils sont enchaînés sur UNE SEULE ligne
|
|
|
|
|
|
// forme couverte : "12.34% [======] 487.86 MiB/s, ETA 00:01"
|
|
|
|
|
|
// + variantes “ETA -”, et même le cas minimal "0.00% [ ]"
|
|
|
|
|
|
const ANSI_M = '(?:\\x1b\\[[0-9;]*m)*';
|
|
|
|
|
|
const NUM = '\\d{1,3}(?:\\.\\d+)?';
|
|
|
|
|
|
const PROG = new RegExp(
|
|
|
|
|
|
// pourcentage
|
|
|
|
|
|
ANSI_M + '\\s*' + NUM + '\\s*%' +
|
|
|
|
|
|
// barre
|
|
|
|
|
|
'\\s*' + ANSI_M + '\\[[^\\]]*\\]' +
|
|
|
|
|
|
// (option) vitesse + ETA
|
|
|
|
|
|
'(?:\\s*' + ANSI_M + NUM + '\\s*MiB\\/s,\\s*ETA\\s*[^\\x1bG\\n]*)?',
|
|
|
|
|
|
'g'
|
|
|
|
|
|
);
|
|
|
|
|
|
s = s.replace(PROG, '');
|
|
|
|
|
|
|
|
|
|
|
|
// 3) nettoyer les résidus "-G" / "G" laissés juste avant G[INFO] (quand une progression était collée)
|
|
|
|
|
|
s = s.replace(/-?\s*G(?=\[INFO\])/g, '');
|
|
|
|
|
|
|
|
|
|
|
|
// 4) petite normalisation (sans casser les couleurs)
|
|
|
|
|
|
s = s.replace(/[ \t]+/g, ' ')
|
|
|
|
|
|
.replace(/\r/g, '')
|
|
|
|
|
|
.replace(/\n{3,}/g, '\n\n')
|
|
|
|
|
|
// Retour à la ligne avant chaque mot-clé (sauf début)
|
|
|
|
|
|
.replace(/(?!^)(?=\[INFO\])/gm, '\n')
|
|
|
|
|
|
.replace(/(?!^)Calculating\s*:/gm, '\nCalculating :')
|
|
|
|
|
|
.replace(/(?!^)Writing\s*:/gm, '\nWriting :')
|
|
|
|
|
|
.replace(/(?!^)Finalizing\s*:/gm, '\nFinalizing :')
|
|
|
|
|
|
.replace(/(?!^)Finished\s*:/gm, '\nFinished :')
|
|
|
|
|
|
.trim();
|
|
|
|
|
|
|
|
|
|
|
|
const htmlContent = convert.toHtml(s); // conserve les couleurs ANSI
|
2025-03-12 12:47:51 +01:00
|
|
|
|
res.json({ content: htmlContent });
|
|
|
|
|
|
});
|
2025-08-11 15:22:45 +02:00
|
|
|
|
|
2025-03-12 12:47:51 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
2025-08-11 15:22:45 +02:00
|
|
|
|
|
2025-06-23 14:27:18 +02:00
|
|
|
|
// --------------------------- Mediainfo -----------------------------
|
2025-03-12 12:47:51 +01:00
|
|
|
|
autopostRouter.get('/mediainfo', (req, res) => {
|
|
|
|
|
|
const filename = req.query.name;
|
|
|
|
|
|
if (!filename) {
|
|
|
|
|
|
return res.status(400).json({ error: "Nom du fichier non spécifié." });
|
|
|
|
|
|
}
|
2025-08-07 16:13:22 +02:00
|
|
|
|
const base = filename.includes('.') ? filename.split('.').slice(0, -1).join('.') : filename;
|
|
|
|
|
|
const jsonPath = path.join(config.infodirectory, base + '.json');
|
|
|
|
|
|
const bdinfoPath = path.join(config.infodirectory, base + '.bdinfo.txt');
|
|
|
|
|
|
|
|
|
|
|
|
// Vérifie lequel des deux existe, priorité au .json
|
|
|
|
|
|
fs.access(jsonPath, fs.constants.F_OK, (errJson) => {
|
|
|
|
|
|
if (!errJson) {
|
|
|
|
|
|
// .json existe
|
|
|
|
|
|
readAndSend(jsonPath);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// .json n'existe pas, on essaie .bdinfo.txt
|
|
|
|
|
|
fs.access(bdinfoPath, fs.constants.F_OK, (errBdinfo) => {
|
|
|
|
|
|
if (!errBdinfo) {
|
|
|
|
|
|
readAndSend(bdinfoPath);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
res.status(404).json({ error: "Aucun fichier mediainfo trouvé." });
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2025-03-12 12:47:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
});
|
2025-08-07 16:13:22 +02:00
|
|
|
|
|
|
|
|
|
|
function readAndSend(filePath) {
|
|
|
|
|
|
fs.readFile(filePath, 'utf8', (err, data) => {
|
|
|
|
|
|
if (err) {
|
|
|
|
|
|
console.error(err.message);
|
|
|
|
|
|
return res.status(500).json({ error: "Erreur lors du chargement du fichier mediainfo." });
|
|
|
|
|
|
}
|
|
|
|
|
|
// Essaie de prettifier si JSON
|
|
|
|
|
|
try {
|
|
|
|
|
|
const obj = JSON.parse(data);
|
|
|
|
|
|
const pretty = JSON.stringify(obj, null, 2);
|
|
|
|
|
|
res.json({ content: pretty });
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
res.json({ content: data });
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-03-12 12:47:51 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
2025-06-23 14:27:18 +02:00
|
|
|
|
// --------------------------- Download -----------------------------
|
2025-03-12 12:47:51 +01:00
|
|
|
|
autopostRouter.get('/dl', (req, res) => {
|
|
|
|
|
|
const filename = req.query.name;
|
|
|
|
|
|
if (!filename) {
|
|
|
|
|
|
return res.status(400).send("Nom du fichier non spécifié.");
|
|
|
|
|
|
}
|
|
|
|
|
|
// Extraire le nom de base sans extension
|
|
|
|
|
|
let base = filename.includes('.') ? filename.split('.').slice(0, -1).join('.') : filename;
|
|
|
|
|
|
const subfolder = base.charAt(0).toUpperCase();
|
|
|
|
|
|
// L'archive est en .7z maintenant
|
2025-03-12 15:46:32 +01:00
|
|
|
|
const archiveFilePath = path.join(config.finishdirectory, subfolder, base + '.7z');
|
2025-03-12 12:47:51 +01:00
|
|
|
|
console.log("Tentative de téléchargement (archive 7z) :", archiveFilePath);
|
|
|
|
|
|
|
|
|
|
|
|
// Utilisation du répertoire temporaire
|
2025-04-17 19:24:47 +02:00
|
|
|
|
const tmpDir = path.join(__dirname, 'tmp');
|
2025-03-12 12:47:51 +01:00
|
|
|
|
// Le fichier extrait attendu (sans dossier interne grâce à "7z e")
|
|
|
|
|
|
const extractedFilePath = path.join(tmpDir, base + '.nzb');
|
|
|
|
|
|
|
|
|
|
|
|
// Utiliser "7z e" pour extraire sans recréer la structure de dossiers
|
|
|
|
|
|
const command = `7z e "${archiveFilePath}" -o"${tmpDir}" -y`;
|
|
|
|
|
|
|
|
|
|
|
|
exec(command, (error, stdout, stderr) => {
|
|
|
|
|
|
if (error) {
|
|
|
|
|
|
console.error(`Erreur lors de la décompression: ${error.message}`);
|
|
|
|
|
|
return res.status(500).send("Erreur lors de la décompression.");
|
|
|
|
|
|
}
|
|
|
|
|
|
// On vérifie que le fichier extrait existe
|
|
|
|
|
|
res.download(extractedFilePath, base + '.nzb', (err) => {
|
|
|
|
|
|
if (err) {
|
|
|
|
|
|
console.error("Erreur lors du téléchargement :", err);
|
|
|
|
|
|
res.status(500).send("Erreur lors du téléchargement.");
|
|
|
|
|
|
}
|
|
|
|
|
|
// Suppression du fichier temporaire après téléchargement (optionnel)
|
|
|
|
|
|
fs.unlink(extractedFilePath, (err) => {
|
|
|
|
|
|
if (err) {
|
|
|
|
|
|
console.error("Erreur lors de la suppression du fichier temporaire :", err);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-06-23 14:27:18 +02:00
|
|
|
|
// --------------------------- Édition -----------------------------
|
|
|
|
|
|
autopostRouter.post('/edit/:id', async (req, res) => {
|
|
|
|
|
|
const id = req.params.id;
|
|
|
|
|
|
const newStatus = req.body.status;
|
|
|
|
|
|
try {
|
2025-08-08 23:07:13 +02:00
|
|
|
|
await db.query(`UPDATE \`${config.DB_TABLE}\` SET status = ? WHERE id = ?`, [newStatus, id]);
|
2025-06-23 14:27:18 +02:00
|
|
|
|
if (req.xhr || req.headers.accept.indexOf('json') > -1) {
|
|
|
|
|
|
res.json({ success: true });
|
|
|
|
|
|
} else {
|
|
|
|
|
|
res.redirect("/autopost/");
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.error(err.message);
|
|
|
|
|
|
res.status(500).send("Erreur lors de la mise à jour.");
|
2025-03-12 12:47:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-06-23 14:27:18 +02:00
|
|
|
|
// --------------------------- Suppression -----------------------------
|
|
|
|
|
|
autopostRouter.post('/delete/:id', async (req, res) => {
|
|
|
|
|
|
const id = req.params.id;
|
|
|
|
|
|
try {
|
2025-08-08 23:07:13 +02:00
|
|
|
|
await db.query(`DELETE FROM \`${config.DB_TABLE}\` WHERE id = ?`, [id]);
|
2025-06-23 14:27:18 +02:00
|
|
|
|
if (req.xhr || req.headers.accept.indexOf('json') > -1) {
|
|
|
|
|
|
res.json({ success: true });
|
|
|
|
|
|
} else {
|
|
|
|
|
|
res.redirect("/autopost/");
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.error(err.message);
|
|
|
|
|
|
res.status(500).send("Erreur lors de la suppression.");
|
2025-03-12 12:47:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-08-12 10:15:05 +02:00
|
|
|
|
// --------------------------- Filtrage -----------------------------
|
2025-08-11 18:43:12 +02:00
|
|
|
|
autopostRouter.get('/filter', async (req, res) => {
|
2025-08-12 10:15:05 +02:00
|
|
|
|
const status = Number.isFinite(parseInt(req.query.status, 10))
|
|
|
|
|
|
? parseInt(req.query.status, 10)
|
|
|
|
|
|
: null;
|
|
|
|
|
|
if (status === null) {
|
|
|
|
|
|
return res.status(400).json({ error: "Status non fourni." });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const page = parseInt(req.query.page, 10) || 1;
|
|
|
|
|
|
const limit = parseInt(req.query.limit, 10) || 100;
|
|
|
|
|
|
const offset = (page - 1) * limit;
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
const [countRows] = await db.query(
|
|
|
|
|
|
`SELECT COUNT(*) AS total FROM \`${config.DB_TABLE}\` WHERE status = ?`,
|
|
|
|
|
|
[status]
|
|
|
|
|
|
);
|
|
|
|
|
|
const total = countRows[0].total;
|
|
|
|
|
|
const totalPages = Math.max(1, Math.ceil(total / limit));
|
|
|
|
|
|
|
|
|
|
|
|
const [rows] = await db.query(
|
|
|
|
|
|
`SELECT nom, status, id
|
|
|
|
|
|
FROM \`${config.DB_TABLE}\`
|
|
|
|
|
|
WHERE status = ?
|
|
|
|
|
|
ORDER BY id DESC
|
|
|
|
|
|
LIMIT ? OFFSET ?`,
|
|
|
|
|
|
[status, limit, offset]
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
res.json({ rows, page, limit, total, totalPages });
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
console.error(err.message);
|
|
|
|
|
|
res.status(500).json({ error: "Erreur lors de la requête." });
|
|
|
|
|
|
}
|
2025-08-11 18:43:12 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
2025-06-23 14:27:18 +02:00
|
|
|
|
// Redirection accès direct GET /edit/:id
|
2025-03-12 12:47:51 +01:00
|
|
|
|
autopostRouter.get('/edit/:id', (req, res) => {
|
2025-06-23 14:27:18 +02:00
|
|
|
|
res.redirect("/autopost/");
|
2025-03-12 12:47:51 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
app.use('/autopost', autopostRouter);
|
|
|
|
|
|
|
|
|
|
|
|
app.listen(port, () => {
|
2025-06-23 14:27:18 +02:00
|
|
|
|
console.log(`Serveur démarré sur http://localhost:${port}/autopost`);
|
2025-03-12 12:47:51 +01:00
|
|
|
|
});
|