/** * @file Sections de la bibliothèque : liste ordonnée de groupes nommés, enregistrée dans * storage/runtime/sections.json. Chaque morceau désigne la sienne par son champ `sectionId`. * * Utilisé par : web/routes/librarySections.js, web/routes/library.js et uploads.js (section * choisie pour un morceau). * Attention : supprimer une section remet ses morceaux « sans section », sans les supprimer. Un * fichier absent vaut une liste vide. */ import fs from "node:fs"; import path from "node:path"; import { randomUUID } from "node:crypto"; import { getPermanentLibrary, RUNTIME_DIR, updatePermanentTracks } from "./storageManager.js"; const SECTIONS_FILE = path.join(RUNTIME_DIR, "sections.json"); const MAX_SECTIONS = 100; const MAX_NAME_LENGTH = 80; function httpError(status, message) { return Object.assign(new Error(message), { status }); } function cleanName(value) { const name = String(value ?? "") .replace(/[\u0000-\u001f]/g, "") .trim() .slice(0, MAX_NAME_LENGTH); if (!name) { throw httpError(400, "Nom de section manquant"); } return name; } function getSections() { try { const sections = JSON.parse(fs.readFileSync(SECTIONS_FILE, "utf8")); if (!Array.isArray(sections)) { throw new TypeError("sections.json doit contenir un tableau"); } return sections.filter( (section) => typeof section?.id === "string" && typeof section.name === "string" ); } catch (error) { if (error.code === "ENOENT") return []; console.error("Failed to read sections file", error); throw new Error("Impossible de lire les sections", { cause: error }); } } function writeSections(sections) { fs.mkdirSync(RUNTIME_DIR, { recursive: true }); const temporaryFile = `${SECTIONS_FILE}.${process.pid}.tmp`; fs.writeFileSync(temporaryFile, JSON.stringify(sections, null, 2)); fs.renameSync(temporaryFile, SECTIONS_FILE); } function findSection(id) { return typeof id === "string" ? getSections().find((section) => section.id === id) : undefined; } function createSection(name) { const sections = getSections(); if (sections.length >= MAX_SECTIONS) { throw httpError(409, `Pas plus de ${MAX_SECTIONS} sections`); } const section = { id: randomUUID(), name: cleanName(name) }; sections.push(section); writeSections(sections); return section; } // Renomme la section et/ou la déplace à `position` (0 : en tête). null si elle n'existe pas. function updateSection(id, { name, position } = {}) { const sections = getSections(); const index = sections.findIndex((section) => section.id === id); if (index === -1) return null; const section = sections[index]; if (name !== undefined) { section.name = cleanName(name); } if (position !== undefined) { if (!Number.isInteger(position) || position < 0 || position >= sections.length) { throw httpError(400, "Position invalide"); } sections.splice(index, 1); sections.splice(position, 0, section); } writeSections(sections); return section; } // Supprime la section ; ses morceaux restent dans la bibliothèque, sans section. function deleteSection(id) { const sections = getSections(); const index = sections.findIndex((section) => section.id === id); if (index === -1) return null; const [removed] = sections.splice(index, 1); writeSections(sections); const orphans = getPermanentLibrary().filter((entry) => entry.sectionId === id); updatePermanentTracks(new Map(orphans.map((entry) => [entry.id, { sectionId: null }]))); return removed; } export { createSection, deleteSection, findSection, getSections, updateSection };