mirror of
https://github.com/burrow-dev/burrow-server.git
synced 2026-11-18 17:27:50 -05:00
feat(music): staff upload MP3s as den music tracks
A "Music" page in both panels: an MP3 (MPEG layer III frames, 4 KB to 15 MB) becomes a den music track at once, with no restart. - lib/custommusic.js: a custom den item made from "Jam On" (614) with a new `audio` column, the file's version. Its abbrName is CustomMusic<id>_<version>, which the content handler answers from data/custom-music/<id>-<version>.mp3, so a new upload is a new name and no game plays an old copy from its cache. - games get it by "xdi" with no client change; players from the item finder, or the shop when staff tick "on sale". - deleting it takes it from every inventory and removes the file. - /mod/api/music and /admin/api/music: list, play, upload (name, cost, inShop in the query), delete; 30 uploads an hour per staff member; "music uploaded"/"music deleted" in the mod log. - new permission music.upload, on the Admin role by default; a role list the admin has changed needs it ticked. The panels' CSP allows media-src 'self' for the preview players. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
a77a1bcfb4
commit
2344270291
9 files changed
+517
-8
No files matched your search
+27
-3
@@ -65,6 +65,11 @@ const now = () => Math.floor(Date.now() / 1000);
|
||||
|
||||
const baseDef = (kind, id) => (kind === "den" ? defpacks.denItemDef(id) : defpacks.itemDef(id));
|
||||
|
||||
// the file name an uploaded music track is asked for by (lib/custommusic.js):
|
||||
// a new upload is a new name, so no game plays an old copy from its cache.
|
||||
const audioName = entry => `CustomMusic${entry.id}_${entry.audio}`;
|
||||
let audioGone = () => {}; // (entry): an uploaded track's item was deleted; set by lib/custommusic.js
|
||||
|
||||
// the def row of an entry: its base's row with its own id, name, price and
|
||||
// colours, always on sale and marked new.
|
||||
const rowOf = entry => {
|
||||
@@ -80,6 +85,11 @@ const rowOf = entry => {
|
||||
combinedCost: "",
|
||||
};
|
||||
if (entry.kind === "den") {
|
||||
// a music track uploaded by staff (lib/custommusic.js): the game plays
|
||||
// "audio/<abbrName>.mp3", which the server answers with its file
|
||||
if (entry.audio) {
|
||||
own.abbrName = audioName(entry);
|
||||
}
|
||||
return {...base, ...own, nameStrId: String(STRING_BASE.den + entry.id - FIRST_ID), cost: String(entry.cost)};
|
||||
}
|
||||
return {...base, ...own, titleStrId: String(STRING_BASE.clothing + entry.id - FIRST_ID), value: String(entry.cost), colors: entry.colors.join(","), isGiftable: "1"};
|
||||
@@ -155,6 +165,7 @@ const fromRow = row => ({
|
||||
artMode: LOOKS.includes(row.art_mode) ? row.art_mode : "sticker",
|
||||
pending: row.pending === 1,
|
||||
artPending: row.art_pending === 1,
|
||||
audio: /^[0-9a-f]{8,64}$/.test(row.audio || "") ? row.audio : null,
|
||||
});
|
||||
|
||||
// a player-made item (lib/playeritems.js) has the maker's account id;
|
||||
@@ -179,6 +190,10 @@ const addOwnerColumn = () => {
|
||||
if (!columns.includes("art_pending")) {
|
||||
db().exec("ALTER TABLE custom_items ADD COLUMN art_pending INTEGER");
|
||||
}
|
||||
// a music track uploaded by staff (lib/custommusic.js): its file's version
|
||||
if (!columns.includes("audio")) {
|
||||
db().exec("ALTER TABLE custom_items ADD COLUMN audio TEXT");
|
||||
}
|
||||
};
|
||||
|
||||
const load = () => {
|
||||
@@ -269,10 +284,12 @@ const create = (kind, fields, by, ownerId = null, pending = false) => {
|
||||
return values;
|
||||
}
|
||||
const id = Math.max(FIRST_ID - 1, ...items.get(kind).keys()) + 1;
|
||||
// `fields.audio`: an uploaded music track's version (lib/custommusic.js)
|
||||
const audio = kind === "den" && /^[0-9a-f]{8,64}$/.test(String(fields.audio || "")) ? String(fields.audio) : null;
|
||||
const entry = {kind, id, base: baseId, colors: [], ...values, createdBy: String(by), createdAt: now(), ownerId, artMode: "sticker", pending, artPending: false,
|
||||
seq: made++};
|
||||
db().prepare("INSERT INTO custom_items (kind, id, base, name, cost, members, colors, in_shop, created_by, created_at, owner_id, pending) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
|
||||
.run(kind, id, baseId, entry.name, entry.cost, entry.members ? 1 : 0, entry.colors.join(","), entry.inShop ? 1 : 0, entry.createdBy, entry.createdAt, ownerId, pending ? 1 : 0);
|
||||
audio, seq: made++};
|
||||
db().prepare("INSERT INTO custom_items (kind, id, base, name, cost, members, colors, in_shop, created_by, created_at, owner_id, pending, audio) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
|
||||
.run(kind, id, baseId, entry.name, entry.cost, entry.members ? 1 : 0, entry.colors.join(","), entry.inShop ? 1 : 0, entry.createdBy, entry.createdAt, ownerId, pending ? 1 : 0, audio);
|
||||
items.get(kind).set(id, entry);
|
||||
if (!pending) {
|
||||
apply(entry);
|
||||
@@ -362,6 +379,9 @@ const remove = (kind, id) => {
|
||||
db().prepare("DELETE FROM custom_items WHERE kind = ? AND id = ?").run(kind, entry.id);
|
||||
items.get(kind).delete(entry.id);
|
||||
defpacks.unsetCustomItem(kind, entry.id);
|
||||
if (entry.audio) {
|
||||
audioGone(entry);
|
||||
}
|
||||
return {item: entry};
|
||||
};
|
||||
|
||||
@@ -371,6 +391,10 @@ const list = () => KINDS.flatMap(kind => [...items.get(kind).values()])
|
||||
module.exports = {
|
||||
KINDS,
|
||||
FIRST_ID,
|
||||
audioName,
|
||||
onAudioGone: callback => {
|
||||
audioGone = callback;
|
||||
},
|
||||
load,
|
||||
create,
|
||||
update,
|
||||
|
||||
@@ -0,0 +1,174 @@
|
||||
"use strict";
|
||||
|
||||
const crypto = require("crypto");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const {logText} = require("./logtext.js");
|
||||
const cdn = require("./cdn.js");
|
||||
const config = require("../config.js");
|
||||
const customItems = require("./customitems.js");
|
||||
const extraMusic = require("./extramusic.js");
|
||||
|
||||
// music tracks staff upload in the panels (permission "music.upload",
|
||||
// lib/roles.js): an MP3 made a den music item while the server runs, as the
|
||||
// extra tracks of lib/extramusic.js are. each is a custom den item
|
||||
// (lib/customitems.js) made from the game's "Jam On", with an `audio`
|
||||
// version of its own: games are sent its def row at once, players get it
|
||||
// from the item finder (or the shop, if staff put it on sale), and deleting
|
||||
// it takes it from everyone, as with any custom item.
|
||||
//
|
||||
// a den music item plays "audio/<abbrName>.mp3" from the content server
|
||||
// (DenItemHolder); for these the server answers with the file uploaded, kept
|
||||
// in data/custom-music/<item id>-<version>.mp3 (the data folder is the one
|
||||
// place the server writes to).
|
||||
|
||||
const MAX_BYTES = 15 * 1024 * 1024;
|
||||
const MIN_BYTES = 4 * 1024;
|
||||
const VERSION_LENGTH = 12;
|
||||
const FOLDER = () => path.join(config.dataDir, "custom-music");
|
||||
const fileOf = entry => path.join(FOLDER(), `${entry.id}-${entry.audio}.mp3`);
|
||||
|
||||
// MPEG audio layer III, as the game's Flash plays it: an ID3v2 tag may come
|
||||
// first; then a frame header whose next frame is where its length says.
|
||||
const BITRATES = {
|
||||
1: [0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320],
|
||||
2: [0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160],
|
||||
};
|
||||
const SAMPLE_RATES = {3: [44100, 48000, 32000], 2: [22050, 24000, 16000], 0: [11025, 12000, 8000]};
|
||||
|
||||
// the frame at `at`: {length, sampleRate} or null.
|
||||
const frameAt = (data, at) => {
|
||||
if (at + 4 > data.length || data[at] !== 0xFF || (data[at + 1] & 0xE0) !== 0xE0) {
|
||||
return null;
|
||||
}
|
||||
const version = (data[at + 1] >> 3) & 3; // 3 MPEG-1, 2 MPEG-2, 0 MPEG-2.5
|
||||
const layer = (data[at + 1] >> 1) & 3; // 1 is layer III
|
||||
const bitrateIndex = data[at + 2] >> 4;
|
||||
const rateIndex = (data[at + 2] >> 2) & 3;
|
||||
const padding = (data[at + 2] >> 1) & 1;
|
||||
if (version === 1 || layer !== 1 || bitrateIndex === 0 || bitrateIndex === 15 || rateIndex === 3) {
|
||||
return null;
|
||||
}
|
||||
const bitrate = BITRATES[version === 3 ? 1 : 2][bitrateIndex] * 1000;
|
||||
const sampleRate = SAMPLE_RATES[version][rateIndex];
|
||||
return {length: Math.floor(((version === 3 ? 144 : 72) * bitrate) / sampleRate) + padding, sampleRate};
|
||||
};
|
||||
|
||||
// null for an MP3 the game can play, or what is wrong with it.
|
||||
const problem = data => {
|
||||
if (!Buffer.isBuffer(data) || data.length < MIN_BYTES) {
|
||||
return "That file is too small to be a song.";
|
||||
}
|
||||
if (data.length > MAX_BYTES) {
|
||||
return `That file is too big: ${MAX_BYTES / 1024 / 1024} MB at most.`;
|
||||
}
|
||||
let at = 0;
|
||||
if (data.toString("latin1", 0, 3) === "ID3" && data.length > 10) {
|
||||
at = 10 + (((data[6] & 0x7F) << 21) | ((data[7] & 0x7F) << 14) | ((data[8] & 0x7F) << 7) | (data[9] & 0x7F)) + (data[5] & 0x10 ? 10 : 0);
|
||||
}
|
||||
// the first frame: the header, then two more where their lengths say
|
||||
for (const end = Math.min(data.length, at + 64 * 1024); at < end; at++) {
|
||||
const first = frameAt(data, at);
|
||||
const second = first && frameAt(data, at + first.length);
|
||||
const third = second && frameAt(data, at + first.length + second.length);
|
||||
if (third) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return "That is not an MP3 file (MPEG audio layer III).";
|
||||
};
|
||||
|
||||
const tracks = () => customItems.list().filter(entry => entry.kind === "den" && entry.audio);
|
||||
|
||||
// content path ("1830/audio/<hash>") -> entry, made again when the tracks change.
|
||||
let paths = null;
|
||||
const pathMap = () => {
|
||||
if (!paths) {
|
||||
paths = new Map(tracks().map(entry => [cdn.hashedPath(`audio/${customItems.audioName(entry)}.mp3`), entry]));
|
||||
}
|
||||
return paths;
|
||||
};
|
||||
|
||||
// what to answer a content request with when it is for one of these
|
||||
// tracks: {body, type}; undefined when it is not.
|
||||
const asset = async relPath => {
|
||||
const entry = pathMap().get(relPath);
|
||||
if (!entry) {
|
||||
return undefined;
|
||||
}
|
||||
const body = await fs.promises.readFile(fileOf(entry)).catch(() => null);
|
||||
return {body, type: "audio/mpeg"};
|
||||
};
|
||||
|
||||
// an upload made a track: {item} or {error}. `fields`: {name, cost, inShop, members}.
|
||||
const add = (data, fields, by) => {
|
||||
const wrong = problem(data);
|
||||
if (wrong) {
|
||||
return {error: wrong};
|
||||
}
|
||||
const audio = crypto.createHash("sha256").update(data).digest("hex").slice(0, VERSION_LENGTH);
|
||||
fs.mkdirSync(FOLDER(), {recursive: true, mode: 0o700});
|
||||
// written before the item exists, so no game asks for a file not there yet
|
||||
const waiting = path.join(FOLDER(), `new-${audio}-${process.pid}.mp3`);
|
||||
fs.writeFileSync(waiting, data, {mode: 0o600});
|
||||
const result = customItems.create("den", {name: fields.name, cost: fields.cost, members: fields.members,
|
||||
inShop: fields.inShop === true, base: extraMusic.TEMPLATE, audio}, by);
|
||||
if (result.error) {
|
||||
fs.rmSync(waiting, {force: true});
|
||||
return result;
|
||||
}
|
||||
fs.renameSync(waiting, fileOf(result.item));
|
||||
paths = null;
|
||||
console.log(`[custom music] ${logText(by)} uploaded "${logText(result.item.name)}" (den item ${logText(result.item.id)}, ${logText(data.length)} bytes)`);
|
||||
return result;
|
||||
};
|
||||
|
||||
// the file of a track, for staff to listen to before or after: {data} or null.
|
||||
const file = id => {
|
||||
const entry = tracks().find(track => track.id === Number(id));
|
||||
if (!entry) {
|
||||
return null;
|
||||
}
|
||||
const data = (() => {
|
||||
try {
|
||||
return fs.readFileSync(fileOf(entry));
|
||||
}
|
||||
catch (err) {
|
||||
return null;
|
||||
}
|
||||
})();
|
||||
return data ? {data, entry} : null;
|
||||
};
|
||||
|
||||
// for the panels: newest first
|
||||
const list = () => tracks().map(entry => ({id: entry.id, name: entry.name, cost: entry.cost, inShop: entry.inShop,
|
||||
createdBy: entry.createdBy, createdAt: entry.createdAt,
|
||||
bytes: (() => {
|
||||
try {
|
||||
return fs.statSync(fileOf(entry)).size;
|
||||
}
|
||||
catch (err) {
|
||||
return 0;
|
||||
}
|
||||
})()}));
|
||||
|
||||
// a track's item was deleted (lib/itemcontrol.js deleteCustom): its file goes too.
|
||||
customItems.onAudioGone(entry => {
|
||||
fs.rmSync(fileOf(entry), {force: true});
|
||||
paths = null;
|
||||
});
|
||||
|
||||
// at start: files of tracks no longer there (and of uploads cut short) go.
|
||||
const load = () => {
|
||||
const kept = new Set(tracks().map(entry => path.basename(fileOf(entry))));
|
||||
let gone = 0;
|
||||
for (const name of fs.existsSync(FOLDER()) ? fs.readdirSync(FOLDER()) : []) {
|
||||
if (!kept.has(name)) {
|
||||
fs.rmSync(path.join(FOLDER(), name), {force: true});
|
||||
gone++;
|
||||
}
|
||||
}
|
||||
console.log(`[custom music] ${logText(kept.size)} uploaded tracks${gone ? `, ${logText(gone)} stray files removed` : ""}`);
|
||||
};
|
||||
|
||||
module.exports = {MAX_BYTES, problem, asset, add, file, list, load};
|
||||
+82
-2
@@ -24,6 +24,8 @@ const manage = require("./manage.js");
|
||||
const modpanel = require("./modpanel.js");
|
||||
const badges = require("./badges.js");
|
||||
const extraMusic = require("./extramusic.js");
|
||||
const customMusic = require("./custommusic.js");
|
||||
const itemControl = require("./itemcontrol.js");
|
||||
const masterpieces = require("./masterpieces.js");
|
||||
const panel = require("./panel.js");
|
||||
const totp = require("./totp.js");
|
||||
@@ -230,6 +232,75 @@ const itemArtUpload = async (req, res, url, from, signedIn = null, adminSignedIn
|
||||
send(res, 200, result);
|
||||
};
|
||||
|
||||
// music tracks uploaded in the panels (lib/custommusic.js), at
|
||||
// /mod/api/music (staff with "music.upload") and /admin/api/music:
|
||||
// GET: the tracks; GET ?play=<id>: one's file, to listen to;
|
||||
// POST ?name=&cost=&inShop=1, the MP3 as the body: a new track;
|
||||
// POST ?delete=<id>: the track deleted, and taken from every player.
|
||||
// `signedIn` (req) -> account: the moderator panel's session;
|
||||
// `adminSignedIn` (req) -> boolean: the admin panel's, written down as "admin".
|
||||
const musicUploads = limits.limiter(30, 3600000); // tracks an hour, per staff member
|
||||
const musicRoute = async (req, res, url, from, signedIn = null, adminSignedIn = null) => {
|
||||
res.setHeader("Cache-Control", "no-store");
|
||||
const asAdmin = Boolean(adminSignedIn && adminSignedIn(req));
|
||||
const account = asAdmin ? null : signedIn ? signedIn(req) : null;
|
||||
if (!asAdmin && !(account && roles.can(account, "music.upload"))) {
|
||||
send(res, 403, {error: "Your role cannot upload music."});
|
||||
return;
|
||||
}
|
||||
const by = asAdmin ? "admin" : account.username;
|
||||
if (req.method === "GET" || req.method === "HEAD") {
|
||||
if (url.searchParams.has("play")) {
|
||||
const track = customMusic.file(url.searchParams.get("play"));
|
||||
if (!track) {
|
||||
send(res, 404, "Not found", "text/plain");
|
||||
return;
|
||||
}
|
||||
res.setHeader("Content-Security-Policy", "default-src 'none'; sandbox");
|
||||
res.setHeader("X-Content-Type-Options", "nosniff");
|
||||
send(res, 200, track.data, "audio/mpeg");
|
||||
return;
|
||||
}
|
||||
send(res, 200, {tracks: customMusic.list(), maxBytes: customMusic.MAX_BYTES});
|
||||
return;
|
||||
}
|
||||
if (req.method !== "POST" || !limits.originAllowed(req)) {
|
||||
send(res, 403, {error: "forbidden"});
|
||||
return;
|
||||
}
|
||||
if (url.searchParams.has("delete")) {
|
||||
const entry = customItems.get("den", url.searchParams.get("delete"));
|
||||
if (!entry || !entry.audio) {
|
||||
send(res, 404, {error: "No such uploaded track."});
|
||||
return;
|
||||
}
|
||||
const result = itemControl.deleteCustom("den", entry.id, world);
|
||||
if (result.ok) {
|
||||
modlog.record(by, "music deleted", "", `den ${entry.id} ${entry.name}`);
|
||||
}
|
||||
send(res, result.ok ? 200 : 400, result);
|
||||
return;
|
||||
}
|
||||
if (!asAdmin && !musicUploads.allow(account.id)) {
|
||||
send(res, 429, {error: "Too many uploads. Try again later."});
|
||||
return;
|
||||
}
|
||||
const data = await readBody(req, customMusic.MAX_BYTES + 1).catch(() => null);
|
||||
if (!data) {
|
||||
send(res, 413, {error: `That file is too big: ${customMusic.MAX_BYTES / 1024 / 1024} MB at most.`});
|
||||
return;
|
||||
}
|
||||
const result = customMusic.add(data, {name: url.searchParams.get("name"), cost: url.searchParams.get("cost") || 0,
|
||||
inShop: url.searchParams.get("inShop") === "1"}, by);
|
||||
if (result.error) {
|
||||
send(res, 400, result);
|
||||
return;
|
||||
}
|
||||
modlog.record(by, "music uploaded", "", `den ${result.item.id} ${result.item.name}`);
|
||||
send(res, 200, {ok: `"${result.item.name}" is den item ${result.item.id}. Players can get it from the item finder now${result.item.inShop ? ", and the shop" : ""}.`,
|
||||
item: {id: result.item.id, name: result.item.name}});
|
||||
};
|
||||
|
||||
// a picture still waiting for a moderator, which /itemart/ does not serve:
|
||||
// to its maker (the account page) and to staff who review or make items.
|
||||
const itemArtView = (req, res, url, signedIn, adminSignedIn) => {
|
||||
@@ -731,6 +802,10 @@ const handle = async (req, res) => {
|
||||
await itemArtUpload(req, res, url, from, null, panel.signedIn);
|
||||
return;
|
||||
}
|
||||
if (url.pathname === "/admin/api/music") {
|
||||
await musicRoute(req, res, url, from, null, panel.signedIn);
|
||||
return;
|
||||
}
|
||||
await panel.handle(req, res, url, from, {send, readJson});
|
||||
return;
|
||||
}
|
||||
@@ -757,6 +832,10 @@ const handle = async (req, res) => {
|
||||
await itemArtUpload(req, res, url, from, modpanel.signedInAs);
|
||||
return;
|
||||
}
|
||||
if (isMod && url.pathname === "/mod/api/music") {
|
||||
await musicRoute(req, res, url, from, modpanel.signedInAs);
|
||||
return;
|
||||
}
|
||||
if (isMod) {
|
||||
await modpanel.handle(req, res, url, from, {send, readJson});
|
||||
return;
|
||||
@@ -1043,8 +1122,9 @@ const handle = async (req, res) => {
|
||||
send(res, body ? 200 : 404, body || "Not found", body ? MIME[".swf"] : "text/plain");
|
||||
return;
|
||||
}
|
||||
// the extra den music tracks, which the CDN does not have as files.
|
||||
const extra = await extraMusic.asset(relPath);
|
||||
// the extra den music tracks, which the CDN does not have as files, and
|
||||
// those uploaded by staff.
|
||||
const extra = await extraMusic.asset(relPath) || await customMusic.asset(relPath);
|
||||
if (extra) {
|
||||
send(res, extra.body ? 200 : 404, extra.body || "Not found", extra.body ? extra.type : "text/plain");
|
||||
return;
|
||||
|
||||
+1
-1
@@ -395,7 +395,7 @@ const PAGE_HEADERS = {
|
||||
"X-Frame-Options": "DENY",
|
||||
"X-Content-Type-Options": "nosniff",
|
||||
"Referrer-Policy": "no-referrer",
|
||||
"Content-Security-Policy": captcha.policy("default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; connect-src 'self'; img-src 'self' data:; font-src 'self'; "
|
||||
"Content-Security-Policy": captcha.policy("default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; connect-src 'self'; img-src 'self' data:; media-src 'self'; font-src 'self'; "
|
||||
+ "form-action 'none'; frame-ancestors 'none'; base-uri 'none'"),
|
||||
};
|
||||
|
||||
|
||||
+1
-1
@@ -600,7 +600,7 @@ const PAGE_HEADERS = {
|
||||
"X-Frame-Options": "DENY",
|
||||
"X-Content-Type-Options": "nosniff",
|
||||
"Referrer-Policy": "no-referrer",
|
||||
"Content-Security-Policy": "default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; connect-src 'self'; img-src 'self' data:; font-src 'self'; "
|
||||
"Content-Security-Policy": "default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; connect-src 'self'; img-src 'self' data:; media-src 'self'; font-src 'self'; "
|
||||
+ "form-action 'none'; frame-ancestors 'none'; base-uri 'none'",
|
||||
};
|
||||
|
||||
|
||||
+2
-1
@@ -32,11 +32,12 @@ const PERMISSIONS = {
|
||||
"items.create": "make new items from the game's art, change and delete them, lock items out of the item finder and take items from players (!item, !lock, !unlock, !locked, !take)",
|
||||
"room.effects": "run temporary room-wide size and colour effects (!roomfx)",
|
||||
"theater.control": "play or stop a YouTube video for everyone in the theatre",
|
||||
"music.upload": "upload MP3s as den music tracks, and delete those uploaded (the Music page)",
|
||||
};
|
||||
|
||||
const HELPER = ["server.view", "player.view", "staff.list", "player.warn", "player.goto", "chat.silence"];
|
||||
const MODERATOR = [...HELPER, "player.kick", "player.summon", "staff.invisible", "player.ban", "player.names", "address.ban", "items.approve", "items.restrict", "theater.control"];
|
||||
const ADMIN = [...MODERATOR, "items.create", "room.effects"];
|
||||
const ADMIN = [...MODERATOR, "items.create", "room.effects", "music.upload"];
|
||||
// a developer makes things, and moderates nobody.
|
||||
const DEVELOPER = ["server.view", "staff.list", "items.create", "room.effects", "theater.control"];
|
||||
|
||||
|
||||
@@ -413,6 +413,7 @@
|
||||
<button role="tab" data-tab="masterpieces" aria-selected="false"><span class="icon" aria-hidden="true">▣</span>Masterpieces<span id="masterpiece-count"></span></button>
|
||||
<button role="tab" data-tab="items" aria-selected="false"><span class="icon" aria-hidden="true">❖</span>Items</button>
|
||||
<button role="tab" data-tab="wear-rules" aria-selected="false"><span class="icon" aria-hidden="true">⚑</span>Wear rules</button>
|
||||
<button role="tab" data-tab="music" aria-selected="false"><span class="icon" aria-hidden="true">♫</span>Music</button>
|
||||
<button role="tab" data-tab="bans" aria-selected="false"><span class="icon" aria-hidden="true">⊘</span>Bans</button>
|
||||
<button role="tab" data-tab="moderators" aria-selected="false"><span class="icon" aria-hidden="true">★</span>Moderators</button>
|
||||
<button role="tab" data-tab="messages" aria-selected="false"><span class="icon" aria-hidden="true">✉</span>Messages</button>
|
||||
@@ -735,6 +736,31 @@
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<div data-view="music" hidden>
|
||||
<section>
|
||||
<h2>Upload music</h2>
|
||||
<p class="hint">An MP3 becomes a den music track at once, with no restart: players get it from the item finder (and the shop, if it is on sale) and play it in their dens like the game's own tracks. 44.1 kHz MP3s play best; 15 MB at most.</p>
|
||||
<div class="actions">
|
||||
<div class="action">
|
||||
<h3>The track</h3>
|
||||
<div class="row"><input id="music-file" type="file" accept=".mp3,audio/mpeg" aria-label="MP3 file"></div>
|
||||
<div class="row" style="margin-top:8px"><input id="music-name" maxlength="40" placeholder="Name" aria-label="Name"></div>
|
||||
<div class="row" style="margin-top:8px"><input id="music-cost" type="number" min="0" placeholder="Price in gems (0)" aria-label="Price in gems">
|
||||
<label class="check"><input type="checkbox" id="music-shop"> On sale in the shop</label></div>
|
||||
<div class="row" style="margin-top:8px"><button type="button" class="primary" id="music-upload">Upload</button></div>
|
||||
</div>
|
||||
</div>
|
||||
<p class="status" id="music-status" role="status"></p>
|
||||
</section>
|
||||
<section>
|
||||
<h2>Uploaded tracks</h2>
|
||||
<div class="table-wrap"><table>
|
||||
<thead><tr><th>Track</th><th class="num">Den item</th><th>Uploaded</th><th class="num">Size</th><th></th></tr></thead>
|
||||
<tbody id="music-list"></tbody>
|
||||
</table></div>
|
||||
<div class="empty" id="no-music" hidden>None yet.</div>
|
||||
</section>
|
||||
</div>
|
||||
<div data-view="items" hidden>
|
||||
<section>
|
||||
<h2 id="item-form-title">Make an item</h2>
|
||||
@@ -2853,6 +2879,91 @@
|
||||
setStatus($("control-status"), said(result), !result.ok);
|
||||
});
|
||||
|
||||
// ---- music uploaded by staff (lib/custommusic.js): an MP3 made a den
|
||||
// music track, which players get from the item finder at once
|
||||
const MUSIC = "/admin/api/music";
|
||||
let musicShown = "";
|
||||
const musicCall = async (query, body) => {
|
||||
try {
|
||||
const response = await fetch(`${MUSIC}${query}`, {method: "POST", headers: {"Content-Type": "audio/mpeg"}, body});
|
||||
return await response.json().catch(() => ({error: "That did not work."}));
|
||||
}
|
||||
catch (err) {
|
||||
return {error: "The server did not answer."};
|
||||
}
|
||||
};
|
||||
const renderMusic = data => {
|
||||
const tracks = data.tracks || [];
|
||||
// drawn again only when something changed: a track being listened to keeps playing
|
||||
const shown = JSON.stringify(tracks);
|
||||
if (shown === musicShown) {
|
||||
return;
|
||||
}
|
||||
musicShown = shown;
|
||||
$("no-music").hidden = tracks.length > 0;
|
||||
$("music-list").replaceChildren(...tracks.map(track => {
|
||||
const row = el("tr");
|
||||
const name = el("td");
|
||||
const player = el("audio");
|
||||
player.controls = true;
|
||||
player.preload = "none";
|
||||
player.src = `${MUSIC}?play=${track.id}`;
|
||||
player.style.cssText = "display:block;margin-top:6px;height:32px;max-width:260px";
|
||||
name.append(el("strong", track.name), player);
|
||||
const actions = el("td");
|
||||
actions.append(button("Delete", async () => {
|
||||
if (!confirm(`Delete "${track.name}"? It is taken from every player who has it. This cannot be undone.`)) {
|
||||
return;
|
||||
}
|
||||
const result = await musicCall(`?delete=${track.id}`, new Uint8Array(0));
|
||||
setStatus($("music-status"), result.ok || result.error || "That did not work.", !result.ok);
|
||||
await loadMusic();
|
||||
}, "danger"));
|
||||
row.append(name, el("td", String(track.id), "num"), el("td", `${when(track.createdAt)} · ${track.createdBy}${track.inShop ? ` · on sale, ${track.cost} gems` : ""}`),
|
||||
el("td", `${(track.bytes / 1048576).toFixed(1)} MB`, "num"), actions);
|
||||
return row;
|
||||
}));
|
||||
};
|
||||
const loadMusic = async () => {
|
||||
try {
|
||||
const response = await fetch(MUSIC);
|
||||
const data = await response.json().catch(() => ({}));
|
||||
if (response.ok) {
|
||||
renderMusic(data);
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
// shown again with the next refresh
|
||||
}
|
||||
};
|
||||
// the name from the file's, until one is typed
|
||||
$("music-file").addEventListener("change", () => {
|
||||
const file = $("music-file").files[0];
|
||||
if (file && !$("music-name").value.trim()) {
|
||||
$("music-name").value = file.name.replace(/\.mp3$/i, "").replace(/[_]+/g, " ").slice(0, 40);
|
||||
}
|
||||
});
|
||||
$("music-upload").addEventListener("click", async () => {
|
||||
const file = $("music-file").files[0];
|
||||
if (!file || !$("music-name").value.trim()) {
|
||||
setStatus($("music-status"), file ? "Give the track a name." : "Choose an MP3 first.", true);
|
||||
return;
|
||||
}
|
||||
$("music-upload").disabled = true;
|
||||
setStatus($("music-status"), "Uploading...");
|
||||
const query = `?name=${encodeURIComponent($("music-name").value.trim())}&cost=${encodeURIComponent($("music-cost").value || 0)}&inShop=${$("music-shop").checked ? 1 : 0}`;
|
||||
const result = await musicCall(query, new Uint8Array(await file.arrayBuffer()));
|
||||
$("music-upload").disabled = false;
|
||||
setStatus($("music-status"), result.ok || result.error || "That did not work.", !result.ok);
|
||||
if (result.ok) {
|
||||
for (const id of ["music-file", "music-name", "music-cost"]) {
|
||||
$(id).value = "";
|
||||
}
|
||||
$("music-shop").checked = false;
|
||||
}
|
||||
await loadMusic();
|
||||
});
|
||||
|
||||
const refresh = async () => {
|
||||
clearTimeout(timer);
|
||||
if (!state || !state.signedIn) {
|
||||
@@ -2921,6 +3032,9 @@
|
||||
else if (tab === "wear-rules") {
|
||||
await loadWearRules();
|
||||
}
|
||||
else if (tab === "music") {
|
||||
await loadMusic();
|
||||
}
|
||||
else if (tab === "items") {
|
||||
result = await api("items");
|
||||
if (result.ok) {
|
||||
|
||||
+114
@@ -478,6 +478,7 @@
|
||||
<button role="tab" data-tab="review" data-needs="items.approve" aria-selected="false" hidden><span class="icon" aria-hidden="true">✔</span>Review</button>
|
||||
<button role="tab" data-tab="wear-rules" data-needs="items.restrict" aria-selected="false" hidden><span class="icon" aria-hidden="true">⚑</span>Wear rules</button>
|
||||
<button role="tab" data-tab="items" data-needs="items.create" aria-selected="false" hidden><span class="icon" aria-hidden="true">❖</span>Items</button>
|
||||
<button role="tab" data-tab="music" data-needs="music.upload" aria-selected="false" hidden><span class="icon" aria-hidden="true">♫</span>Music</button>
|
||||
</nav>
|
||||
<button id="sign-out" type="button">Sign out</button>
|
||||
</aside>
|
||||
@@ -897,6 +898,31 @@
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<div data-view="music" hidden>
|
||||
<section>
|
||||
<h2>Upload music</h2>
|
||||
<p class="hint">An MP3 becomes a den music track at once, with no restart: players get it from the item finder (and the shop, if it is on sale) and play it in their dens like the game's own tracks. 44.1 kHz MP3s play best; 15 MB at most.</p>
|
||||
<div class="actions">
|
||||
<div class="action">
|
||||
<h3>The track</h3>
|
||||
<div class="row"><input id="music-file" type="file" accept=".mp3,audio/mpeg" aria-label="MP3 file"></div>
|
||||
<div class="row" style="margin-top:8px"><input id="music-name" maxlength="40" placeholder="Name" aria-label="Name"></div>
|
||||
<div class="row" style="margin-top:8px"><input id="music-cost" type="number" min="0" placeholder="Price in gems (0)" aria-label="Price in gems">
|
||||
<label class="check"><input type="checkbox" id="music-shop"> On sale in the shop</label></div>
|
||||
<div class="row" style="margin-top:8px"><button type="button" class="primary" id="music-upload">Upload</button></div>
|
||||
</div>
|
||||
</div>
|
||||
<p class="status" id="music-status" role="status"></p>
|
||||
</section>
|
||||
<section>
|
||||
<h2>Uploaded tracks</h2>
|
||||
<div class="table-wrap"><table>
|
||||
<thead><tr><th>Track</th><th class="num">Den item</th><th>Uploaded</th><th class="num">Size</th><th></th></tr></thead>
|
||||
<tbody id="music-list"></tbody>
|
||||
</table></div>
|
||||
<div class="empty" id="no-music" hidden>None yet.</div>
|
||||
</section>
|
||||
</div>
|
||||
<div data-view="items" hidden>
|
||||
<section>
|
||||
<h2 id="item-form-title">Make an item</h2>
|
||||
@@ -2733,6 +2759,91 @@
|
||||
$("server-updated").textContent = `Updated ${new Date(Number(data.now || Date.now())).toLocaleTimeString()}.`;
|
||||
};
|
||||
|
||||
// ---- music uploaded by staff (lib/custommusic.js): an MP3 made a den
|
||||
// music track, which players get from the item finder at once
|
||||
const MUSIC = "/mod/api/music";
|
||||
let musicShown = "";
|
||||
const musicCall = async (query, body) => {
|
||||
try {
|
||||
const response = await fetch(`${MUSIC}${query}`, {method: "POST", headers: {"Content-Type": "audio/mpeg"}, body});
|
||||
return await response.json().catch(() => ({error: "That did not work."}));
|
||||
}
|
||||
catch (err) {
|
||||
return {error: "The server did not answer."};
|
||||
}
|
||||
};
|
||||
const renderMusic = data => {
|
||||
const tracks = data.tracks || [];
|
||||
// drawn again only when something changed: a track being listened to keeps playing
|
||||
const shown = JSON.stringify(tracks);
|
||||
if (shown === musicShown) {
|
||||
return;
|
||||
}
|
||||
musicShown = shown;
|
||||
$("no-music").hidden = tracks.length > 0;
|
||||
$("music-list").replaceChildren(...tracks.map(track => {
|
||||
const row = el("tr");
|
||||
const name = el("td");
|
||||
const player = el("audio");
|
||||
player.controls = true;
|
||||
player.preload = "none";
|
||||
player.src = `${MUSIC}?play=${track.id}`;
|
||||
player.style.cssText = "display:block;margin-top:6px;height:32px;max-width:260px";
|
||||
name.append(el("strong", track.name), player);
|
||||
const actions = el("td");
|
||||
actions.append(button("Delete", async () => {
|
||||
if (!confirm(`Delete "${track.name}"? It is taken from every player who has it. This cannot be undone.`)) {
|
||||
return;
|
||||
}
|
||||
const result = await musicCall(`?delete=${track.id}`, new Uint8Array(0));
|
||||
setStatus($("music-status"), result.ok || result.error || "That did not work.", !result.ok);
|
||||
await loadMusic();
|
||||
}, "danger"));
|
||||
row.append(name, el("td", String(track.id), "num"), el("td", `${when(track.createdAt)} · ${track.createdBy}${track.inShop ? ` · on sale, ${track.cost} gems` : ""}`),
|
||||
el("td", `${(track.bytes / 1048576).toFixed(1)} MB`, "num"), actions);
|
||||
return row;
|
||||
}));
|
||||
};
|
||||
const loadMusic = async () => {
|
||||
try {
|
||||
const response = await fetch(MUSIC);
|
||||
const data = await response.json().catch(() => ({}));
|
||||
if (response.ok) {
|
||||
renderMusic(data);
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
// shown again with the next refresh
|
||||
}
|
||||
};
|
||||
// the name from the file's, until one is typed
|
||||
$("music-file").addEventListener("change", () => {
|
||||
const file = $("music-file").files[0];
|
||||
if (file && !$("music-name").value.trim()) {
|
||||
$("music-name").value = file.name.replace(/\.mp3$/i, "").replace(/[_]+/g, " ").slice(0, 40);
|
||||
}
|
||||
});
|
||||
$("music-upload").addEventListener("click", async () => {
|
||||
const file = $("music-file").files[0];
|
||||
if (!file || !$("music-name").value.trim()) {
|
||||
setStatus($("music-status"), file ? "Give the track a name." : "Choose an MP3 first.", true);
|
||||
return;
|
||||
}
|
||||
$("music-upload").disabled = true;
|
||||
setStatus($("music-status"), "Uploading...");
|
||||
const query = `?name=${encodeURIComponent($("music-name").value.trim())}&cost=${encodeURIComponent($("music-cost").value || 0)}&inShop=${$("music-shop").checked ? 1 : 0}`;
|
||||
const result = await musicCall(query, new Uint8Array(await file.arrayBuffer()));
|
||||
$("music-upload").disabled = false;
|
||||
setStatus($("music-status"), result.ok || result.error || "That did not work.", !result.ok);
|
||||
if (result.ok) {
|
||||
for (const id of ["music-file", "music-name", "music-cost"]) {
|
||||
$(id).value = "";
|
||||
}
|
||||
$("music-shop").checked = false;
|
||||
}
|
||||
await loadMusic();
|
||||
});
|
||||
|
||||
const refresh = async () => {
|
||||
clearTimeout(timer);
|
||||
if (!signedIn) {
|
||||
@@ -2868,6 +2979,9 @@
|
||||
else if (shown === "wear-rules") {
|
||||
await loadWearRules();
|
||||
}
|
||||
else if (shown === "music") {
|
||||
await loadMusic();
|
||||
}
|
||||
else if (shown === "items") {
|
||||
const result = await api("items");
|
||||
if (result.ok) {
|
||||
|
||||
@@ -12,6 +12,7 @@ const modlog = require("./lib/modlog.js");
|
||||
const retention = require("./lib/retention.js");
|
||||
const colours = require("./lib/colours.js");
|
||||
const customItems = require("./lib/customitems.js");
|
||||
const customMusic = require("./lib/custommusic.js");
|
||||
const config = require("./config.js");
|
||||
const defpacks = require("./lib/defpacks.js");
|
||||
const http = require("./lib/http.js");
|
||||
@@ -41,6 +42,7 @@ const main = async () => {
|
||||
accounts.startBackups();
|
||||
colours.load();
|
||||
customItems.load();
|
||||
customMusic.load();
|
||||
reports.load();
|
||||
support.load();
|
||||
bans.load();
|
||||
|
||||
Reference in new issue
Block a user