Add library sections and track covers
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>
This commit is contained in:
parent
3795ee06cb
commit
11ee972636
19 changed files with 1258 additions and 76 deletions
87
test/coverArt.test.js
Normal file
87
test/coverArt.test.js
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
/**
|
||||
* @file Vérifie la fabrication des pochettes avec le vrai ffmpeg (image envoyée, pochette intégrée à
|
||||
* un MP3) et le refus des fichiers qui ne sont pas des images ; ignoré sans ffmpeg.
|
||||
*/
|
||||
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { createCoverImage, detectImageFormat } from "../src/media/coverArt.js";
|
||||
|
||||
const hasFfmpeg =
|
||||
spawnSync("ffmpeg", ["-version"]).status === 0 &&
|
||||
spawnSync("ffprobe", ["-version"]).status === 0;
|
||||
const skip = hasFfmpeg ? false : "ffmpeg et ffprobe sont nécessaires";
|
||||
|
||||
const directory = fs.mkdtempSync(path.join(os.tmpdir(), "palico-cover-"));
|
||||
test.after(() => fs.rmSync(directory, { recursive: true, force: true }));
|
||||
|
||||
function ffmpeg(...args) {
|
||||
const result = spawnSync("ffmpeg", ["-v", "error", "-y", ...args]);
|
||||
assert.equal(result.status, 0, result.stderr?.toString());
|
||||
}
|
||||
|
||||
function imageSize(file) {
|
||||
const result = spawnSync("ffprobe", [
|
||||
"-v", "error", "-select_streams", "v:0",
|
||||
"-show_entries", "stream=width,height,codec_name", "-of", "json", file,
|
||||
]);
|
||||
return JSON.parse(result.stdout.toString()).streams[0];
|
||||
}
|
||||
|
||||
test("image signatures are recognised, other files are not", async () => {
|
||||
const samples = {
|
||||
"a.png": Buffer.from("89504e470d0a1a0a0000000d", "hex"),
|
||||
"a.jpg": Buffer.from("ffd8ffe000104a4649460001", "hex"),
|
||||
"a.gif": Buffer.from("GIF89a\x01\x00\x01\x00\x00\x00", "latin1"),
|
||||
"a.webp": Buffer.from("RIFF\x24\x00\x00\x00WEBPVP8 ", "latin1"),
|
||||
"playlist.png": Buffer.from("#EXTM3U\n#EXT-X-MEDIA-SEQUENCE:0\n"),
|
||||
"short.png": Buffer.from("89504e47", "hex"),
|
||||
};
|
||||
const formats = {};
|
||||
for (const [name, bytes] of Object.entries(samples)) {
|
||||
const file = path.join(directory, name);
|
||||
fs.writeFileSync(file, bytes);
|
||||
formats[name] = await detectImageFormat(file);
|
||||
}
|
||||
assert.deepEqual(formats, {
|
||||
"a.png": "png_pipe",
|
||||
"a.jpg": "jpeg_pipe",
|
||||
"a.gif": "gif",
|
||||
"a.webp": "webp_pipe",
|
||||
"playlist.png": null,
|
||||
"short.png": null,
|
||||
});
|
||||
});
|
||||
|
||||
test("an uploaded image becomes a JPEG of at most 512 px", { skip }, async () => {
|
||||
const input = path.join(directory, "wide.png");
|
||||
ffmpeg("-f", "lavfi", "-i", "color=c=purple:s=1024x256", "-frames:v", "1", input);
|
||||
const output = path.join(directory, "wide.jpg");
|
||||
|
||||
await createCoverImage(input, output, { inputFormat: await detectImageFormat(input) });
|
||||
|
||||
assert.deepEqual(imageSize(output), { codec_name: "mjpeg", width: 512, height: 128 });
|
||||
});
|
||||
|
||||
test("the cover embedded in an MP3 is extracted, and its absence is an error", { skip }, async () => {
|
||||
const picture = path.join(directory, "picture.jpg");
|
||||
ffmpeg("-f", "lavfi", "-i", "color=c=orange:s=300x300", "-frames:v", "1", picture);
|
||||
const withCover = path.join(directory, "with-cover.mp3");
|
||||
ffmpeg(
|
||||
"-f", "lavfi", "-i", "sine=frequency=440:duration=1", "-i", picture,
|
||||
"-map", "0:a", "-map", "1:v", "-c:a", "libmp3lame", "-c:v", "copy",
|
||||
"-disposition:v", "attached_pic", withCover
|
||||
);
|
||||
const withoutCover = path.join(directory, "without-cover.mp3");
|
||||
ffmpeg("-f", "lavfi", "-i", "sine=frequency=440:duration=1", "-c:a", "libmp3lame", withoutCover);
|
||||
|
||||
const output = path.join(directory, "extracted.jpg");
|
||||
await createCoverImage(withCover, output);
|
||||
assert.deepEqual(imageSize(output), { codec_name: "mjpeg", width: 300, height: 300 });
|
||||
|
||||
await assert.rejects(createCoverImage(withoutCover, path.join(directory, "none.jpg")));
|
||||
});
|
||||
118
test/libraryCovers.test.js
Normal file
118
test/libraryCovers.test.js
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
/**
|
||||
* @file Vérifie le stockage des pochettes sans ffmpeg : pochette intégrée reprise à l'ajout,
|
||||
* remplacement, refus d'un fichier qui n'est pas une image, suppression avec le morceau, et
|
||||
* adresse donnée par l'API.
|
||||
*/
|
||||
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { EventEmitter } from "node:events";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
// storageManager.js fixe ses dossiers au chargement, d'après le répertoire de travail.
|
||||
const originalDirectory = process.cwd();
|
||||
const temporaryDirectory = fs.mkdtempSync(path.join(os.tmpdir(), "palico-covers-"));
|
||||
process.chdir(temporaryDirectory);
|
||||
const storage = await import("../src/web/storage/storageManager.js");
|
||||
const covers = await import("../src/web/storage/libraryCovers.js");
|
||||
const { ingestAudioFile } = await import("../src/music/ingest.js");
|
||||
const { backfillLibraryMetadata } = await import("../src/web/storage/libraryMetadata.js");
|
||||
const { serializeTrack } = await import("../src/web/serializers.js");
|
||||
storage.ensureStorageLayout();
|
||||
test.after(() => {
|
||||
process.chdir(originalDirectory);
|
||||
fs.rmSync(temporaryDirectory, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
// Remplace ffmpeg : écrit un faux JPEG, ou échoue comme pour un fichier sans pochette.
|
||||
const fakeCreate = async (_input, output) => fs.writeFileSync(output, "jpeg");
|
||||
const noCover = async () => {
|
||||
throw new Error("Output file does not contain any stream");
|
||||
};
|
||||
const probe = async () => ({ duration: 10, title: null, artist: null });
|
||||
|
||||
function tempFile(name, content = "audio") {
|
||||
const filePath = path.join(storage.TEMP_DIR, `upload-${name}`);
|
||||
fs.writeFileSync(filePath, Buffer.from(content, "latin1"));
|
||||
return { path: filePath, originalname: name, size: content.length, mimetype: "audio/mpeg" };
|
||||
}
|
||||
|
||||
function fakeQueue() {
|
||||
const queue = new EventEmitter();
|
||||
queue.enqueue = () => {};
|
||||
return queue;
|
||||
}
|
||||
|
||||
const coverPath = (name) => path.join(storage.COVERS_DIR, name);
|
||||
|
||||
test("a kept track takes the cover embedded in its file", async () => {
|
||||
const track = await ingestAudioFile(
|
||||
tempFile("with.mp3"),
|
||||
{ permanent: true },
|
||||
{ queue: fakeQueue(), probe, createCover: fakeCreate }
|
||||
);
|
||||
const entry = storage.findPermanentTrack(track.libraryTrackId);
|
||||
assert.match(entry.cover, /^[0-9a-f-]{36}\.jpg$/);
|
||||
assert.equal(fs.existsSync(coverPath(entry.cover)), true);
|
||||
assert.equal(serializeTrack(entry).coverUrl, `/covers/${entry.cover}`);
|
||||
assert.equal("cover" in serializeTrack(entry), false);
|
||||
|
||||
const without = await ingestAudioFile(
|
||||
tempFile("without.mp3"),
|
||||
{ permanent: true },
|
||||
{ queue: fakeQueue(), probe, createCover: noCover }
|
||||
);
|
||||
const withoutEntry = storage.findPermanentTrack(without.libraryTrackId);
|
||||
// Examiné une fois : null, pour ne pas être retenté à chaque démarrage.
|
||||
assert.equal(withoutEntry.cover, null);
|
||||
assert.equal("coverUrl" in serializeTrack(withoutEntry), false);
|
||||
});
|
||||
|
||||
test("a new cover replaces the old file, and only images are accepted", async () => {
|
||||
const id = (
|
||||
await ingestAudioFile(tempFile("c.mp3"), { permanent: true }, {
|
||||
queue: fakeQueue(), probe, createCover: fakeCreate,
|
||||
})
|
||||
).libraryTrackId;
|
||||
const oldCover = storage.findPermanentTrack(id).cover;
|
||||
|
||||
const png = tempFile("cover.png", "\x89PNG\r\n\x1a\n0000");
|
||||
const newCover = await covers.saveCoverFromImage(png.path, { create: fakeCreate });
|
||||
covers.setTrackCover(id, newCover);
|
||||
assert.equal(storage.findPermanentTrack(id).cover, newCover);
|
||||
assert.equal(fs.existsSync(coverPath(oldCover)), false);
|
||||
|
||||
const text = tempFile("cover.txt", "#EXTM3U\nfile:///etc/passwd\n");
|
||||
await assert.rejects(covers.saveCoverFromImage(text.path, { create: fakeCreate }), {
|
||||
status: 400,
|
||||
});
|
||||
|
||||
covers.setTrackCover(id, null);
|
||||
assert.equal(storage.findPermanentTrack(id).cover, null);
|
||||
assert.equal(fs.existsSync(coverPath(newCover)), false);
|
||||
});
|
||||
|
||||
test("deleting a track deletes its cover", async () => {
|
||||
const { libraryTrackId } = await ingestAudioFile(tempFile("d.mp3"), { permanent: true }, {
|
||||
queue: fakeQueue(), probe, createCover: fakeCreate,
|
||||
});
|
||||
const { cover } = storage.findPermanentTrack(libraryTrackId);
|
||||
storage.deletePermanentTrack(libraryTrackId);
|
||||
assert.equal(fs.existsSync(coverPath(cover)), false);
|
||||
});
|
||||
|
||||
test("the startup backfill looks once for the covers of older tracks", async () => {
|
||||
const older = storage.registerUploadedFile(tempFile("old.mp3"), {
|
||||
permanent: true,
|
||||
duration: 10,
|
||||
}).libraryTrackId;
|
||||
assert.equal(storage.findPermanentTrack(older).cover, undefined);
|
||||
|
||||
const updated = await backfillLibraryMetadata({ probe, createCover: fakeCreate });
|
||||
assert.deepEqual(updated.map((entry) => entry.id), [older]);
|
||||
assert.ok(storage.findPermanentTrack(older).cover);
|
||||
|
||||
assert.deepEqual(await backfillLibraryMetadata({ probe, createCover: fakeCreate }), []);
|
||||
});
|
||||
68
test/librarySections.test.js
Normal file
68
test/librarySections.test.js
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
/**
|
||||
* @file Vérifie les sections de la bibliothèque : création, renommage, déplacement, et suppression qui
|
||||
* remet ses morceaux sans section.
|
||||
*/
|
||||
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
// storageManager.js fixe ses dossiers au chargement, d'après le répertoire de travail.
|
||||
const originalDirectory = process.cwd();
|
||||
const temporaryDirectory = fs.mkdtempSync(path.join(os.tmpdir(), "palico-sections-"));
|
||||
process.chdir(temporaryDirectory);
|
||||
const storage = await import("../src/web/storage/storageManager.js");
|
||||
const sections = await import("../src/web/storage/librarySections.js");
|
||||
storage.ensureStorageLayout();
|
||||
test.after(() => {
|
||||
process.chdir(originalDirectory);
|
||||
fs.rmSync(temporaryDirectory, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
function addTrack(name, sectionId) {
|
||||
const filePath = path.join(storage.TEMP_DIR, `upload-${name}`);
|
||||
fs.writeFileSync(filePath, "audio");
|
||||
return storage.registerUploadedFile(
|
||||
{ path: filePath, originalname: name, size: 5, mimetype: "audio/mpeg" },
|
||||
{ permanent: true, uploader: "test", sectionId }
|
||||
).libraryTrackId;
|
||||
}
|
||||
|
||||
test("sections are created, renamed and reordered", () => {
|
||||
assert.deepEqual(sections.getSections(), []);
|
||||
const chill = sections.createSection(" Chill\u0007 ");
|
||||
const boss = sections.createSection("Combats de boss");
|
||||
const menu = sections.createSection("Menus");
|
||||
assert.equal(chill.name, "Chill");
|
||||
|
||||
sections.updateSection(menu.id, { name: "Écrans titres", position: 0 });
|
||||
assert.deepEqual(
|
||||
sections.getSections().map((section) => section.name),
|
||||
["Écrans titres", "Chill", "Combats de boss"]
|
||||
);
|
||||
assert.equal(sections.findSection(boss.id).name, "Combats de boss");
|
||||
assert.equal(sections.updateSection("unknown", { name: "x" }), null);
|
||||
});
|
||||
|
||||
test("invalid names and positions are refused", () => {
|
||||
const [first] = sections.getSections();
|
||||
assert.throws(() => sections.createSection(" "), { status: 400 });
|
||||
assert.throws(() => sections.updateSection(first.id, { position: 99 }), { status: 400 });
|
||||
assert.throws(() => sections.updateSection(first.id, { position: "1" }), { status: 400 });
|
||||
});
|
||||
|
||||
test("deleting a section keeps its tracks, without a section", () => {
|
||||
const doomed = sections.createSection("À supprimer");
|
||||
const kept = sections.createSection("Gardée");
|
||||
const inDoomed = addTrack("a.mp3", doomed.id);
|
||||
const inKept = addTrack("b.mp3", kept.id);
|
||||
|
||||
assert.equal(sections.deleteSection(doomed.id).id, doomed.id);
|
||||
|
||||
assert.equal(sections.findSection(doomed.id), undefined);
|
||||
assert.equal(storage.findPermanentTrack(inDoomed).sectionId, null);
|
||||
assert.equal(storage.findPermanentTrack(inKept).sectionId, kept.id);
|
||||
assert.equal(sections.deleteSection(doomed.id), null);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue