Sections: - The library can be split into named, ordered sections, stored in storage/runtime/sections.json; each track keeps its sectionId. - Sections are created, renamed, reordered and deleted from the page (/api/library/sections). Deleting one keeps its tracks, without a section. - A section, or the tracks without one, can be queued in one go, shuffled or not (enqueue-all takes a sectionId). - The upload form (file or link) picks the section of a kept track. - Without any section, the library stays a plain list. Covers: - A kept track takes the cover embedded in its file (MP3, FLAC...), from the web page or /play garder:. Older tracks are looked at once on startup; a track without one keeps cover: null. - "Renommer" becomes "Modifier": a dialog to change the title, the section and the cover (upload with preview, or removal). - ffmpeg turns covers into JPEGs of at most 512 px, stored in storage/covers and deleted with their track. An uploaded image is only passed to ffmpeg after its signature is checked (PNG, JPEG, WebP, GIF), with that decoder forced, so ffmpeg never guesses the format of an unknown file. - Covers are served under /covers without authentication, since an <img> cannot send the key; their random names are only given by the API and never change content (one-year cache). - The current track and the queue show the cover of their library track. Docs: README lists the new data to back up; storage/covers is ignored. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
107 lines
3.6 KiB
JavaScript
107 lines
3.6 KiB
JavaScript
/**
|
|
* @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 };
|