Add tracks from a direct link and from Discord with /play, /skip, /queue

- Download direct audio links server side (web page and /play), refusing
  internal and special addresses at connection time and after every
  redirect, with size and time limits.
- /play takes an attachment, a direct link or a library track
  (autocomplete), and can keep the file in the library; /skip and /queue
  control and show the shared queue.
- Share one ingest path between uploads, links and Discord attachments.
- Answer 413 instead of 500 when the library is full.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Shinuwa 2026-11-29 12:13:12 +01:00
parent 98f4d29ac7
commit 39d2b6acfb
14 changed files with 715 additions and 54 deletions

View file

@ -31,7 +31,7 @@ async function loadCommands() {
test("all Discord commands expose valid command data", async () => {
const modules = await loadCommands();
assert.equal(modules.length, 10);
assert.equal(modules.length, 13);
for (const module of modules) {
assert.doesNotThrow(() => module.command.data.toJSON());
assert.equal(typeof module.command.execute, "function");
@ -57,3 +57,13 @@ test("administrative commands require Discord permissions", async () => {
}
}
});
test("/queue lists the current track and what comes next", async () => {
const { buildQueueMessage } = await import("../src/commands/jukebox/queue.js");
const message = buildQueueMessage({
currentTrack: { title: "Thème *principal*", duration: 187 },
queue: [{ title: "Suite", duration: 3909 }],
});
assert.match(message, /En cours : \*\*Thème \\\*principal\\\* \(3:07\)\*\*/);
assert.match(message, /1\. Suite \(1:05:09\)/);
});

114
test/downloadAudio.test.js Normal file
View file

@ -0,0 +1,114 @@
import test from "node:test";
import assert from "node:assert/strict";
import dns from "node:dns";
import fs from "node:fs";
import http from "node:http";
import os from "node:os";
import path from "node:path";
import { downloadAudio, isBlockedAddress } from "../src/media/downloadAudio.js";
const directory = fs.mkdtempSync(path.join(os.tmpdir(), "palico-download-"));
test.after(() => fs.rmSync(directory, { recursive: true, force: true }));
// Le serveur de test écoute en local, adresse que le téléchargeur refuse : les tests qui
// doivent l'atteindre passent une résolution DNS sans filtre.
const allowLocal = (hostname, options, callback) => dns.lookup(hostname, options, callback);
async function withServer(handler, run) {
const server = http.createServer(handler);
server.listen(0, "127.0.0.1");
await new Promise((resolve) => server.once("listening", resolve));
try {
await run(`http://localhost:${server.address().port}`);
} finally {
await new Promise((resolve) => server.close(resolve));
}
}
test("internal and special addresses are blocked", () => {
for (const address of ["127.0.0.1", "10.1.2.3", "192.168.0.100", "172.20.0.1", "169.254.169.254", "100.64.0.1", "::1", "fd00::1", "fe80::1", "::ffff:10.0.0.1"]) {
assert.equal(isBlockedAddress(address), true, address);
}
for (const address of ["1.1.1.1", "151.101.130.132", "2606:4700::1111"]) {
assert.equal(isBlockedAddress(address), false, address);
}
});
test("a direct link is downloaded with its file name", async () => {
await withServer(
(req, res) => {
res.writeHead(200, {
"content-type": "audio/mpeg",
"content-disposition": "attachment; filename=\"chanson.mp3\"",
});
res.end("fake audio");
},
async (base) => {
const file = await downloadAudio(`${base}/download?id=1`, {
destinationDir: directory,
lookup: allowLocal,
});
assert.equal(file.originalname, "chanson.mp3");
assert.equal(file.mimetype, "audio/mpeg");
assert.equal(file.size, 10);
assert.equal(fs.readFileSync(file.path, "utf8"), "fake audio");
}
);
});
test("local addresses are refused, written as an IP or behind a host name", async () => {
await assert.rejects(downloadAudio("http://127.0.0.1:1/a.mp3", { destinationDir: directory }), {
status: 400,
});
await withServer(
(req, res) => res.end("secret"),
async (base) => {
await assert.rejects(downloadAudio(`${base}/a.mp3`, { destinationDir: directory }), {
message: /non autorisée/,
});
}
);
});
test("a redirect towards an internal address is refused", async () => {
await withServer(
(req, res) => {
res.writeHead(302, { location: "http://192.168.0.100/admin" });
res.end();
},
async (base) => {
await assert.rejects(
downloadAudio(`${base}/a.mp3`, { destinationDir: directory, lookup: allowLocal }),
{ message: /non autorisée/ }
);
}
);
});
test("files over the size limit are refused and removed", async () => {
await withServer(
(req, res) => {
res.writeHead(200);
res.write(Buffer.alloc(600));
res.end(Buffer.alloc(600));
},
async (base) => {
const before = fs.readdirSync(directory).length;
await assert.rejects(
downloadAudio(`${base}/big.mp3`, {
destinationDir: directory,
lookup: allowLocal,
maxSize: 1000,
}),
{ status: 413 }
);
assert.equal(fs.readdirSync(directory).length, before);
}
);
});
test("only http and https links are accepted", async () => {
await assert.rejects(downloadAudio("file:///etc/passwd", { destinationDir: directory }), {
status: 400,
});
});

67
test/ingest.test.js Normal file
View file

@ -0,0 +1,67 @@
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-ingest-"));
process.chdir(temporaryDirectory);
const storage = await import("../src/web/storage/storageManager.js");
const { defaultTitle, ingestAudioFile } = await import("../src/music/ingest.js");
storage.ensureStorageLayout();
test.after(() => {
process.chdir(originalDirectory);
fs.rmSync(temporaryDirectory, { recursive: true, force: true });
});
function fakeQueue() {
const queue = new EventEmitter();
queue.enqueued = [];
queue.enqueue = (track) => queue.enqueued.push(track);
return queue;
}
function tempFile(name) {
const filePath = path.join(storage.TEMP_DIR, `upload-${name}`);
fs.writeFileSync(filePath, "audio");
return { path: filePath, originalname: name, size: 5, mimetype: "audio/mpeg" };
}
test("default titles come from the tags, else from the file name", () => {
assert.equal(defaultTitle({ title: "Song", artist: "Band" }, "x.mp3"), "Band - Song");
assert.equal(defaultTitle({ title: "Song", artist: null }, "x.mp3"), "Song");
assert.equal(defaultTitle({ title: null, artist: null }, "Mon morceau.flac"), "Mon morceau");
});
test("an ingested file is queued with its metadata", async () => {
const queue = fakeQueue();
let libraryUpdates = 0;
queue.on("libraryUpdate", () => libraryUpdates++);
const track = await ingestAudioFile(
tempFile("a.mp3"),
{ permanent: true, uploader: "Shinuwa\u0007" },
{ queue, probe: async () => ({ duration: 61, title: "Titre", artist: "Artiste" }) }
);
assert.equal(track.title, "Artiste - Titre");
assert.equal(track.duration, 61);
assert.equal(track.uploader, "Shinuwa");
assert.deepEqual(queue.enqueued, [track]);
assert.equal(libraryUpdates, 1);
assert.equal(storage.getPermanentLibrary()[0].duration, 61);
});
test("a file that is not audio is deleted", async () => {
const file = tempFile("b.mp3");
const invalid = Object.assign(new Error("pas de l'audio"), { status: 400 });
await assert.rejects(
ingestAudioFile(file, {}, { queue: fakeQueue(), probe: async () => { throw invalid; } }),
invalid
);
assert.equal(fs.existsSync(file.path), false);
});