Initial commit: Club DynaSQY
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const pool = require('../config/db');
|
||||
|
||||
const galleryDir = path.join(__dirname, '..', 'gallery');
|
||||
|
||||
exports.list = async (req, res) => {
|
||||
try {
|
||||
if (!fs.existsSync(galleryDir)) return res.json([]);
|
||||
const dates = fs.readdirSync(galleryDir).filter((d) => fs.statSync(path.join(galleryDir, d)).isDirectory()).sort().reverse();
|
||||
const result = [];
|
||||
for (const date of dates) {
|
||||
const dirPath = path.join(galleryDir, date);
|
||||
const files = fs.readdirSync(dirPath)
|
||||
.filter((f) => /\.(jpg|jpeg|png|gif|webp|heic)$/i.test(f) && f !== '.thumbnails')
|
||||
.map((f) => ({ filename: f, url: `/gallery/${date}/${encodeURIComponent(f)}` }))
|
||||
.sort();
|
||||
if (files.length > 0) result.push({ date, files });
|
||||
}
|
||||
res.json(result);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: 'Erreur serveur' });
|
||||
}
|
||||
};
|
||||
|
||||
exports.upload = async (req, res) => {
|
||||
try {
|
||||
if (!req.files || req.files.length === 0) {
|
||||
return res.status(400).json({ error: 'Aucun fichier sélectionné' });
|
||||
}
|
||||
const { date_club } = req.body;
|
||||
if (!date_club) return res.status(400).json({ error: 'Date du rendez-vous requise' });
|
||||
const dateFolder = date_club.replace(/-/g, '');
|
||||
const targetDir = path.join(galleryDir, dateFolder);
|
||||
if (!fs.existsSync(targetDir)) fs.mkdirSync(targetDir, { recursive: true });
|
||||
const saved = [];
|
||||
for (const file of req.files) {
|
||||
const ext = path.extname(file.originalname);
|
||||
const baseName = path.basename(file.originalname, ext);
|
||||
let destPath = path.join(targetDir, file.originalname);
|
||||
let counter = 1;
|
||||
while (fs.existsSync(destPath)) {
|
||||
destPath = path.join(targetDir, `${baseName}_${counter}${ext}`);
|
||||
counter++;
|
||||
}
|
||||
fs.renameSync(file.path, destPath);
|
||||
saved.push({ filename: path.basename(destPath), url: `/gallery/${dateFolder}/${path.basename(destPath)}` });
|
||||
}
|
||||
res.json({ message: `${saved.length} photo(s) ajoutée(s)`, files: saved });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: 'Erreur upload' });
|
||||
}
|
||||
};
|
||||
|
||||
exports.deletePhoto = async (req, res) => {
|
||||
try {
|
||||
if (req.user.role !== 'admin') return res.status(403).json({ error: 'Admin seulement' });
|
||||
const { date, filename } = req.params;
|
||||
const filePath = path.join(galleryDir, date, filename);
|
||||
if (!fs.existsSync(filePath)) return res.status(404).json({ error: 'Fichier introuvable' });
|
||||
fs.unlinkSync(filePath);
|
||||
const thumbPath = path.join(galleryDir, date, '.thumbnails', filename);
|
||||
if (fs.existsSync(thumbPath)) fs.unlinkSync(thumbPath);
|
||||
const dirPath = path.join(galleryDir, date);
|
||||
if (fs.existsSync(dirPath) && fs.readdirSync(dirPath).filter((f) => f !== '.thumbnails').length === 0) {
|
||||
const thumbDir = path.join(galleryDir, date, '.thumbnails');
|
||||
if (fs.existsSync(thumbDir)) fs.rmdirSync(thumbDir);
|
||||
fs.rmdirSync(dirPath);
|
||||
}
|
||||
res.json({ message: 'Photo supprimée' });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: 'Erreur serveur' });
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user