mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-10-02 13:02:49 +08:00
Compare commits
16
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
faf6ac09ec | ||
|
|
8ff51ea6e0 | ||
|
|
3c0e8df763 | ||
|
|
2ea02f54d9 | ||
|
|
a804b2edc1 | ||
|
|
5e9ae49f52 | ||
|
|
1407cadf7b | ||
|
|
9fd1b39092 | ||
|
|
af1dac848d | ||
|
|
9fdc164f98 | ||
|
|
19ad48c4ab | ||
|
|
c79a9a6dee | ||
|
|
ea0d7abce3 | ||
|
|
c7b577adba | ||
|
|
7c3926a2ae | ||
|
|
28b3cd771f |
No files matched your search
@@ -32,7 +32,7 @@
|
|||||||
- **游客模式(免登录点歌,默认关闭)** — 管理员可选择允许访客**无需账号密码**进入 WebUI 点歌,并逐项配置游客权限(8 个开关,默认仅「添加到队列末尾」开启)与可控机器人白名单;游客无法查看 / 修改任何设置、管理机器人或访问用户管理。开启后登录页出现 **「以游客身份进入」**。详见下文 **「游客模式 / Guest mode」** 小节
|
- **游客模式(免登录点歌,默认关闭)** — 管理员可选择允许访客**无需账号密码**进入 WebUI 点歌,并逐项配置游客权限(8 个开关,默认仅「添加到队列末尾」开启)与可控机器人白名单;游客无法查看 / 修改任何设置、管理机器人或访问用户管理。开启后登录页出现 **「以游客身份进入」**。详见下文 **「游客模式 / Guest mode」** 小节
|
||||||
- **本地收藏歌单** — 在首页 / 搜索 / 歌单页一键收藏,收藏内容按用户存储,登录后跨设备同步
|
- **本地收藏歌单** — 在首页 / 搜索 / 歌单页一键收藏,收藏内容按用户存储,登录后跨设备同步
|
||||||
- **保存/加载播放清单 + 重启后自动恢复队列(可选,默认关闭)** — 管理员在 设置 → 行为设置 开启后,可在网页「已存队列」页或聊天命令(`!save` / `!load` / `!queues`)把当前队列保存为清单,随时**替换**加载或**追加**到队列末尾;同时机器人重启后会自动恢复并继续播放上次的队列。网页保存可选「共享」,聊天保存进入共享清单。**说明**:重启只能从当前曲目的开头恢复(不记忆播放进度);Spotify 自动恢复为尽力而为(依赖 sidecar 可用)。详见 [使用说明](#使用说明)
|
- **保存/加载播放清单 + 重启后自动恢复队列(可选,默认关闭)** — 管理员在 设置 → 行为设置 开启后,可在网页「已存队列」页或聊天命令(`!save` / `!load` / `!queues`)把当前队列保存为清单,随时**替换**加载或**追加**到队列末尾;同时机器人重启后会自动恢复并继续播放上次的队列。网页保存可选「共享」,聊天保存进入共享清单。**说明**:重启只能从当前曲目的开头恢复(不记忆播放进度);Spotify 自动恢复为尽力而为(依赖 sidecar 可用)。详见 [使用说明](#使用说明)
|
||||||
- **本地音频上传播放** — 在搜索页拖拽或选择本地音频上传,上传后可直接播放 / 下一首播放 / 加入队列;管理员可在 设置 → 行为设置 开关此功能,播放结束或停止/清空/替换队列时会清理服务端接收的本地文件
|
- **本地音视频上传播放** — 在搜索页拖拽或选择本地文件上传,音频(mp3 / flac / wav / m4a / ogg / opus 等)和视频(mp4 / mov / avi / mkv / flv / wmv 等)都支持,视频上传后只保留其中的音轨;上传后可直接播放 / 下一首播放 / 加入队列;管理员可在 设置 → 行为设置 开关此功能,播放结束或停止/清空/替换队列时会清理服务端接收的本地文件
|
||||||
- **专属链接(单机器人锁定)** — 通过 `/bot/<id>` 专属链接打开 WebUI 时锁定到单个机器人,刷新后保持,适合把某台机器人的控制页分享给特定用户
|
- **专属链接(单机器人锁定)** — 通过 `/bot/<id>` 专属链接打开 WebUI 时锁定到单个机器人,刷新后保持,适合把某台机器人的控制页分享给特定用户
|
||||||
- **频道无人时自动暂停** — 机器人所在频道没有其他人时自动暂停播放,有人加入后自动恢复(**默认关闭**,可在设置中开启)
|
- **频道无人时自动暂停** — 机器人所在频道没有其他人时自动暂停播放,有人加入后自动恢复(**默认关闭**,可在设置中开启)
|
||||||
- **Jellyfin 音源(可选)** — 连接自建 [Jellyfin](https://jellyfin.org/) 服务器作为额外音源:搜索(歌曲 / 专辑 / 歌单)、懒解析直传播放、同步歌词、收藏 Instant Mix 电台(`!fm -j`)、首页「最近添加 / 播放最多 / 收藏 / 流派」,并把播放进度回报给 Jellyfin(PlayCount / 播放状态)。**默认关闭**,在 设置 → Jellyfin 音乐库 一键开启。详见 [可选:Jellyfin 音源](#可选jellyfin-音源)
|
- **Jellyfin 音源(可选)** — 连接自建 [Jellyfin](https://jellyfin.org/) 服务器作为额外音源:搜索(歌曲 / 专辑 / 歌单)、懒解析直传播放、同步歌词、收藏 Instant Mix 电台(`!fm -j`)、首页「最近添加 / 播放最多 / 收藏 / 流派」,并把播放进度回报给 Jellyfin(PlayCount / 播放状态)。**默认关闭**,在 设置 → Jellyfin 音乐库 一键开启。详见 [可选:Jellyfin 音源](#可选jellyfin-音源)
|
||||||
@@ -65,22 +65,22 @@
|
|||||||
先装好 Node.js,其余依赖(含内置 FFmpeg)全部自动安装。
|
先装好 Node.js,其余依赖(含内置 FFmpeg)全部自动安装。
|
||||||
|
|
||||||
```
|
```
|
||||||
1. 安装 Node.js 20 LTS 或 22 LTS(https://nodejs.org/ 或 https://nodejs.cn/)
|
1. 安装 Node.js 22 LTS(https://nodejs.org/ 或 https://nodejs.cn/)
|
||||||
2. 下载或 clone 本项目
|
2. 下载或 clone 本项目
|
||||||
3. 双击 scripts\setup.bat (安装依赖并构建,不含 Node.js 本身)
|
3. 双击 scripts\setup.bat (安装依赖并构建,不含 Node.js 本身)
|
||||||
4. 双击 scripts\start.bat (启动机器人)
|
4. 双击 scripts\start.bat (启动机器人)
|
||||||
5. 浏览器打开 http://localhost:3000
|
5. 浏览器打开 http://localhost:3000
|
||||||
```
|
```
|
||||||
|
|
||||||
> **先装 Node.js 20 LTS 或 22 LTS**([nodejs.org](https://nodejs.org/) / 国内镜像 [nodejs.cn](https://nodejs.cn/))。`setup.bat` 检测到没装 Node 时会给出下载地址并退出,不会替你安装。
|
> **先装 Node.js 22 LTS**([nodejs.org](https://nodejs.org/) / 国内镜像 [nodejs.cn](https://nodejs.cn/))。`setup.bat` 检测到没装 Node 时会给出下载地址并退出,不会替你安装。
|
||||||
>
|
>
|
||||||
> 之后 `setup.bat` 会运行 `npm install` 安装所有依赖(包括内置 FFmpeg),按当前 Node 版本准备好原生模块,最后构建项目。之后每次只需双击 `start.bat` 启动。
|
> 之后 `setup.bat` 会运行 `npm install` 安装所有依赖(包括内置 FFmpeg),按当前 Node 版本准备好原生模块,最后构建项目。之后每次只需双击 `start.bat` 启动。
|
||||||
>
|
>
|
||||||
> 更新的 Node 大版本(如 24)也能用,但通常没有现成的 opus / better-sqlite3 预编译包,安装脚本会改用源码编译,需要 C/C++ 构建工具且耗时更久——所以推荐 20 / 22 LTS。**装好之后不要再换 Node 大版本**:原生模块只能在编译它的那个版本上加载,换版本后必须重新运行 `setup.bat`(脚本会自动检测并重装,见下方常见问题)。
|
> **Node 20 已不再支持**:better-sqlite3 从 12.10.0 起不再发布它那个 ABI(115)的预编译包,装起来必须先备好 Python + C++ 构建工具([#152](https://github.com/ZHANGTIANYAO1/teamspeak-music-bot/issues/152))。Node 24 及更新的大版本能用,但 @discordjs/opus 0.10.0 同样没有 Node 24(ABI 137)的预编译包,安装脚本会改用源码编译,需要构建工具且耗时更久——所以推荐 22 LTS。**装好之后不要再换 Node 大版本**:原生模块只能在编译它的那个版本上加载,换版本后必须重新运行 `setup.bat`(脚本会自动检测并重装,见下方常见问题)。
|
||||||
|
|
||||||
### 方式二:手动安装(所有系统)
|
### 方式二:手动安装(所有系统)
|
||||||
|
|
||||||
**前置条件:** [Node.js 20 LTS 或 22 LTS](https://nodejs.org/)(推荐;更新的大版本可用但需要源码编译原生模块)和一个 TeamSpeak 服务器(TS3/TS5/TS6 均可)。
|
**前置条件:** [Node.js 22 LTS](https://nodejs.org/)(Node 24 及更新版本也能用,但需要源码编译原生模块;Node 20 已不再支持)和一个 TeamSpeak 服务器(TS3/TS5/TS6 均可)。
|
||||||
FFmpeg **已自动内置**,无需手动安装。
|
FFmpeg **已自动内置**,无需手动安装。
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -510,7 +510,7 @@ teamspeak-music-bot/
|
|||||||
|
|
||||||
| 层级 | 技术 |
|
| 层级 | 技术 |
|
||||||
|------|------|
|
|------|------|
|
||||||
| **运行时** | Node.js 20 / 22 LTS, TypeScript 5 |
|
| **运行时** | Node.js 22 LTS(推荐), TypeScript 5 |
|
||||||
| **后端框架** | Express 4, WebSocket (ws) |
|
| **后端框架** | Express 4, WebSocket (ws) |
|
||||||
| **数据库** | better-sqlite3 (SQLite) |
|
| **数据库** | better-sqlite3 (SQLite) |
|
||||||
| **音频处理** | FFmpeg (ffmpeg-static 内置), @discordjs/opus |
|
| **音频处理** | FFmpeg (ffmpeg-static 内置), @discordjs/opus |
|
||||||
@@ -884,7 +884,26 @@ A:本项目内置 `/login` 限流(每 IP 每分钟 5 次),但生产部
|
|||||||
|
|
||||||
> 完整历史请查看 [git log](https://github.com/ZHANGTIANYAO1/teamspeak-music-bot/commits/main) 或 [Releases](https://github.com/ZHANGTIANYAO1/teamspeak-music-bot/releases)。这里只列出重要变更和面向用户的破坏性改动。
|
> 完整历史请查看 [git log](https://github.com/ZHANGTIANYAO1/teamspeak-music-bot/commits/main) 或 [Releases](https://github.com/ZHANGTIANYAO1/teamspeak-music-bot/releases)。这里只列出重要变更和面向用户的破坏性改动。
|
||||||
|
|
||||||
### 最新版本 — v1.12.0:网站图标 / 移动端交互 / 安装脚本按 ABI 自愈
|
### 最新版本 — v1.13.0:本地视频上传播放 / 头像上传时机
|
||||||
|
|
||||||
|
处理了 2 个社区反馈的 issue。**没有配置变化,升级无需任何操作**;原有的本地音频上传行为完全不变。
|
||||||
|
|
||||||
|
**本地视频上传播放([#149](https://github.com/ZHANGTIANYAO1/teamspeak-music-bot/issues/149),[PR #151](https://github.com/ZHANGTIANYAO1/teamspeak-music-bot/pull/151),感谢 [@LadenceE](https://github.com/LadenceE))**
|
||||||
|
|
||||||
|
- 搜索页的本地上传现在也收**视频文件**:mp4 / mov / avi / mkv / flv / wmv / m4v / mpg / mpeg / 3gp / ts / m2ts / ogv。上传后当普通歌曲用——直接播放、下一首播放、加入队列都一样。
|
||||||
|
- 视频**只保留音轨**:上传后立刻把音频流原样搬进一个音频容器(不重编码、无损),再删掉原视频。720p 素材实测落盘只剩原文件的 14%,不然十几个视频就把 5 GiB 的上传目录配额占满了。
|
||||||
|
- 没有音轨的视频会在**上传时**就被拒绝并说明原因,而不是排进队列后静默跳过。
|
||||||
|
- 单文件上限从 200 MB 提到 **500 MB**;超限时的报错从 Express 默认的 HTML 错误页(带堆栈和服务器绝对路径)换成正常的中文提示,浏览器端也会在开传前就拦下超大文件。
|
||||||
|
- 上传进度按文件显示百分比,传完切到「服务端处理中」——视频比音频大得多,原先那句静止的「正在上传」看着像卡死。
|
||||||
|
- 说明:这里做的是「把你本地磁盘上的文件传上来播放」。让机器人直接读取**服务器**磁盘上任意路径的文件没有做——那等于开一个全盘任意文件读取的口子,而「播放服务器上已有的媒体库」用 Jellyfin 音源即可。
|
||||||
|
|
||||||
|
**初始化阶段不再发起注定失败的头像上传([#148](https://github.com/ZHANGTIANYAO1/teamspeak-music-bot/issues/148),[PR #150](https://github.com/ZHANGTIANYAO1/teamspeak-music-bot/pull/150),感谢 [@shenmu-rua](https://github.com/shenmu-rua))**
|
||||||
|
|
||||||
|
- 机器人构造阶段读出已保存的自定义头像后会立刻发起文件传输,但那时 TeamSpeak 还没连上,这次传输必定失败。现在构造阶段只把头像数据装入内存,实际上传交给连接成功后的 `onConnect()`。
|
||||||
|
- **影响范围说明**:头像本身一直是能正常显示的(连接成功后本来就会重新应用一次),所以这不是「头像丢了」。真正的代价是每次启动 / 重启都会多一次注定失败的请求和一条 `Profile update failed` 警告日志——现在没有了。
|
||||||
|
- 顺带修掉一个边角:头像文件写到一半崩溃会留下 0 字节文件,原先这会再触发两个同样注定失败的请求。
|
||||||
|
|
||||||
|
### v1.12.0:网站图标 / 移动端交互 / 安装脚本按 ABI 自愈
|
||||||
|
|
||||||
一次性处理了 6 个社区反馈的 issue。**没有配置变化,升级无需任何操作**;`!play id:<id>` 等旧写法全部继续可用。
|
一次性处理了 6 个社区反馈的 issue。**没有配置变化,升级无需任何操作**;`!play id:<id>` 等旧写法全部继续可用。
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -4,7 +4,7 @@
|
|||||||
"description": "TeamSpeak music bot with NetEase Cloud Music and QQ Music support",
|
"description": "TeamSpeak music bot with NetEase Cloud Music and QQ Music support",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": "^20.19.0 || ^22.12.0 || >=24.0.0"
|
"node": "^22.12.0 || >=24.0.0"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "tsx watch src/index.ts",
|
"dev": "tsx watch src/index.ts",
|
||||||
|
|||||||
@@ -0,0 +1,591 @@
|
|||||||
|
diff --git a/src/music/netease.test.ts b/src/music/netease.test.ts
|
||||||
|
index b2adb3d..ecc2202 100644
|
||||||
|
--- a/src/music/netease.test.ts
|
||||||
|
+++ b/src/music/netease.test.ts
|
||||||
|
@@ -1,5 +1,5 @@
|
||||||
|
import { describe, it, expect } from "vitest";
|
||||||
|
-import { parseLyrics } from "./netease.js";
|
||||||
|
+import { parseLyrics, mapNeteaseAlbums } from "./netease.js";
|
||||||
|
|
||||||
|
describe("NetEase adapter", () => {
|
||||||
|
it("parses LRC format lyrics", () => {
|
||||||
|
@@ -28,4 +28,32 @@ describe("NetEase adapter", () => {
|
||||||
|
expect(lines[0].text).toBe("Hello world");
|
||||||
|
expect(lines[0].translation).toBe("你好世界");
|
||||||
|
});
|
||||||
|
+
|
||||||
|
+ it("mapNeteaseAlbums maps raw cloudsearch albums to Album shape", () => {
|
||||||
|
+ const raw = [
|
||||||
|
+ {
|
||||||
|
+ id: 42,
|
||||||
|
+ name: "Album A",
|
||||||
|
+ picUrl: "https://x/p.jpg",
|
||||||
|
+ artists: [{ name: "Artist X" }, { name: "Featured Y" }],
|
||||||
|
+ size: 12,
|
||||||
|
+ },
|
||||||
|
+ {
|
||||||
|
+ id: 99,
|
||||||
|
+ name: "Album B",
|
||||||
|
+ picUrl: "",
|
||||||
|
+ artists: [],
|
||||||
|
+ },
|
||||||
|
+ ];
|
||||||
|
+ expect(mapNeteaseAlbums(raw)).toEqual([
|
||||||
|
+ { id: "42", name: "Album A", artist: "Artist X / Featured Y", coverUrl: "https://x/p.jpg", songCount: 12, platform: "netease" },
|
||||||
|
+ { id: "99", name: "Album B", artist: "", coverUrl: "", songCount: 0, platform: "netease" },
|
||||||
|
+ ]);
|
||||||
|
+ });
|
||||||
|
+
|
||||||
|
+ it("mapNeteaseAlbums returns [] for empty/null input", () => {
|
||||||
|
+ expect(mapNeteaseAlbums([])).toEqual([]);
|
||||||
|
+ expect(mapNeteaseAlbums(null as any)).toEqual([]);
|
||||||
|
+ expect(mapNeteaseAlbums(undefined as any)).toEqual([]);
|
||||||
|
+ });
|
||||||
|
});
|
||||||
|
diff --git a/src/music/netease.ts b/src/music/netease.ts
|
||||||
|
index 8aaeab6..4a863db 100644
|
||||||
|
--- a/src/music/netease.ts
|
||||||
|
+++ b/src/music/netease.ts
|
||||||
|
@@ -8,6 +8,7 @@ import type {
|
||||||
|
SearchResult,
|
||||||
|
QrCodeResult,
|
||||||
|
AuthStatus,
|
||||||
|
+ Album,
|
||||||
|
} from "./provider.js";
|
||||||
|
|
||||||
|
export function parseLyrics(lrc: string, tlyric?: string): LyricLine[] {
|
||||||
|
@@ -55,6 +56,18 @@ export function parseLyrics(lrc: string, tlyric?: string): LyricLine[] {
|
||||||
|
return lines.sort((a, b) => a.time - b.time);
|
||||||
|
}
|
||||||
|
|
||||||
|
+export function mapNeteaseAlbums(raw: any[] | null | undefined): Album[] {
|
||||||
|
+ if (!Array.isArray(raw)) return [];
|
||||||
|
+ return raw.map((a) => ({
|
||||||
|
+ id: String(a.id),
|
||||||
|
+ name: a.name ?? "",
|
||||||
|
+ artist: (a.artists ?? []).map((x: any) => x.name).join(" / "),
|
||||||
|
+ coverUrl: a.picUrl ?? "",
|
||||||
|
+ songCount: a.size ?? 0,
|
||||||
|
+ platform: "netease",
|
||||||
|
+ }));
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
// NetEase quality levels: standard(128k) higher(192k) exhigh(320k) lossless(flac) hires(hi-res) jyeffect jymaster
|
||||||
|
export const NETEASE_QUALITY_LEVELS = [
|
||||||
|
{ value: "standard", label: "标准 (128kbps)", bitrate: 128 },
|
||||||
|
@@ -91,7 +104,7 @@ export class NeteaseProvider implements MusicProvider {
|
||||||
|
}
|
||||||
|
|
||||||
|
async search(query: string, limit = 20): Promise<SearchResult> {
|
||||||
|
- const [songRes, playlistRes] = await Promise.all([
|
||||||
|
+ const [songRes, playlistRes, albumRes] = await Promise.all([
|
||||||
|
this.api.get("/cloudsearch", {
|
||||||
|
params: { keywords: query, type: 1, limit, ...this.cookieParams },
|
||||||
|
}),
|
||||||
|
@@ -103,6 +116,9 @@ export class NeteaseProvider implements MusicProvider {
|
||||||
|
...this.cookieParams,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
+ this.api.get("/cloudsearch", {
|
||||||
|
+ params: { keywords: query, type: 10, limit: 5, ...this.cookieParams },
|
||||||
|
+ }),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const songs: Song[] = (songRes.data?.result?.songs ?? []).map(
|
||||||
|
@@ -127,7 +143,9 @@ export class NeteaseProvider implements MusicProvider {
|
||||||
|
platform: "netease",
|
||||||
|
}));
|
||||||
|
|
||||||
|
- return { songs, playlists, albums: [] };
|
||||||
|
+ const albums = mapNeteaseAlbums(albumRes.data?.result?.albums);
|
||||||
|
+
|
||||||
|
+ return { songs, playlists, albums };
|
||||||
|
}
|
||||||
|
|
||||||
|
async getSongUrl(songId: string, quality?: string): Promise<string | null> {
|
||||||
|
diff --git a/src/music/qq.test.ts b/src/music/qq.test.ts
|
||||||
|
new file mode 100644
|
||||||
|
index 0000000..4f606cf
|
||||||
|
--- /dev/null
|
||||||
|
+++ b/src/music/qq.test.ts
|
||||||
|
@@ -0,0 +1,43 @@
|
||||||
|
+import { describe, it, expect } from "vitest";
|
||||||
|
+import { mapQqAlbums } from "./qq.js";
|
||||||
|
+
|
||||||
|
+describe("QQ adapter", () => {
|
||||||
|
+ it("mapQqAlbums maps albumMID-style raw entries", () => {
|
||||||
|
+ const raw = [
|
||||||
|
+ {
|
||||||
|
+ albumMID: "abc",
|
||||||
|
+ albumName: "Aero",
|
||||||
|
+ singerName: "Singer A",
|
||||||
|
+ },
|
||||||
|
+ {
|
||||||
|
+ albumMID: "xyz",
|
||||||
|
+ albumName: "Beta",
|
||||||
|
+ singer: [{ name: "Singer B" }, { name: "Singer C" }],
|
||||||
|
+ },
|
||||||
|
+ ];
|
||||||
|
+ const out = mapQqAlbums(raw);
|
||||||
|
+ expect(out).toHaveLength(2);
|
||||||
|
+ expect(out[0]).toMatchObject({
|
||||||
|
+ id: "abc",
|
||||||
|
+ name: "Aero",
|
||||||
|
+ artist: "Singer A",
|
||||||
|
+ platform: "qq",
|
||||||
|
+ });
|
||||||
|
+ expect(out[0].coverUrl).toContain("T002R300x300M000abc.jpg");
|
||||||
|
+ expect(out[1].artist).toBe("Singer B / Singer C");
|
||||||
|
+ expect(out[1].coverUrl).toContain("xyz");
|
||||||
|
+ });
|
||||||
|
+
|
||||||
|
+ it("mapQqAlbums returns [] for empty/null input", () => {
|
||||||
|
+ expect(mapQqAlbums([])).toEqual([]);
|
||||||
|
+ expect(mapQqAlbums(null as any)).toEqual([]);
|
||||||
|
+ expect(mapQqAlbums(undefined as any)).toEqual([]);
|
||||||
|
+ });
|
||||||
|
+
|
||||||
|
+ it("mapQqAlbums falls back to albumPic when no albumMID", () => {
|
||||||
|
+ const raw = [{ albumName: "C", albumPic: "https://x/p.jpg", singerName: "S" }];
|
||||||
|
+ const out = mapQqAlbums(raw);
|
||||||
|
+ expect(out[0].coverUrl).toBe("https://x/p.jpg");
|
||||||
|
+ expect(out[0].id).toBe("");
|
||||||
|
+ });
|
||||||
|
+});
|
||||||
|
diff --git a/src/music/qq.ts b/src/music/qq.ts
|
||||||
|
index 9c0360d..1e7a8ae 100644
|
||||||
|
--- a/src/music/qq.ts
|
||||||
|
+++ b/src/music/qq.ts
|
||||||
|
@@ -8,6 +8,7 @@ import type {
|
||||||
|
SearchResult,
|
||||||
|
QrCodeResult,
|
||||||
|
AuthStatus,
|
||||||
|
+ Album,
|
||||||
|
} from "./provider.js";
|
||||||
|
import { parseLyrics } from "./netease.js";
|
||||||
|
|
||||||
|
@@ -27,6 +28,26 @@ const qqFavApi = axios.create({
|
||||||
|
headers: { referer: "https://y.qq.com/" },
|
||||||
|
});
|
||||||
|
|
||||||
|
+export function mapQqAlbums(raw: any[] | null | undefined): Album[] {
|
||||||
|
+ if (!Array.isArray(raw)) return [];
|
||||||
|
+ return raw.map((a) => {
|
||||||
|
+ const id = String(a.albumMID ?? a.mid ?? a.albumID ?? "");
|
||||||
|
+ const artist = a.singerName
|
||||||
|
+ ?? (Array.isArray(a.singer) ? a.singer.map((s: any) => s.name).join(" / ") : "");
|
||||||
|
+ const coverUrl = id
|
||||||
|
+ ? `https://y.gtimg.cn/music/photo_new/T002R300x300M000${id}.jpg`
|
||||||
|
+ : (a.albumPic ?? "");
|
||||||
|
+ return {
|
||||||
|
+ id,
|
||||||
|
+ name: a.albumName ?? a.title ?? "",
|
||||||
|
+ artist,
|
||||||
|
+ coverUrl,
|
||||||
|
+ songCount: a.song_count ?? a.songCount ?? 0,
|
||||||
|
+ platform: "qq" as const,
|
||||||
|
+ };
|
||||||
|
+ });
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
function computeGtk(pSkey: string): number {
|
||||||
|
let hash = 5381;
|
||||||
|
for (let i = 0; i < pSkey.length; i++) {
|
||||||
|
@@ -65,11 +86,12 @@ export class QQMusicProvider implements MusicProvider {
|
||||||
|
req_0: {
|
||||||
|
module: "music.search.SearchCgiService",
|
||||||
|
method: "DoSearchForQQMusicDesktop",
|
||||||
|
- param: {
|
||||||
|
- searchid: "1",
|
||||||
|
- query,
|
||||||
|
- num_per_page: Math.min(limit, 50),
|
||||||
|
- },
|
||||||
|
+ param: { searchid: "1", query, num_per_page: Math.min(limit, 50), search_type: 0 },
|
||||||
|
+ },
|
||||||
|
+ req_album: {
|
||||||
|
+ module: "music.search.SearchCgiService",
|
||||||
|
+ method: "DoSearchForQQMusicDesktop",
|
||||||
|
+ param: { searchid: "1", query, num_per_page: 5, search_type: 8 },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const res = await qqDirectApi.get("/cgi-bin/musicu.fcg", {
|
||||||
|
@@ -90,7 +112,10 @@ export class QQMusicProvider implements MusicProvider {
|
||||||
|
platform: "qq",
|
||||||
|
}));
|
||||||
|
|
||||||
|
- return { songs, playlists: [], albums: [] };
|
||||||
|
+ const albumList: any[] = res.data?.req_album?.data?.body?.album?.list ?? [];
|
||||||
|
+ const albums = mapQqAlbums(albumList);
|
||||||
|
+
|
||||||
|
+ return { songs, playlists: [], albums };
|
||||||
|
}
|
||||||
|
|
||||||
|
async getSongUrl(songId: string, quality?: string): Promise<string | null> {
|
||||||
|
diff --git a/src/web/api/music.ts b/src/web/api/music.ts
|
||||||
|
index b08f9a2..edf9c04 100644
|
||||||
|
--- a/src/web/api/music.ts
|
||||||
|
+++ b/src/web/api/music.ts
|
||||||
|
@@ -52,14 +52,20 @@ export function createMusicRouter(
|
||||||
|
]);
|
||||||
|
|
||||||
|
const songs = [
|
||||||
|
- ...(neteaseResult.status === "fulfilled"
|
||||||
|
- ? neteaseResult.value.songs
|
||||||
|
- : []),
|
||||||
|
+ ...(neteaseResult.status === "fulfilled" ? neteaseResult.value.songs : []),
|
||||||
|
...(qqResult.status === "fulfilled" ? qqResult.value.songs : []),
|
||||||
|
...(bilibiliResult.status === "fulfilled" ? bilibiliResult.value.songs : []),
|
||||||
|
];
|
||||||
|
+ const albums = [
|
||||||
|
+ ...(neteaseResult.status === "fulfilled" ? neteaseResult.value.albums : []),
|
||||||
|
+ ...(qqResult.status === "fulfilled" ? qqResult.value.albums : []),
|
||||||
|
+ ];
|
||||||
|
+ const playlists = [
|
||||||
|
+ ...(neteaseResult.status === "fulfilled" ? neteaseResult.value.playlists : []),
|
||||||
|
+ ...(qqResult.status === "fulfilled" ? qqResult.value.playlists : []),
|
||||||
|
+ ];
|
||||||
|
|
||||||
|
- res.json({ songs });
|
||||||
|
+ res.json({ songs, albums, playlists });
|
||||||
|
} catch (err) {
|
||||||
|
logger.error({ err }, "Unified search failed");
|
||||||
|
res.status(500).json({ error: (err as Error).message });
|
||||||
|
diff --git a/src/web/api/player.ts b/src/web/api/player.ts
|
||||||
|
index a9af600..4f0930b 100644
|
||||||
|
--- a/src/web/api/player.ts
|
||||||
|
+++ b/src/web/api/player.ts
|
||||||
|
@@ -313,6 +313,78 @@ export function createPlayerRouter(
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
+ // Play an album by ID — mirrors play-playlist but calls getAlbumSongs
|
||||||
|
+ router.post("/:botId/play-album", async (req, res) => {
|
||||||
|
+ try {
|
||||||
|
+ const bot = (req as any).bot;
|
||||||
|
+ const { albumId, platform } = req.body;
|
||||||
|
+ const provider = bot.getProviderFor(
|
||||||
|
+ platform === "bilibili" || platform === "qq" || platform === "youtube"
|
||||||
|
+ ? platform
|
||||||
|
+ : "netease"
|
||||||
|
+ );
|
||||||
|
+
|
||||||
|
+ // Stop current playback
|
||||||
|
+ bot.getPlayer().stop();
|
||||||
|
+ bot.getPlayer().resetFailures();
|
||||||
|
+
|
||||||
|
+ const songs = await provider.getAlbumSongs(albumId);
|
||||||
|
+ if (songs.length === 0) {
|
||||||
|
+ res.json({ message: "Album is empty" });
|
||||||
|
+ return;
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
+ // QQ-specific optimization: batch-resolve playable IDs to avoid
|
||||||
|
+ // wasting retries on region/copyright-restricted tracks.
|
||||||
|
+ let queueable: { id: string }[] = songs;
|
||||||
|
+ const totalCount = songs.length;
|
||||||
|
+ const qqLike = provider as { getPlayableSongIds?: (ids: string[]) => Promise<Set<string> | null> };
|
||||||
|
+ if (typeof qqLike.getPlayableSongIds === "function") {
|
||||||
|
+ const playable = await qqLike.getPlayableSongIds(songs.map((s: { id: string }) => s.id));
|
||||||
|
+ if (playable !== null) {
|
||||||
|
+ queueable = songs.filter((s: { id: string }) => playable.has(s.id));
|
||||||
|
+ }
|
||||||
|
+ }
|
||||||
|
+ if (queueable.length === 0) {
|
||||||
|
+ res.json({ ok: false, message: `专辑 ${totalCount} 首歌曲均无版权可播放(区域/版权限制)` });
|
||||||
|
+ return;
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
+ const queue = bot.getQueueManager();
|
||||||
|
+ queue.clear();
|
||||||
|
+ for (const song of queueable) {
|
||||||
|
+ queue.add({ ...song, platform: provider.platform });
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
+ const mode = queue.getMode();
|
||||||
|
+ let first;
|
||||||
|
+ if (mode === "random" || mode === "rloop") {
|
||||||
|
+ const idx = Math.floor(Math.random() * queue.size());
|
||||||
|
+ first = queue.playAt(idx);
|
||||||
|
+ } else {
|
||||||
|
+ first = queue.play();
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
+ let started = first ? await bot.resolveAndPlay(first) : false;
|
||||||
|
+ if (first && !started) {
|
||||||
|
+ started = await bot.playNext(20);
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
+ const playing = queue.current();
|
||||||
|
+ const loadedMsg = queueable.length < totalCount
|
||||||
|
+ ? `已加载 ${queueable.length}/${totalCount} 首(其余区域/版权限制)`
|
||||||
|
+ : `已加载 ${queueable.length} 首`;
|
||||||
|
+ if (started && playing) {
|
||||||
|
+ res.json({ ok: true, message: `${loadedMsg},正在播放:${playing.name}` });
|
||||||
|
+ } else {
|
||||||
|
+ res.json({ ok: false, message: `${loadedMsg},但无法开始播放。` });
|
||||||
|
+ }
|
||||||
|
+ } catch (err) {
|
||||||
|
+ logger.error({ err }, "play-album failed");
|
||||||
|
+ res.status(500).json({ error: (err as Error).message });
|
||||||
|
+ }
|
||||||
|
+ });
|
||||||
|
+
|
||||||
|
// Play a single song by ID — resolves URL on demand
|
||||||
|
router.post("/:botId/play-song", async (req, res) => {
|
||||||
|
try {
|
||||||
|
diff --git a/web/src/router/index.ts b/web/src/router/index.ts
|
||||||
|
index cc060f5..d62afcd 100644
|
||||||
|
--- a/web/src/router/index.ts
|
||||||
|
+++ b/web/src/router/index.ts
|
||||||
|
@@ -22,6 +22,13 @@ const router = createRouter({
|
||||||
|
path: '/playlist/:id',
|
||||||
|
name: 'playlist',
|
||||||
|
component: () => import('../views/Playlist.vue'),
|
||||||
|
+ meta: { kind: 'playlist' },
|
||||||
|
+ },
|
||||||
|
+ {
|
||||||
|
+ path: '/album/:id',
|
||||||
|
+ name: 'album',
|
||||||
|
+ component: () => import('../views/Playlist.vue'),
|
||||||
|
+ meta: { kind: 'album' },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: '/lyrics',
|
||||||
|
diff --git a/web/src/stores/player.ts b/web/src/stores/player.ts
|
||||||
|
index 9bc817d..083262c 100644
|
||||||
|
--- a/web/src/stores/player.ts
|
||||||
|
+++ b/web/src/stores/player.ts
|
||||||
|
@@ -322,6 +322,16 @@ export const usePlayerStore = defineStore('player', {
|
||||||
|
this._syncAfterAction();
|
||||||
|
},
|
||||||
|
|
||||||
|
+ async playAlbum(albumId: string, platform = 'netease') {
|
||||||
|
+ if (!this.activeBotId) return;
|
||||||
|
+ const res = await axios.post(`/api/player/${this.activeBotId}/play-album`, { albumId, platform });
|
||||||
|
+ if (res.data?.message) {
|
||||||
|
+ this.notify(res.data.message, res.data.ok === false ? 'error' : 'info');
|
||||||
|
+ }
|
||||||
|
+ this._setTiming(this.activeBotId, { serverElapsed: 0 });
|
||||||
|
+ this._syncAfterAction();
|
||||||
|
+ },
|
||||||
|
+
|
||||||
|
async pause() {
|
||||||
|
if (!this.activeBotId) return;
|
||||||
|
// Freeze elapsed at current interpolated value
|
||||||
|
diff --git a/web/src/views/Playlist.vue b/web/src/views/Playlist.vue
|
||||||
|
index d5c9f8d..d00101d 100644
|
||||||
|
--- a/web/src/views/Playlist.vue
|
||||||
|
+++ b/web/src/views/Playlist.vue
|
||||||
|
@@ -38,7 +38,7 @@
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
- <div v-else class="loading">歌单不存在或加载失败</div>
|
||||||
|
+ <div v-else class="loading">{{ kind === 'album' ? '专辑' : '歌单' }}不存在或加载失败</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
@@ -64,6 +64,8 @@ interface PlaylistDetail {
|
||||||
|
songCount: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
+const kind = (route.meta.kind as string) ?? 'playlist'; // 'playlist' | 'album'
|
||||||
|
+
|
||||||
|
const playlist = ref<PlaylistDetail | null>(null);
|
||||||
|
const songs = ref<Song[]>([]);
|
||||||
|
const loading = ref(true);
|
||||||
|
@@ -71,20 +73,32 @@ const loading = ref(true);
|
||||||
|
async function playAll() {
|
||||||
|
const id = route.params.id as string;
|
||||||
|
const platform = (route.query.platform as string) || 'netease';
|
||||||
|
- await store.playPlaylist(id, platform);
|
||||||
|
+ if (kind === 'album') {
|
||||||
|
+ await store.playAlbum(id, platform);
|
||||||
|
+ } else {
|
||||||
|
+ await store.playPlaylist(id, platform);
|
||||||
|
+ }
|
||||||
|
}
|
||||||
|
|
||||||
|
onMounted(async () => {
|
||||||
|
const id = route.params.id as string;
|
||||||
|
const platform = (route.query.platform as string) || 'netease';
|
||||||
|
|
||||||
|
+ const detailUrl = kind === 'album'
|
||||||
|
+ ? `/api/music/album/${id}/detail`
|
||||||
|
+ : `/api/music/playlist/${id}/detail`;
|
||||||
|
+ const songsUrl = kind === 'album'
|
||||||
|
+ ? `/api/music/album/${id}`
|
||||||
|
+ : `/api/music/playlist/${id}`;
|
||||||
|
+
|
||||||
|
// allSettled, not Promise.all — if detail 404s but songs is fine
|
||||||
|
// (e.g., a QQ playlist whose detail endpoint flaked but the song
|
||||||
|
// list resolved), we still want to show the songs rather than
|
||||||
|
- // the "歌单不存在" empty state.
|
||||||
|
+ // the "不存在" empty state. For albums, detail always 404s — that
|
||||||
|
+ // is intentional; the fallback stub below handles it.
|
||||||
|
const [detailRes, songsRes] = await Promise.allSettled([
|
||||||
|
- axios.get(`/api/music/playlist/${id}/detail`, { params: { platform } }),
|
||||||
|
- axios.get(`/api/music/playlist/${id}`, { params: { platform } }),
|
||||||
|
+ axios.get(detailUrl, { params: { platform } }),
|
||||||
|
+ axios.get(songsUrl, { params: { platform } }),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const detail = detailRes.status === 'fulfilled' ? detailRes.value.data?.playlist : null;
|
||||||
|
@@ -96,7 +110,7 @@ onMounted(async () => {
|
||||||
|
// Fall back to a stub built from the route + first song's cover.
|
||||||
|
playlist.value = {
|
||||||
|
id,
|
||||||
|
- name: '歌单',
|
||||||
|
+ name: kind === 'album' ? '专辑' : '歌单',
|
||||||
|
description: '',
|
||||||
|
coverUrl: songList[0]?.coverUrl ?? '',
|
||||||
|
songCount: songList.length,
|
||||||
|
@@ -104,7 +118,7 @@ onMounted(async () => {
|
||||||
|
} else {
|
||||||
|
playlist.value = null;
|
||||||
|
if (detailRes.status === 'rejected') {
|
||||||
|
- console.error('Failed to load playlist detail:', (detailRes.reason as any)?.response?.status, (detailRes.reason as any)?.message);
|
||||||
|
+ console.error('Failed to load detail:', (detailRes.reason as any)?.response?.status, (detailRes.reason as any)?.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
songs.value = songList;
|
||||||
|
diff --git a/web/src/views/Search.vue b/web/src/views/Search.vue
|
||||||
|
index 0536e36..8822277 100644
|
||||||
|
--- a/web/src/views/Search.vue
|
||||||
|
+++ b/web/src/views/Search.vue
|
||||||
|
@@ -20,22 +20,54 @@
|
||||||
|
|
||||||
|
<div v-if="loading" class="loading">搜索中...</div>
|
||||||
|
|
||||||
|
- <div v-else-if="results.length > 0" class="results">
|
||||||
|
- <SongCard
|
||||||
|
- v-for="(song, i) in results"
|
||||||
|
- :key="`${song.platform}-${song.id}`"
|
||||||
|
- :song="song"
|
||||||
|
- :index="i + 1"
|
||||||
|
- :active="store.currentSong?.id === song.id"
|
||||||
|
- @play="store.playSong(song)"
|
||||||
|
- @playNext="store.playNextSong(song)"
|
||||||
|
- @add="store.addSong(song)"
|
||||||
|
- />
|
||||||
|
- </div>
|
||||||
|
+ <template v-else-if="songs.length || albums.length || playlists.length">
|
||||||
|
+ <section v-if="albums.length" class="result-section">
|
||||||
|
+ <h2 class="section-title">专辑</h2>
|
||||||
|
+ <div class="card-grid">
|
||||||
|
+ <router-link
|
||||||
|
+ v-for="al in albums"
|
||||||
|
+ :key="`${al.platform}-${al.id}`"
|
||||||
|
+ :to="`/album/${al.id}?platform=${al.platform}`"
|
||||||
|
+ class="card hover-scale"
|
||||||
|
+ >
|
||||||
|
+ <CoverArt :url="al.coverUrl" :size="160" :radius="10" :show-shadow="true" />
|
||||||
|
+ <div class="card-name">{{ al.name }}</div>
|
||||||
|
+ <div class="card-sub">{{ al.artist }}</div>
|
||||||
|
+ </router-link>
|
||||||
|
+ </div>
|
||||||
|
+ </section>
|
||||||
|
+
|
||||||
|
+ <section v-if="playlists.length" class="result-section">
|
||||||
|
+ <h2 class="section-title">歌单</h2>
|
||||||
|
+ <div class="card-grid">
|
||||||
|
+ <router-link
|
||||||
|
+ v-for="pl in playlists"
|
||||||
|
+ :key="`${pl.platform}-${pl.id}`"
|
||||||
|
+ :to="`/playlist/${pl.id}?platform=${pl.platform}`"
|
||||||
|
+ class="card hover-scale"
|
||||||
|
+ >
|
||||||
|
+ <CoverArt :url="pl.coverUrl" :size="160" :radius="10" :show-shadow="true" />
|
||||||
|
+ <div class="card-name">{{ pl.name }}</div>
|
||||||
|
+ </router-link>
|
||||||
|
+ </div>
|
||||||
|
+ </section>
|
||||||
|
+
|
||||||
|
+ <section v-if="songs.length" class="result-section">
|
||||||
|
+ <h2 class="section-title">单曲</h2>
|
||||||
|
+ <SongCard
|
||||||
|
+ v-for="(song, i) in songs"
|
||||||
|
+ :key="`${song.platform}-${song.id}`"
|
||||||
|
+ :song="song"
|
||||||
|
+ :index="i + 1"
|
||||||
|
+ :active="store.currentSong?.id === song.id"
|
||||||
|
+ @play="store.playSong(song)"
|
||||||
|
+ @playNext="store.playNextSong(song)"
|
||||||
|
+ @add="store.addSong(song)"
|
||||||
|
+ />
|
||||||
|
+ </section>
|
||||||
|
+ </template>
|
||||||
|
|
||||||
|
- <div v-else-if="searched" class="empty">
|
||||||
|
- 未找到相关结果
|
||||||
|
- </div>
|
||||||
|
+ <div v-else-if="searched" class="empty">未找到相关结果</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
@@ -45,15 +77,21 @@ import { useRoute } from 'vue-router';
|
||||||
|
import { Icon } from '@iconify/vue';
|
||||||
|
import axios from 'axios';
|
||||||
|
import { usePlayerStore } from '../stores/player.js';
|
||||||
|
+import type { Song } from '../stores/player.js';
|
||||||
|
import SongCard from '../components/SongCard.vue';
|
||||||
|
+import CoverArt from '../components/CoverArt.vue';
|
||||||
|
|
||||||
|
const store = usePlayerStore();
|
||||||
|
const route = useRoute();
|
||||||
|
|
||||||
|
const query = ref((route.query.q as string) || '');
|
||||||
|
-import { Song } from '../stores/player.js';
|
||||||
|
|
||||||
|
-const results = ref<Song[]>([]);
|
||||||
|
+interface Album { id: string; name: string; artist: string; coverUrl: string; songCount?: number; platform: string; }
|
||||||
|
+interface Playlist { id: string; name: string; coverUrl: string; songCount?: number; platform: string; }
|
||||||
|
+
|
||||||
|
+const songs = ref<Song[]>([]);
|
||||||
|
+const albums = ref<Album[]>([]);
|
||||||
|
+const playlists = ref<Playlist[]>([]);
|
||||||
|
const loading = ref(false);
|
||||||
|
const searched = ref(false);
|
||||||
|
|
||||||
|
@@ -62,12 +100,12 @@ async function doSearch() {
|
||||||
|
loading.value = true;
|
||||||
|
searched.value = true;
|
||||||
|
try {
|
||||||
|
- const res = await axios.get('/api/music/search/all', {
|
||||||
|
- params: { q: query.value },
|
||||||
|
- });
|
||||||
|
- results.value = res.data.songs;
|
||||||
|
+ const res = await axios.get('/api/music/search/all', { params: { q: query.value } });
|
||||||
|
+ songs.value = res.data.songs ?? [];
|
||||||
|
+ albums.value = res.data.albums ?? [];
|
||||||
|
+ playlists.value = res.data.playlists ?? [];
|
||||||
|
} catch {
|
||||||
|
- results.value = [];
|
||||||
|
+ songs.value = []; albums.value = []; playlists.value = [];
|
||||||
|
} finally {
|
||||||
|
loading.value = false;
|
||||||
|
}
|
||||||
|
@@ -141,4 +179,23 @@ onMounted(() => {
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 2px;
|
||||||
|
}
|
||||||
|
+
|
||||||
|
+.result-section {
|
||||||
|
+ margin-bottom: 32px;
|
||||||
|
+ .section-title { font-size: 18px; margin: 0 0 12px; opacity: 0.85; }
|
||||||
|
+}
|
||||||
|
+.card-grid {
|
||||||
|
+ display: grid;
|
||||||
|
+ grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
|
||||||
|
+ gap: 16px;
|
||||||
|
+}
|
||||||
|
+.card {
|
||||||
|
+ display: flex;
|
||||||
|
+ flex-direction: column;
|
||||||
|
+ gap: 6px;
|
||||||
|
+ text-decoration: none;
|
||||||
|
+ color: inherit;
|
||||||
|
+ .card-name { font-size: 14px; line-height: 1.3; max-height: 2.6em; overflow: hidden; }
|
||||||
|
+ .card-sub { font-size: 12px; opacity: 0.6; }
|
||||||
|
+}
|
||||||
|
</style>
|
||||||
@@ -23,6 +23,7 @@ import { execFileSync } from "node:child_process";
|
|||||||
import { existsSync, readFileSync } from "node:fs";
|
import { existsSync, readFileSync } from "node:fs";
|
||||||
import { dirname, join } from "node:path";
|
import { dirname, join } from "node:path";
|
||||||
import { fileURLToPath } from "node:url";
|
import { fileURLToPath } from "node:url";
|
||||||
|
import { createLineWriter } from "./lib/console-log.mjs";
|
||||||
|
|
||||||
const ROOT = join(dirname(fileURLToPath(import.meta.url)), "..");
|
const ROOT = join(dirname(fileURLToPath(import.meta.url)), "..");
|
||||||
const NODE_MODULES = join(ROOT, "node_modules");
|
const NODE_MODULES = join(ROOT, "node_modules");
|
||||||
@@ -106,7 +107,11 @@ for (const spec of REQUIRED) {
|
|||||||
function report() {
|
function report() {
|
||||||
const stamp = readStamp();
|
const stamp = readStamp();
|
||||||
const mismatch = broken.find((b) => b.abiMismatch);
|
const mismatch = broken.find((b) => b.abiMismatch);
|
||||||
const out = (line) => process.stderr.write(`${line}\n`);
|
// Never let a failed console write become an uncaught error and replace this
|
||||||
|
// report with a stack trace - the console that cannot print the Chinese half
|
||||||
|
// of these lines is exactly the one a user needs the English half from.
|
||||||
|
// See scripts/lib/console-log.mjs and issue #152.
|
||||||
|
const out = createLineWriter(process.stderr);
|
||||||
|
|
||||||
out("");
|
out("");
|
||||||
out("============================================================");
|
out("============================================================");
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
# ==========================================
|
# ==========================================
|
||||||
|
|
||||||
# --- Stage 1: Build backend + frontend ---
|
# --- Stage 1: Build backend + frontend ---
|
||||||
FROM node:20-slim AS builder
|
FROM node:22-slim AS builder
|
||||||
|
|
||||||
# Install build tools for native modules (opus, better-sqlite3)
|
# Install build tools for native modules (opus, better-sqlite3)
|
||||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||||
@@ -30,7 +30,7 @@ RUN npm run build
|
|||||||
RUN rm -rf node_modules && npm ci --production && npm cache clean --force
|
RUN rm -rf node_modules && npm ci --production && npm cache clean --force
|
||||||
|
|
||||||
# --- Stage 2: Production image ---
|
# --- Stage 2: Production image ---
|
||||||
FROM node:20-slim
|
FROM node:22-slim
|
||||||
|
|
||||||
# Install system FFmpeg — the ffmpeg-static npm package bundles a pre-compiled
|
# Install system FFmpeg — the ffmpeg-static npm package bundles a pre-compiled
|
||||||
# binary that can SIGSEGV inside Docker (incompatible glibc / missing libs).
|
# binary that can SIGSEGV inside Docker (incompatible glibc / missing libs).
|
||||||
@@ -51,6 +51,8 @@ COPY --from=builder /app/node_modules ./node_modules
|
|||||||
# goes through npm, but an interactive `docker exec ... npm start` would
|
# goes through npm, but an interactive `docker exec ... npm start` would
|
||||||
# otherwise die on a missing script rather than starting the bot.
|
# otherwise die on a missing script rather than starting the bot.
|
||||||
COPY --from=builder /app/scripts/check-native.mjs ./scripts/check-native.mjs
|
COPY --from=builder /app/scripts/check-native.mjs ./scripts/check-native.mjs
|
||||||
|
# ...and the module it imports for crash-proof logging.
|
||||||
|
COPY --from=builder /app/scripts/lib/console-log.mjs ./scripts/lib/console-log.mjs
|
||||||
|
|
||||||
# Data directory for database, cookies, logs
|
# Data directory for database, cookies, logs
|
||||||
RUN mkdir -p /app/data
|
RUN mkdir -p /app/data
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ import { Readable } from "node:stream";
|
|||||||
import { execFileSync, execSync } from "node:child_process";
|
import { execFileSync, execSync } from "node:child_process";
|
||||||
import { createRequire } from "node:module";
|
import { createRequire } from "node:module";
|
||||||
import { fileURLToPath } from "node:url";
|
import { fileURLToPath } from "node:url";
|
||||||
|
import { createLineWriter } from "./lib/console-log.mjs";
|
||||||
|
|
||||||
const ROOT = join(dirname(fileURLToPath(import.meta.url)), "..");
|
const ROOT = join(dirname(fileURLToPath(import.meta.url)), "..");
|
||||||
const NODE_MODULES = join(ROOT, "node_modules");
|
const NODE_MODULES = join(ROOT, "node_modules");
|
||||||
@@ -61,6 +62,11 @@ const CDN = process.argv[2] || "https://cdn.npmmirror.com/binaries";
|
|||||||
const PLATFORM = process.platform;
|
const PLATFORM = process.platform;
|
||||||
const ARCH = process.arch;
|
const ARCH = process.arch;
|
||||||
const NODE_ABI = process.versions.modules;
|
const NODE_ABI = process.versions.modules;
|
||||||
|
const NODE_MAJOR = Number(process.versions.node.split(".")[0]);
|
||||||
|
/** The newest Node major this project is regularly tested against, and the one
|
||||||
|
* every required addon currently ships a prebuild for. Keep in sync with
|
||||||
|
* TESTED_NODE_MAJOR in scripts/setup.bat. */
|
||||||
|
const TESTED_NODE_MAJOR = 22;
|
||||||
|
|
||||||
/** Modules the bot cannot start without. ffmpeg-static is optional: a system
|
/** Modules the bot cannot start without. ffmpeg-static is optional: a system
|
||||||
* ffmpeg on PATH is a documented fallback, so it only ever produces a WARN. */
|
* ffmpeg on PATH is a documented fallback, so it only ever produces a WARN. */
|
||||||
@@ -79,10 +85,17 @@ const FFMPEG_MIN_BYTES = 20 * 1024 * 1024;
|
|||||||
// log keeps the full transcript too.
|
// log keeps the full transcript too.
|
||||||
const ECHO_STDOUT = process.env.TSMB_BINARY_LOG_STDOUT === "1";
|
const ECHO_STDOUT = process.env.TSMB_BINARY_LOG_STDOUT === "1";
|
||||||
|
|
||||||
|
// Both writers swallow a failed write instead of letting it become an uncaught
|
||||||
|
// 'error' event: a console that cannot print the Chinese half of a line (issue
|
||||||
|
// #152) must not be able to abort a whole setup run. The two streams degrade
|
||||||
|
// independently, so setup.log keeps the full bilingual transcript either way.
|
||||||
|
const writeErr = createLineWriter(process.stderr);
|
||||||
|
const writeOut = createLineWriter(process.stdout);
|
||||||
|
|
||||||
function log(msg) {
|
function log(msg) {
|
||||||
const line = msg === "" ? "" : ` [binary] ${msg}`;
|
const line = msg === "" ? "" : ` [binary] ${msg}`;
|
||||||
process.stderr.write(`${line}\n`);
|
writeErr(line);
|
||||||
if (ECHO_STDOUT) process.stdout.write(`${line}\n`);
|
if (ECHO_STDOUT) writeOut(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -401,6 +414,25 @@ function buildFromSource(command) {
|
|||||||
execSync(command, { cwd: ROOT, stdio: ["ignore", "inherit", "inherit"] });
|
execSync(command, { cwd: ROOT, stdio: ["ignore", "inherit", "inherit"] });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A 404 from the CDN is not a mirror outage: it means this exact package
|
||||||
|
* version publishes no prebuilt binary for the running Node ABI at all.
|
||||||
|
* @discordjs/opus 0.10.0 has no build for Node 24 (ABI 137), so a user on that
|
||||||
|
* major lands in the source-build fallback below and is told to install Python
|
||||||
|
* and a C++ toolchain. Switching Node major is the far cheaper fix, and nothing
|
||||||
|
* else in this output points at it. (better-sqlite3 dropping its Node 20 / ABI
|
||||||
|
* 115 builds in 12.10.0 is why Node 20 is no longer accepted at all.)
|
||||||
|
* See issue #152.
|
||||||
|
*/
|
||||||
|
function explainMissingPrebuild(name, version, err) {
|
||||||
|
if (!/HTTP 404/.test(err.message)) return;
|
||||||
|
log(`${name}: ${name}@${version} ships no prebuilt binary for Node ${NODE_MAJOR} (ABI ${NODE_ABI})`);
|
||||||
|
log(
|
||||||
|
`${name}: Node ${TESTED_NODE_MAJOR} LTS has one — switching Node is usually much quicker than ` +
|
||||||
|
`setting up a compiler (换用 Node ${TESTED_NODE_MAJOR} LTS 通常比装编译环境快得多)`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function buildToolsHint() {
|
function buildToolsHint() {
|
||||||
log("Install build tools first:");
|
log("Install build tools first:");
|
||||||
log(" Windows: npm install --global windows-build-tools (或安装 Visual Studio Build Tools + Python)");
|
log(" Windows: npm install --global windows-build-tools (或安装 Visual Studio Build Tools + Python)");
|
||||||
@@ -521,6 +553,7 @@ async function ensureOpus() {
|
|||||||
log(`${name}: prebuilt binary installed`);
|
log(`${name}: prebuilt binary installed`);
|
||||||
} catch (cdnErr) {
|
} catch (cdnErr) {
|
||||||
log(`${name}: CDN install failed (${cdnErr.message})`);
|
log(`${name}: CDN install failed (${cdnErr.message})`);
|
||||||
|
explainMissingPrebuild(name, version, cdnErr);
|
||||||
log(`${name}: falling back to a source build — 'npm rebuild ${name}' (可能需要几分钟)`);
|
log(`${name}: falling back to a source build — 'npm rebuild ${name}' (可能需要几分钟)`);
|
||||||
buildFromSource(`npm rebuild ${name}`);
|
buildFromSource(`npm rebuild ${name}`);
|
||||||
}
|
}
|
||||||
@@ -591,6 +624,7 @@ async function ensureBetterSqlite3() {
|
|||||||
log(`${name}: prebuilt binary installed (${humanSize(dest)})`);
|
log(`${name}: prebuilt binary installed (${humanSize(dest)})`);
|
||||||
} catch (cdnErr) {
|
} catch (cdnErr) {
|
||||||
log(`${name}: CDN install failed (${cdnErr.message})`);
|
log(`${name}: CDN install failed (${cdnErr.message})`);
|
||||||
|
explainMissingPrebuild(name, version, cdnErr);
|
||||||
log(`${name}: falling back to a source build — 'npm rebuild ${name} --build-from-source' (可能需要几分钟)`);
|
log(`${name}: falling back to a source build — 'npm rebuild ${name} --build-from-source' (可能需要几分钟)`);
|
||||||
buildFromSource(`npm rebuild ${name} --build-from-source`);
|
buildFromSource(`npm rebuild ${name} --build-from-source`);
|
||||||
}
|
}
|
||||||
@@ -661,10 +695,9 @@ try {
|
|||||||
// that same loop. Run these concurrently and the first module to fall back to
|
// that same loop. Run these concurrently and the first module to fall back to
|
||||||
// a source build kills every download still in flight — the connection is
|
// a source build kills every download still in flight — the connection is
|
||||||
// healthy, the timer just never got a chance to be reset. That is not a rare
|
// healthy, the timer just never got a chance to be reset. That is not a rare
|
||||||
// race: npmmirror has no opus prebuild for ABI 137 (Node 24) and no
|
// race: @discordjs/opus 0.10.0 has no prebuild for ABI 137, so on Node 24 that
|
||||||
// better-sqlite3 prebuild for ABI 115 (Node 20), so on both of the Node
|
// module 404s within ~100ms and starts building while ffmpeg's ~80MB download
|
||||||
// versions this project supports, one module 404s within ~100ms and starts
|
// is still going. ffmpeg is optional,
|
||||||
// building while ffmpeg's ~80MB download is still going. ffmpeg is optional,
|
|
||||||
// so the spurious failure used to be swallowed as a WARN and setup still
|
// so the spurious failure used to be swallowed as a WARN and setup still
|
||||||
// reported success — leaving the user with no ffmpeg and no working playback.
|
// reported success — leaving the user with no ffmpeg and no working playback.
|
||||||
// Nothing here benefits from overlap anyway: every probe is execFileSync.
|
// Nothing here benefits from overlap anyway: every probe is execFileSync.
|
||||||
|
|||||||
@@ -0,0 +1,142 @@
|
|||||||
|
/**
|
||||||
|
* Crash-proof line logging for the setup scripts.
|
||||||
|
*
|
||||||
|
* WHY THIS EXISTS (issue #152)
|
||||||
|
* ----------------------------
|
||||||
|
* setup.bat runs `chcp 65001` and shows progress on stderr. Some Windows
|
||||||
|
* consoles - Windows Server 2012 R2 above all - cannot render non-ASCII text in
|
||||||
|
* that code page and the OS fails the write with EIO. `process.stderr` is an
|
||||||
|
* ordinary stream, so that EIO arrives as an 'error' event, and a stream with
|
||||||
|
* no 'error' listener rethrows it as an uncaught exception:
|
||||||
|
*
|
||||||
|
* Error: write EIO
|
||||||
|
* at afterWriteDispatched (node:internal/stream_base_commons:159:15)
|
||||||
|
* ...
|
||||||
|
* at log (scripts/download-binaries.mjs:84:18)
|
||||||
|
* at ensureFfmpeg (scripts/download-binaries.mjs:451:5)
|
||||||
|
*
|
||||||
|
* That is setup killing itself inside its own progress logging, on the first
|
||||||
|
* line of the run that happened to contain Chinese - nothing was wrong with the
|
||||||
|
* download it was about to start.
|
||||||
|
*
|
||||||
|
* So: listen for the error and degrade instead of dying.
|
||||||
|
* full -> ascii : drop the CJK the console choked on, keep the English half
|
||||||
|
* ascii -> off : the stream is simply gone (closed pipe) - stay quiet
|
||||||
|
* Each stream degrades on its own, so a console that gives up does not cost
|
||||||
|
* setup.log its full bilingual transcript: that stdout is a redirected file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const HAS_NON_ASCII = /[^\x00-\x7F]/;
|
||||||
|
|
||||||
|
/** Placeholders for a removed run: one that separated words, one that did not. */
|
||||||
|
const SPACED = "\u0000";
|
||||||
|
const TIGHT = "\u0001";
|
||||||
|
|
||||||
|
/** Punctuation the bilingual strings use that has an obvious ASCII twin. */
|
||||||
|
const PUNCTUATION = new Map(
|
||||||
|
Object.entries({
|
||||||
|
"—": "-",
|
||||||
|
"–": "-",
|
||||||
|
"…": "...",
|
||||||
|
"“": '"',
|
||||||
|
"”": '"',
|
||||||
|
"‘": "'",
|
||||||
|
"’": "'",
|
||||||
|
",": ",",
|
||||||
|
"。": ".",
|
||||||
|
"、": ",",
|
||||||
|
":": ":",
|
||||||
|
";": ";",
|
||||||
|
"(": "(",
|
||||||
|
")": ")",
|
||||||
|
"!": "!",
|
||||||
|
"?": "?",
|
||||||
|
"←": "<-",
|
||||||
|
"→": "->",
|
||||||
|
"×": "x",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Best-effort ASCII rendering of a log line, for a console that cannot print
|
||||||
|
* anything else. Returns null when nothing worth printing survives - every
|
||||||
|
* Chinese-only line in these scripts sits directly beside an English line
|
||||||
|
* saying the same thing, so dropping it loses no information.
|
||||||
|
*/
|
||||||
|
export function toAsciiFallback(text) {
|
||||||
|
if (!HAS_NON_ASCII.test(text)) return text;
|
||||||
|
|
||||||
|
let out = "";
|
||||||
|
for (const ch of text) out += PUNCTUATION.get(ch) ?? ch;
|
||||||
|
|
||||||
|
out = out
|
||||||
|
.replace(/[\u0000\u0001]/g, "")
|
||||||
|
// Mark each removed run rather than just deleting it, so the tidy-up below
|
||||||
|
// can tell "a separator that introduced text we dropped" from "a separator
|
||||||
|
// that belongs to the English half". SPACED was holding two ASCII words
|
||||||
|
// apart; TIGHT was hugging a bracket or a comma.
|
||||||
|
.replace(/[ \t]*[^\x00-\x7F]+[ \t]*/g, (run) =>
|
||||||
|
/^[ \t]/.test(run) && /[ \t]$/.test(run) ? SPACED : TIGHT,
|
||||||
|
)
|
||||||
|
// "(可能需要几分钟)" — the parentheses held nothing else.
|
||||||
|
.replace(/[ \t]*\([ \t]*(?:[\u0000\u0001][ \t]*)+\)/g, "")
|
||||||
|
// "FAILED — 编译失败", "(~80 MB, 请耐心等待)" — drop the trailing marks along
|
||||||
|
// with the separators that were only ever there to introduce them.
|
||||||
|
.replace(/[ \t]*[-,;:]*[ \t]*(?:[\u0000\u0001][ \t,;:-]*)+(?=[)\]]|$)/gm, "")
|
||||||
|
.replace(/\u0000/g, " ")
|
||||||
|
.replace(/\u0001/g, "")
|
||||||
|
.replace(/[ \t]+$/gm, "");
|
||||||
|
|
||||||
|
return /[A-Za-z0-9]/.test(out) ? out : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** One degradation state per stream, shared by every writer built on it. */
|
||||||
|
const guards = new WeakMap();
|
||||||
|
|
||||||
|
function guardFor(stream) {
|
||||||
|
const existing = guards.get(stream);
|
||||||
|
if (existing) return existing;
|
||||||
|
|
||||||
|
const guard = { mode: "full" };
|
||||||
|
guards.set(stream, guard);
|
||||||
|
try {
|
||||||
|
// The whole point: without this listener the next EIO/EPIPE is fatal.
|
||||||
|
stream.on("error", () => degrade(guard));
|
||||||
|
} catch {
|
||||||
|
/* not an EventEmitter - the try/catch around write() still guards us */
|
||||||
|
}
|
||||||
|
return guard;
|
||||||
|
}
|
||||||
|
|
||||||
|
function degrade(guard) {
|
||||||
|
guard.mode = guard.mode === "full" ? "ascii" : "off";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build a `writeLine(text)` that appends a newline, never throws, and never
|
||||||
|
* lets a failed console write take the process down with it.
|
||||||
|
* Returns true when the line reached the stream.
|
||||||
|
*/
|
||||||
|
export function createLineWriter(stream) {
|
||||||
|
const guard = guardFor(stream);
|
||||||
|
|
||||||
|
return function writeLine(text) {
|
||||||
|
if (guard.mode === "off") return false;
|
||||||
|
|
||||||
|
let line = text;
|
||||||
|
if (guard.mode === "ascii") {
|
||||||
|
line = toAsciiFallback(text);
|
||||||
|
if (line === null) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
stream.write(`${line}\n`);
|
||||||
|
return true;
|
||||||
|
} catch {
|
||||||
|
// A synchronous throw (EBADF on a closed handle) never reaches the
|
||||||
|
// 'error' listener, so degrade here too.
|
||||||
|
degrade(guard);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,133 @@
|
|||||||
|
import { EventEmitter } from "node:events";
|
||||||
|
import { describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
|
import { createLineWriter, toAsciiFallback } from "./console-log.mjs";
|
||||||
|
|
||||||
|
/** Stand-in for process.stderr: an EventEmitter with a write() we can steer. */
|
||||||
|
function fakeStream() {
|
||||||
|
const stream = new EventEmitter();
|
||||||
|
stream.written = [];
|
||||||
|
stream.throwOnWrite = false;
|
||||||
|
stream.write = (chunk) => {
|
||||||
|
if (stream.throwOnWrite) throw new Error("EBADF");
|
||||||
|
stream.written.push(chunk);
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("toAsciiFallback", () => {
|
||||||
|
it("leaves ASCII lines exactly as they are", () => {
|
||||||
|
const line = " [binary] better-sqlite3: OK (loads under v22.23.2, ABI 127)";
|
||||||
|
expect(toAsciiFallback(line)).toBe(line);
|
||||||
|
expect(toAsciiFallback("")).toBe("");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps the English half of the line that crashed setup in #152", () => {
|
||||||
|
expect(
|
||||||
|
toAsciiFallback(
|
||||||
|
" [binary] ffmpeg-static: GET https://cdn/ffmpeg.gz (~80 MB, 这一步比较慢,请耐心等待)",
|
||||||
|
),
|
||||||
|
).toBe(" [binary] ffmpeg-static: GET https://cdn/ffmpeg.gz (~80 MB)");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("drops parentheses and separators left stranded by the removed text", () => {
|
||||||
|
expect(
|
||||||
|
toAsciiFallback(" [binary] better-sqlite3: falling back — 'npm rebuild' (可能需要几分钟)"),
|
||||||
|
).toBe(" [binary] better-sqlite3: falling back - 'npm rebuild'");
|
||||||
|
expect(
|
||||||
|
toAsciiFallback(" Windows: npm install --global windows-build-tools (或安装 VS Build Tools)"),
|
||||||
|
).toBe(" Windows: npm install --global windows-build-tools (VS Build Tools)");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("drops a Chinese-only line, which always has an English twin beside it", () => {
|
||||||
|
expect(toAsciiFallback("必需的原生模块不可用,机器人无法启动 —— 请查看上面的错误信息。")).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("preserves the indentation the summary is aligned on, and drops the dangling dash", () => {
|
||||||
|
expect(toAsciiFallback(" - better-sqlite3 FAILED — 编译失败")).toBe(
|
||||||
|
" - better-sqlite3 FAILED",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps a separator that belongs to the English half", () => {
|
||||||
|
expect(toAsciiFallback("Summary — Node v22.0.0 / ABI 127 / win32-x64:")).toBe(
|
||||||
|
"Summary - Node v22.0.0 / ABI 127 / win32-x64:",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("createLineWriter", () => {
|
||||||
|
it("appends a newline and reports the write", () => {
|
||||||
|
const stream = fakeStream();
|
||||||
|
expect(createLineWriter(stream)("hello")).toBe(true);
|
||||||
|
expect(stream.written).toEqual(["hello\n"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("survives the EIO that killed setup: an 'error' event must not throw", () => {
|
||||||
|
const stream = fakeStream();
|
||||||
|
createLineWriter(stream);
|
||||||
|
expect(stream.listenerCount("error")).toBe(1);
|
||||||
|
expect(() => stream.emit("error", Object.assign(new Error("write EIO"), { code: "EIO" }))).not.toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("falls back to ASCII once the console has refused a line", () => {
|
||||||
|
const stream = fakeStream();
|
||||||
|
const write = createLineWriter(stream);
|
||||||
|
write(" [binary] GET https://cdn/ffmpeg.gz (~80 MB, 这一步比较慢,请耐心等待)");
|
||||||
|
stream.emit("error", new Error("write EIO"));
|
||||||
|
write(" [binary] GET https://cdn/opus.tar.gz (~1 MB, 这一步比较慢,请耐心等待)");
|
||||||
|
expect(stream.written).toEqual([
|
||||||
|
" [binary] GET https://cdn/ffmpeg.gz (~80 MB, 这一步比较慢,请耐心等待)\n",
|
||||||
|
" [binary] GET https://cdn/opus.tar.gz (~1 MB)\n",
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("goes quiet after a second failure rather than retrying a dead stream", () => {
|
||||||
|
const stream = fakeStream();
|
||||||
|
const write = createLineWriter(stream);
|
||||||
|
stream.emit("error", new Error("write EIO"));
|
||||||
|
stream.emit("error", new Error("write EPIPE"));
|
||||||
|
expect(write("anything at all")).toBe(false);
|
||||||
|
expect(stream.written).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("degrades on a synchronous throw too, which never reaches the listener", () => {
|
||||||
|
const stream = fakeStream();
|
||||||
|
const write = createLineWriter(stream);
|
||||||
|
stream.throwOnWrite = true;
|
||||||
|
expect(write(" [binary] 下载中 downloading")).toBe(false);
|
||||||
|
stream.throwOnWrite = false;
|
||||||
|
write(" [binary] 下载中 downloading");
|
||||||
|
expect(stream.written).toEqual([" [binary] downloading\n"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("degrades each stream on its own, so setup.log keeps the full transcript", () => {
|
||||||
|
const console_ = fakeStream();
|
||||||
|
const logFile = fakeStream();
|
||||||
|
const writeConsole = createLineWriter(console_);
|
||||||
|
const writeLog = createLineWriter(logFile);
|
||||||
|
console_.emit("error", new Error("write EIO"));
|
||||||
|
|
||||||
|
const line = " [binary] ffmpeg-static: 下载完成 done";
|
||||||
|
writeConsole(line);
|
||||||
|
writeLog(line);
|
||||||
|
|
||||||
|
expect(console_.written).toEqual([" [binary] ffmpeg-static: done\n"]);
|
||||||
|
expect(logFile.written).toEqual([`${line}\n`]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("never installs a second listener for a stream that already has a writer", () => {
|
||||||
|
const stream = fakeStream();
|
||||||
|
createLineWriter(stream);
|
||||||
|
createLineWriter(stream);
|
||||||
|
expect(stream.listenerCount("error")).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("still guards a stream that is not an EventEmitter", () => {
|
||||||
|
const stream = { write: vi.fn(() => { throw new Error("EBADF"); }) };
|
||||||
|
const write = createLineWriter(stream);
|
||||||
|
expect(() => write("line")).not.toThrow();
|
||||||
|
expect(write("line")).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
+11
-9
@@ -11,7 +11,7 @@ title TSMusicBot Setup
|
|||||||
:: ============================================================
|
:: ============================================================
|
||||||
|
|
||||||
set "SCRIPT_VERSION=2.2"
|
set "SCRIPT_VERSION=2.2"
|
||||||
set "MIN_NODE_MAJOR=20"
|
set "MIN_NODE_MAJOR=22"
|
||||||
:: Newest Node major this project is regularly tested against. Anything above
|
:: Newest Node major this project is regularly tested against. Anything above
|
||||||
:: still works, it just may have no prebuilt addons and fall back to a source build.
|
:: still works, it just may have no prebuilt addons and fall back to a source build.
|
||||||
set "TESTED_NODE_MAJOR=22"
|
set "TESTED_NODE_MAJOR=22"
|
||||||
@@ -67,13 +67,15 @@ for /f "tokens=1 delims=v." %%a in ("%NODE_VER%") do set "NODE_MAJOR=%%a"
|
|||||||
call :log "Node.js version: %NODE_VER%"
|
call :log "Node.js version: %NODE_VER%"
|
||||||
echo [OK] Node.js found: %NODE_VER%
|
echo [OK] Node.js found: %NODE_VER%
|
||||||
|
|
||||||
:: The supported floor is not just a major version, so let node decide:
|
:: The supported floor is not just a major version, so let node decide.
|
||||||
:: @honeybbq/teamspeak-client needs >=20.19, @sansenjian/qq-music-api needs
|
:: Node 20 was dropped: better-sqlite3 ships no prebuilt binary for its ABI
|
||||||
:: >=20.17 / >=22.9, and the odd majors (21 / 23) are excluded by
|
:: (115) since 12.10.0, so every Node 20 install needed Python and a C++
|
||||||
:: better-sqlite3 and vitest. Keep this in sync with "engines" in package.json.
|
:: toolchain just to get off the ground (issue #152). The odd majors (21 /
|
||||||
node -e "const v=process.versions.node.split('.').map(Number); process.exit((v[0]===20&&v[1]>=19)||(v[0]===22&&v[1]>=12)||v[0]>=24?0:1)"
|
:: 23) are excluded by better-sqlite3 and vitest.
|
||||||
|
:: Keep this in sync with "engines" in package.json.
|
||||||
|
node -e "const v=process.versions.node.split('.').map(Number); process.exit((v[0]===22&&v[1]>=12)||v[0]>=24?0:1)"
|
||||||
if errorlevel 1 (
|
if errorlevel 1 (
|
||||||
call :error "Node.js %NODE_VER% is not supported. Use Node 20.19+ LTS or Node 22.12+ LTS."
|
call :error "Node.js %NODE_VER% is not supported. Use Node 22.12+ LTS or newer."
|
||||||
echo Download: https://nodejs.org/ or https://nodejs.cn/
|
echo Download: https://nodejs.org/ or https://nodejs.cn/
|
||||||
pause
|
pause
|
||||||
exit /b 1
|
exit /b 1
|
||||||
@@ -86,10 +88,10 @@ if errorlevel 1 (
|
|||||||
:: characters and starts eating the "echo " prefix of following lines.
|
:: characters and starts eating the "echo " prefix of following lines.
|
||||||
:: Bilingual guidance lives in the Node scripts, which print UTF-8 reliably.
|
:: Bilingual guidance lives in the Node scripts, which print UTF-8 reliably.
|
||||||
if %NODE_MAJOR% GTR %TESTED_NODE_MAJOR% (
|
if %NODE_MAJOR% GTR %TESTED_NODE_MAJOR% (
|
||||||
echo [WARN] Node %NODE_VER% is newer than the tested LTS line, Node 20 / Node 22.
|
echo [WARN] Node %NODE_VER% is newer than the tested LTS line, Node 22.
|
||||||
echo Newer Node majors may have no prebuilt opus / better-sqlite3,
|
echo Newer Node majors may have no prebuilt opus / better-sqlite3,
|
||||||
echo so setup falls back to a source build - slower, needs C++ build tools.
|
echo so setup falls back to a source build - slower, needs C++ build tools.
|
||||||
echo Recommended: Node 20 LTS or Node 22 LTS - https://nodejs.org/ or https://nodejs.cn/
|
echo Recommended: Node 22 LTS - https://nodejs.org/ or https://nodejs.cn/
|
||||||
echo This is only a warning; setup still builds the binaries for %NODE_VER%.
|
echo This is only a warning; setup still builds the binaries for %NODE_VER%.
|
||||||
call :log "[WARN] Node major %NODE_MAJOR% is newer than tested LTS %TESTED_NODE_MAJOR%"
|
call :log "[WARN] Node major %NODE_MAJOR% is newer than tested LTS %TESTED_NODE_MAJOR%"
|
||||||
)
|
)
|
||||||
|
|||||||
+8
-6
@@ -32,16 +32,18 @@ echo "[OK] Node.js $(node -v)"
|
|||||||
TESTED_NODE_MAJOR=22
|
TESTED_NODE_MAJOR=22
|
||||||
NODE_MAJOR="$(node -p 'process.versions.node.split(".")[0]')"
|
NODE_MAJOR="$(node -p 'process.versions.node.split(".")[0]')"
|
||||||
|
|
||||||
# The floor is not just a major version, so let node decide: @honeybbq/teamspeak-client
|
# The floor is not just a major version, so let node decide. Node 20 was dropped:
|
||||||
# needs >=20.19, @sansenjian/qq-music-api needs >=20.17 / >=22.9, and the odd majors
|
# better-sqlite3 ships no prebuilt binary for its ABI (115) since 12.10.0, so every
|
||||||
# (21 / 23) are excluded by better-sqlite3 and vitest. Keep in sync with package.json "engines".
|
# Node 20 install needed Python and a C++ toolchain just to get off the ground
|
||||||
if ! node -e 'const v=process.versions.node.split(".").map(Number); process.exit((v[0]===20&&v[1]>=19)||(v[0]===22&&v[1]>=12)||v[0]>=24?0:1)'; then
|
# (issue #152). The odd majors (21 / 23) are excluded by better-sqlite3 and vitest.
|
||||||
echo "[ERROR] Node.js $(node -v) is not supported. Use Node 20.19+ LTS or Node 22.12+ LTS."
|
# Keep in sync with package.json "engines".
|
||||||
|
if ! node -e 'const v=process.versions.node.split(".").map(Number); process.exit((v[0]===22&&v[1]>=12)||v[0]>=24?0:1)'; then
|
||||||
|
echo "[ERROR] Node.js $(node -v) is not supported. Use Node 22.12+ LTS or newer."
|
||||||
echo " https://nodejs.org/ | https://nodejs.cn/"
|
echo " https://nodejs.org/ | https://nodejs.cn/"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
if [ "$NODE_MAJOR" -gt "$TESTED_NODE_MAJOR" ]; then
|
if [ "$NODE_MAJOR" -gt "$TESTED_NODE_MAJOR" ]; then
|
||||||
echo "[WARN] Node $(node -v) is newer than the tested LTS line (Node 20 / Node 22)."
|
echo "[WARN] Node $(node -v) is newer than the tested LTS line (Node 22)."
|
||||||
echo " 新版 Node 可能没有现成的 opus / better-sqlite3 预编译包,"
|
echo " 新版 Node 可能没有现成的 opus / better-sqlite3 预编译包,"
|
||||||
echo " 安装时会自动改用源码编译,需要 C/C++ 构建工具,速度较慢。"
|
echo " 安装时会自动改用源码编译,需要 C/C++ 构建工具,速度较慢。"
|
||||||
echo " This is only a warning - setup builds the binaries for $(node -v) either way."
|
echo " This is only a warning - setup builds the binaries for $(node -v) either way."
|
||||||
|
|||||||
@@ -0,0 +1,96 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Smoke test for issue #51 — run AFTER you start the bot from temp/preview-merge
|
||||||
|
# (or from main once both PRs are merged).
|
||||||
|
#
|
||||||
|
# Usage: ./scripts/smoke_issue51.sh [HOST]
|
||||||
|
# Default HOST is http://127.0.0.1:3000
|
||||||
|
|
||||||
|
set -e
|
||||||
|
HOST="${1:-http://127.0.0.1:3000}"
|
||||||
|
PASS=0
|
||||||
|
FAIL=0
|
||||||
|
note() { echo -e "\n=== $* ==="; }
|
||||||
|
ok() { echo " [PASS] $*"; PASS=$((PASS+1)); }
|
||||||
|
bad() { echo " [FAIL] $*"; FAIL=$((FAIL+1)); }
|
||||||
|
|
||||||
|
# ---- Album search ----------------------------------------------------------
|
||||||
|
note "1. /api/music/search/all returns {songs,albums,playlists}"
|
||||||
|
|
||||||
|
RES=$(curl.exe -s "$HOST/api/music/search/all?q=%E5%91%A8%E6%9D%B0%E4%BC%A6") # 周杰伦
|
||||||
|
KEYS=$(echo "$RES" | python3 -c "import json,sys;d=json.load(sys.stdin);print(','.join(sorted(d.keys())))")
|
||||||
|
if [ "$KEYS" = "albums,playlists,songs" ]; then ok "keys = $KEYS"; else bad "keys = $KEYS (expected albums,playlists,songs)"; fi
|
||||||
|
|
||||||
|
NA=$(echo "$RES" | python3 -c "import json,sys;d=json.load(sys.stdin);print(len(d.get('albums',[])))")
|
||||||
|
NS=$(echo "$RES" | python3 -c "import json,sys;d=json.load(sys.stdin);print(len(d.get('songs',[])))")
|
||||||
|
NP=$(echo "$RES" | python3 -c "import json,sys;d=json.load(sys.stdin);print(len(d.get('playlists',[])))")
|
||||||
|
echo " songs=$NS, albums=$NA, playlists=$NP"
|
||||||
|
if [ "$NA" -gt 0 ]; then ok "albums populated"; else bad "albums empty (expected >0 for 周杰伦)"; fi
|
||||||
|
if [ "$NS" -gt 0 ]; then ok "songs populated"; fi
|
||||||
|
|
||||||
|
# ---- Album detail playback path -------------------------------------------
|
||||||
|
note "2. /api/music/album/:id returns songs"
|
||||||
|
|
||||||
|
if [ "$NA" -gt 0 ]; then
|
||||||
|
ALBUM_ID=$(echo "$RES" | python3 -c "import json,sys;d=json.load(sys.stdin);a=d['albums'][0];print(a['id'])")
|
||||||
|
PLATFORM=$(echo "$RES" | python3 -c "import json,sys;d=json.load(sys.stdin);a=d['albums'][0];print(a['platform'])")
|
||||||
|
echo " testing album id=$ALBUM_ID platform=$PLATFORM"
|
||||||
|
ASONGS=$(curl.exe -s "$HOST/api/music/album/$ALBUM_ID?platform=$PLATFORM" | python3 -c "import json,sys;d=json.load(sys.stdin);print(len(d.get('songs',[])))" 2>/dev/null || echo 0)
|
||||||
|
if [ "$ASONGS" -gt 0 ]; then ok "album returned $ASONGS songs"; else bad "album endpoint returned 0 songs"; fi
|
||||||
|
else
|
||||||
|
echo " (skipped — no albums to test)"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# ---- Avatar API ------------------------------------------------------------
|
||||||
|
note "3. avatar GET 404 on bot with no avatar"
|
||||||
|
|
||||||
|
BOT_ID=$(curl.exe -s "$HOST/api/bot" | python3 -c "import json,sys;d=json.load(sys.stdin);bots=d.get('bots',[]);print(bots[0]['id'] if bots else '')")
|
||||||
|
if [ -z "$BOT_ID" ]; then bad "no bot found — create a bot first"; exit 1; fi
|
||||||
|
echo " using bot $BOT_ID"
|
||||||
|
|
||||||
|
curl.exe -s -o /dev/null -w "%{http_code}" "$HOST/api/bot/$BOT_ID/avatar" > /tmp/code
|
||||||
|
CODE=$(cat /tmp/code)
|
||||||
|
if [ "$CODE" = "404" ] || [ "$CODE" = "200" ]; then ok "GET initial state = $CODE"; else bad "unexpected GET status $CODE"; fi
|
||||||
|
|
||||||
|
note "4. avatar PUT 200 + GET 200 round-trip"
|
||||||
|
# 1×1 transparent PNG (67 bytes)
|
||||||
|
TINY_PNG_B64="iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII="
|
||||||
|
PUT_RES=$(curl.exe -s -X PUT "$HOST/api/bot/$BOT_ID/avatar" -H "Content-Type: application/json" \
|
||||||
|
-d "{\"dataUrl\":\"data:image/png;base64,$TINY_PNG_B64\"}")
|
||||||
|
echo " PUT response: $PUT_RES"
|
||||||
|
GOT_PATH=$(echo "$PUT_RES" | python3 -c "import json,sys;d=json.load(sys.stdin);print(d.get('path',''))" 2>/dev/null || echo "")
|
||||||
|
if [ "$GOT_PATH" = "$BOT_ID.png" ]; then ok "PUT returned path=$GOT_PATH"; else bad "PUT path = $GOT_PATH (expected $BOT_ID.png)"; fi
|
||||||
|
|
||||||
|
curl.exe -s -o /tmp/avatar_check.png -w "%{http_code}" "$HOST/api/bot/$BOT_ID/avatar" > /tmp/code
|
||||||
|
CODE=$(cat /tmp/code)
|
||||||
|
SIZE=$(wc -c < /tmp/avatar_check.png)
|
||||||
|
if [ "$CODE" = "200" ] && [ "$SIZE" -gt 60 ]; then ok "GET returned 200, $SIZE bytes"; else bad "GET status=$CODE size=$SIZE"; fi
|
||||||
|
|
||||||
|
note "5. avatar DELETE 204 + GET 404"
|
||||||
|
curl.exe -s -X DELETE "$HOST/api/bot/$BOT_ID/avatar" -o /dev/null -w "%{http_code}" > /tmp/code
|
||||||
|
CODE=$(cat /tmp/code)
|
||||||
|
if [ "$CODE" = "204" ]; then ok "DELETE returned 204"; else bad "DELETE status = $CODE"; fi
|
||||||
|
|
||||||
|
curl.exe -s -o /dev/null -w "%{http_code}" "$HOST/api/bot/$BOT_ID/avatar" > /tmp/code
|
||||||
|
CODE=$(cat /tmp/code)
|
||||||
|
if [ "$CODE" = "404" ]; then ok "GET after DELETE returned 404"; else bad "GET after DELETE = $CODE"; fi
|
||||||
|
|
||||||
|
note "6. avatar PUT rejects oversize (>200KB)"
|
||||||
|
BIG_B64=$(node -e "console.log(Buffer.alloc(210*1024,7).toString('base64'))")
|
||||||
|
curl.exe -s -o /dev/null -w "%{http_code}" -X PUT "$HOST/api/bot/$BOT_ID/avatar" \
|
||||||
|
-H "Content-Type: application/json" -d "{\"dataUrl\":\"data:image/png;base64,$BIG_B64\"}" > /tmp/code
|
||||||
|
CODE=$(cat /tmp/code)
|
||||||
|
if [ "$CODE" = "413" ]; then ok "oversize rejected with 413"; else bad "oversize status = $CODE (expected 413)"; fi
|
||||||
|
|
||||||
|
note "7. avatar PUT rejects bad MIME (image/gif)"
|
||||||
|
GIF_B64="R0lGODlhAQABAAAAACw=" # tiny gif
|
||||||
|
curl.exe -s -o /dev/null -w "%{http_code}" -X PUT "$HOST/api/bot/$BOT_ID/avatar" \
|
||||||
|
-H "Content-Type: application/json" -d "{\"dataUrl\":\"data:image/gif;base64,$GIF_B64\"}" > /tmp/code
|
||||||
|
CODE=$(cat /tmp/code)
|
||||||
|
if [ "$CODE" = "400" ]; then ok "bad MIME rejected with 400"; else bad "bad MIME status = $CODE (expected 400)"; fi
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
echo ""
|
||||||
|
echo "============================================="
|
||||||
|
echo "SMOKE RESULT: $PASS passed, $FAIL failed"
|
||||||
|
echo "============================================="
|
||||||
|
[ "$FAIL" -eq 0 ]
|
||||||
@@ -55,13 +55,13 @@ describe("buildFfmpegArgs", () => {
|
|||||||
expect(idx).toBeLessThan(args.indexOf("-i")); // input options must precede -i
|
expect(idx).toBeLessThan(args.indexOf("-i")); // input options must precede -i
|
||||||
});
|
});
|
||||||
|
|
||||||
it("inserts -ss before -i when seekSeconds > 0", () => {
|
it("inserts -ss after -i when seekSeconds > 0", () => {
|
||||||
const args = buildFfmpegArgs("https://example.com/song.mp3", 42);
|
const args = buildFfmpegArgs("https://example.com/song.mp3", 42);
|
||||||
const ssIdx = args.indexOf("-ss");
|
const ssIdx = args.indexOf("-ss");
|
||||||
const iIdx = args.indexOf("-i");
|
const iIdx = args.indexOf("-i");
|
||||||
expect(ssIdx).toBeGreaterThan(-1);
|
expect(ssIdx).toBeGreaterThan(-1);
|
||||||
expect(args[ssIdx + 1]).toBe("42");
|
expect(args[ssIdx + 1]).toBe("42");
|
||||||
expect(ssIdx).toBeLessThan(iIdx);
|
expect(ssIdx).toBeGreaterThan(iIdx);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("does not insert -ss when seekSeconds is 0", () => {
|
it("does not insert -ss when seekSeconds is 0", () => {
|
||||||
|
|||||||
+3
-1
@@ -103,8 +103,10 @@ export function buildFfmpegArgs(url: string, seekSeconds: number): string[] {
|
|||||||
"-reconnect_on_http_error", "4xx,5xx",
|
"-reconnect_on_http_error", "4xx,5xx",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
args.push("-i", url);
|
||||||
|
// Output-side seek (after -i): works on CDNs that reject Range/keyframe seeks (NetEase music.126.net).
|
||||||
if (seekSeconds > 0) args.push("-ss", String(seekSeconds));
|
if (seekSeconds > 0) args.push("-ss", String(seekSeconds));
|
||||||
args.push("-i", url, "-f", "s16le", "-ar", "48000", "-ac", "2", "-acodec", "pcm_s16le", "-");
|
args.push("-f", "s16le", "-ar", "48000", "-ac", "2", "-acodec", "pcm_s16le", "-");
|
||||||
|
|
||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1538,3 +1538,55 @@ describe("BotInstance live-queue persistence (#119)", () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("BotInstance Bilibili multi-P resolution", () => {
|
||||||
|
it("resolves multi-P search result to P1 with accurate duration and name", async () => {
|
||||||
|
const multiPSongDetail = {
|
||||||
|
id: "BV1multiP?p=1",
|
||||||
|
name: "测试视频 - P1 分P1",
|
||||||
|
artist: "UP主",
|
||||||
|
album: "",
|
||||||
|
duration: 100, // P1 duration
|
||||||
|
coverUrl: "",
|
||||||
|
platform: "bilibili" as const,
|
||||||
|
};
|
||||||
|
const mockBili = {
|
||||||
|
platform: "bilibili" as const,
|
||||||
|
search: vi.fn().mockResolvedValue({
|
||||||
|
songs: [{
|
||||||
|
id: "BV1multiP",
|
||||||
|
name: "测试视频",
|
||||||
|
artist: "UP主",
|
||||||
|
album: "",
|
||||||
|
duration: 300, // total duration in search
|
||||||
|
coverUrl: "",
|
||||||
|
platform: "bilibili",
|
||||||
|
}],
|
||||||
|
albums: [],
|
||||||
|
playlists: [],
|
||||||
|
}),
|
||||||
|
getSongDetail: vi.fn().mockResolvedValue(multiPSongDetail),
|
||||||
|
getSongUrl: vi.fn().mockResolvedValue({ url: "http://audio.test" }),
|
||||||
|
};
|
||||||
|
|
||||||
|
const ctx = {
|
||||||
|
config: { commandPrefix: "!" },
|
||||||
|
lastSearchResults: [] as any[],
|
||||||
|
getProvider: () => mockBili,
|
||||||
|
getProviderFor: () => mockBili,
|
||||||
|
};
|
||||||
|
|
||||||
|
const res = await (BotInstance.prototype as any).resolvePlayQuery.call(ctx, {
|
||||||
|
name: "play",
|
||||||
|
args: "测试视频",
|
||||||
|
rawArgs: ["测试视频"],
|
||||||
|
flags: new Set(),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.song).toBeDefined();
|
||||||
|
expect(res.song.id).toBe("BV1multiP?p=1");
|
||||||
|
expect(res.song.name).toBe("测试视频 - P1 分P1");
|
||||||
|
expect(res.song.duration).toBe(100);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
+33
-4
@@ -295,7 +295,13 @@ export class BotInstance extends EventEmitter {
|
|||||||
const relPath = this.database.getCustomAvatarPath(this.id);
|
const relPath = this.database.getCustomAvatarPath(this.id);
|
||||||
if (relPath) {
|
if (relPath) {
|
||||||
const buf = this.avatarStore.read(relPath);
|
const buf = this.avatarStore.read(relPath);
|
||||||
if (buf) this.profileManager.setCustomAvatar(buf);
|
// loadCustomAvatar, NOT setCustomAvatar (#148): we are still in the
|
||||||
|
// constructor, so tsClient has not connected. setCustomAvatar would
|
||||||
|
// start a file transfer right here and fail. profileManager.onConnect()
|
||||||
|
// uploads it for real once the handshake completes.
|
||||||
|
// `length > 0` because avatarStore.write is delete-then-write, so a
|
||||||
|
// crash mid-write leaves a 0-byte file that is truthy as a Buffer.
|
||||||
|
if (buf && buf.length > 0) this.profileManager.loadCustomAvatar(buf);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.logger.warn({ err }, "Failed to load custom avatar — skipping");
|
this.logger.warn({ err }, "Failed to load custom avatar — skipping");
|
||||||
@@ -450,11 +456,16 @@ export class BotInstance extends EventEmitter {
|
|||||||
this.voiceDucking.removeSpeaker(event.id);
|
this.voiceDucking.removeSpeaker(event.id);
|
||||||
void this.refreshOccupancy();
|
void this.refreshOccupancy();
|
||||||
});
|
});
|
||||||
this.tsClient.on("clientMoved", (event: { id: number }) => {
|
this.tsClient.on("clientMoved", (event: { id: number; targetChannelID: bigint }) => {
|
||||||
if (event.id === this.tsClient.getClientId()) {
|
if (event.id === this.tsClient.getClientId()) {
|
||||||
// Moving the bot invalidates every activity deadline from its old
|
// Moving the bot invalidates every activity deadline from its old
|
||||||
// channel even if no individual leave events arrive.
|
// channel even if no individual leave events arrive.
|
||||||
this.voiceDucking.reset(false);
|
this.voiceDucking.reset(false);
|
||||||
|
// Carry the now-playing channel description over to the new
|
||||||
|
// channel instead of leaving it stale in the old one (#159).
|
||||||
|
this.profileManager.onChannelMoved(event.targetChannelID).catch((err) => {
|
||||||
|
this.logger.warn({ err }, "Channel description move update failed");
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
this.voiceDucking.removeSpeaker(event.id);
|
this.voiceDucking.removeSpeaker(event.id);
|
||||||
}
|
}
|
||||||
@@ -962,6 +973,14 @@ export class BotInstance extends EventEmitter {
|
|||||||
this.voteSkipUsers.clear();
|
this.voteSkipUsers.clear();
|
||||||
const provider = this.getProviderFor(song.platform);
|
const provider = this.getProviderFor(song.platform);
|
||||||
try {
|
try {
|
||||||
|
if (song.platform === "bilibili" && (!song.id.includes("?p=") || song.duration === 0)) {
|
||||||
|
const detail = await provider.getSongDetail(song.id);
|
||||||
|
if (detail) {
|
||||||
|
song.duration = detail.duration;
|
||||||
|
song.name = detail.name;
|
||||||
|
song.id = detail.id;
|
||||||
|
}
|
||||||
|
}
|
||||||
const result = await provider.getSongUrl(song.id);
|
const result = await provider.getSongUrl(song.id);
|
||||||
if (!result?.url) {
|
if (!result?.url) {
|
||||||
this.logger.warn({ songId: song.id, name: song.name }, "No URL available, skipping");
|
this.logger.warn({ songId: song.id, name: song.name }, "No URL available, skipping");
|
||||||
@@ -1128,7 +1147,12 @@ export class BotInstance extends EventEmitter {
|
|||||||
return { error: `No recent search. Use ${p}search <name> first.` };
|
return { error: `No recent search. Use ${p}search <name> first.` };
|
||||||
if (sel > this.lastSearchResults.length)
|
if (sel > this.lastSearchResults.length)
|
||||||
return { error: `Invalid selection #${sel}. ${p}search returned ${this.lastSearchResults.length} results.` };
|
return { error: `Invalid selection #${sel}. ${p}search returned ${this.lastSearchResults.length} results.` };
|
||||||
return { song: this.lastSearchResults[sel - 1] };
|
let song = this.lastSearchResults[sel - 1];
|
||||||
|
if (song.platform === "bilibili") {
|
||||||
|
const detail = await this.getProviderFor("bilibili").getSongDetail(song.id);
|
||||||
|
if (detail) song = { ...detail, platform: "bilibili" };
|
||||||
|
}
|
||||||
|
return { song };
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2) id/URL — fetch that exact song.
|
// 2) id/URL — fetch that exact song.
|
||||||
@@ -1145,7 +1169,12 @@ export class BotInstance extends EventEmitter {
|
|||||||
const provider = this.getProvider(cmd.flags);
|
const provider = this.getProvider(cmd.flags);
|
||||||
const result = await provider.search(args, 1);
|
const result = await provider.search(args, 1);
|
||||||
if (result.songs.length === 0) return { error: `No results found for: ${args}` };
|
if (result.songs.length === 0) return { error: `No results found for: ${args}` };
|
||||||
return { song: { ...result.songs[0], platform: provider.platform } };
|
let song = result.songs[0];
|
||||||
|
if (provider.platform === "bilibili") {
|
||||||
|
const detail = await provider.getSongDetail(song.id);
|
||||||
|
if (detail) song = detail;
|
||||||
|
}
|
||||||
|
return { song: { ...song, platform: provider.platform } };
|
||||||
}
|
}
|
||||||
|
|
||||||
private async cmdSearch(cmd: ParsedCommand): Promise<string> {
|
private async cmdSearch(cmd: ParsedCommand): Promise<string> {
|
||||||
|
|||||||
@@ -145,3 +145,122 @@ describe("BotProfileManager custom avatar precedence", () => {
|
|||||||
expect(ts.clearCalls).toBe(0);
|
expect(ts.clearCalls).toBe(0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// #148: the persisted avatar is loaded in the BotInstance constructor, before
|
||||||
|
// tsClient.connect() has run. Loading it must not touch the wire at all.
|
||||||
|
describe("BotProfileManager loadCustomAvatar (pre-connect load, #148)", () => {
|
||||||
|
let ts: ReturnType<typeof makeMockTs>;
|
||||||
|
beforeEach(() => { ts = makeMockTs(); });
|
||||||
|
|
||||||
|
it("does not upload or clear anything when called before connect", async () => {
|
||||||
|
const pm = new BotProfileManager(ts as any, noopLogger, cfgOn, "Bot");
|
||||||
|
pm.loadCustomAvatar(Buffer.from([7, 7, 7]));
|
||||||
|
await flush();
|
||||||
|
expect(ts.uploadCalls.length).toBe(0);
|
||||||
|
expect(ts.clearCalls).toBe(0);
|
||||||
|
expect(ts.fileTransferInitUpload).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("the loaded avatar is uploaded once onConnect fires", async () => {
|
||||||
|
const pm = new BotProfileManager(ts as any, noopLogger, cfgOn, "Bot");
|
||||||
|
pm.loadCustomAvatar(Buffer.from([7, 7, 7]));
|
||||||
|
await flush();
|
||||||
|
pm.onConnect();
|
||||||
|
await flush();
|
||||||
|
expect(ts.uploadCalls.length).toBe(1);
|
||||||
|
expect(ts.uploadCalls[0].equals(Buffer.from([7, 7, 7]))).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("survives a reconnect: onConnect re-applies the loaded avatar every time", async () => {
|
||||||
|
const pm = new BotProfileManager(ts as any, noopLogger, cfgOn, "Bot");
|
||||||
|
pm.loadCustomAvatar(Buffer.from([8]));
|
||||||
|
pm.onConnect();
|
||||||
|
await flush();
|
||||||
|
pm.onConnect();
|
||||||
|
await flush();
|
||||||
|
expect(ts.uploadCalls.length).toBe(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("loading null leaves the wire untouched and onConnect stays quiet", async () => {
|
||||||
|
const pm = new BotProfileManager(ts as any, noopLogger, cfgOn, "Bot");
|
||||||
|
pm.loadCustomAvatar(null);
|
||||||
|
pm.onConnect();
|
||||||
|
await flush();
|
||||||
|
expect(ts.uploadCalls.length).toBe(0);
|
||||||
|
expect(ts.clearCalls).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("setCustomAvatar still uploads immediately after connect (post-connect edit unchanged)", async () => {
|
||||||
|
const pm = new BotProfileManager(ts as any, noopLogger, cfgOn, "Bot");
|
||||||
|
pm.loadCustomAvatar(Buffer.from([1]));
|
||||||
|
pm.onConnect();
|
||||||
|
await flush();
|
||||||
|
ts.uploadCalls.length = 0;
|
||||||
|
pm.setCustomAvatar(Buffer.from([2, 2]));
|
||||||
|
await flush();
|
||||||
|
expect(ts.uploadCalls.length).toBe(1);
|
||||||
|
expect(ts.uploadCalls[0].equals(Buffer.from([2, 2]))).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("BotProfileManager channel description follows the bot (#159)", () => {
|
||||||
|
const cfgChannelDesc = { ...cfgOff, channelDescEnabled: true };
|
||||||
|
let ts: ReturnType<typeof makeMockTs> & { cid: bigint };
|
||||||
|
let channelEdits: () => string[];
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
ts = makeMockTs() as any;
|
||||||
|
ts.cid = 5n;
|
||||||
|
(ts as any).getChannelId = () => ts.cid;
|
||||||
|
channelEdits = () =>
|
||||||
|
(ts.sendCommandNoWait as any).mock.calls
|
||||||
|
.map((c: any[]) => c[0] as string)
|
||||||
|
.filter((cmd: string) => cmd.startsWith("channeledit"));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("clears the old channel and fills the new one when moved while playing", async () => {
|
||||||
|
const pm = new BotProfileManager(ts as any, noopLogger, cfgChannelDesc, "Bot");
|
||||||
|
await pm.onSongChange(fakeSong);
|
||||||
|
expect(channelEdits()).toEqual([
|
||||||
|
expect.stringMatching(/^channeledit cid=5 channel_description=\S+/),
|
||||||
|
]);
|
||||||
|
|
||||||
|
ts.cid = 9n;
|
||||||
|
await pm.onChannelMoved(9n);
|
||||||
|
|
||||||
|
const edits = channelEdits();
|
||||||
|
expect(edits[1]).toBe("channeledit cid=5 channel_description=");
|
||||||
|
expect(edits[2]).toMatch(/^channeledit cid=9 channel_description=\S+/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("stopping after a move clears the channel the bot is in now, not the old one", async () => {
|
||||||
|
const pm = new BotProfileManager(ts as any, noopLogger, cfgChannelDesc, "Bot");
|
||||||
|
await pm.onSongChange(fakeSong);
|
||||||
|
ts.cid = 9n;
|
||||||
|
await pm.onChannelMoved(9n);
|
||||||
|
await pm.onSongChange(null);
|
||||||
|
expect(channelEdits().at(-1)).toBe("channeledit cid=9 channel_description=");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("a move while idle touches no channel description", async () => {
|
||||||
|
const pm = new BotProfileManager(ts as any, noopLogger, cfgChannelDesc, "Bot");
|
||||||
|
ts.cid = 9n;
|
||||||
|
await pm.onChannelMoved(9n);
|
||||||
|
expect(channelEdits()).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("a move is ignored when the channel description feature is off", async () => {
|
||||||
|
const pm = new BotProfileManager(ts as any, noopLogger, cfgOff, "Bot");
|
||||||
|
await pm.onSongChange(fakeSong);
|
||||||
|
ts.cid = 9n;
|
||||||
|
await pm.onChannelMoved(9n);
|
||||||
|
expect(channelEdits()).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("an event for the channel the description is already in is a no-op", async () => {
|
||||||
|
const pm = new BotProfileManager(ts as any, noopLogger, cfgChannelDesc, "Bot");
|
||||||
|
await pm.onSongChange(fakeSong);
|
||||||
|
await pm.onChannelMoved(5n);
|
||||||
|
expect(channelEdits()).toHaveLength(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
+61
-5
@@ -32,6 +32,13 @@ export class BotProfileManager {
|
|||||||
* pushed immediately (idle) or wait for the next stop event (playing).
|
* pushed immediately (idle) or wait for the next stop event (playing).
|
||||||
*/
|
*/
|
||||||
private currentSong: QueuedSong | null = null;
|
private currentSong: QueuedSong | null = null;
|
||||||
|
/**
|
||||||
|
* Channel whose description currently holds our now-playing text, or null
|
||||||
|
* if we have not written one. Remembered so that when the bot is moved we
|
||||||
|
* can still clean up the channel it was taken out of (#159) — by then
|
||||||
|
* getChannelId() already reports the new channel.
|
||||||
|
*/
|
||||||
|
private channelDescCid: bigint | null = null;
|
||||||
|
|
||||||
/** Per-feature permission-denied flags. Reset on reconnect. */
|
/** Per-feature permission-denied flags. Reset on reconnect. */
|
||||||
private permDenied = {
|
private permDenied = {
|
||||||
@@ -65,6 +72,20 @@ export class BotProfileManager {
|
|||||||
|
|
||||||
// --- Public API ---
|
// --- Public API ---
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store a persisted custom avatar WITHOUT touching TeamSpeak (#148).
|
||||||
|
*
|
||||||
|
* Used during BotInstance construction, when the TS connection does not
|
||||||
|
* exist yet: setCustomAvatar would immediately fire the three-step file
|
||||||
|
* transfer (fileTransferInitUpload → uploadFileData → clientupdate) against
|
||||||
|
* a client that has not connected, so the upload always failed and the
|
||||||
|
* saved avatar never appeared. onConnect() re-applies this.customAvatar
|
||||||
|
* once the handshake completes, so loading it silently here loses nothing.
|
||||||
|
*/
|
||||||
|
loadCustomAvatar(buffer: Buffer | null): void {
|
||||||
|
this.customAvatar = buffer;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set/clear the persistent idle avatar. Pass null to remove.
|
* Set/clear the persistent idle avatar. Pass null to remove.
|
||||||
*
|
*
|
||||||
@@ -120,6 +141,8 @@ export class BotProfileManager {
|
|||||||
onConnect(): void {
|
onConnect(): void {
|
||||||
this.generation++;
|
this.generation++;
|
||||||
this.currentSong = null;
|
this.currentSong = null;
|
||||||
|
// Channel ids are per-server; never carry one across a (re)connect.
|
||||||
|
this.channelDescCid = null;
|
||||||
this.permDenied = {
|
this.permDenied = {
|
||||||
avatar: false,
|
avatar: false,
|
||||||
description: false,
|
description: false,
|
||||||
@@ -136,6 +159,31 @@ export class BotProfileManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when the bot itself has been moved to another channel (#159).
|
||||||
|
* Clears the now-playing text from the channel it left and, if a song is
|
||||||
|
* playing, writes it to the channel it is in now.
|
||||||
|
*/
|
||||||
|
async onChannelMoved(newChannelId: bigint): Promise<void> {
|
||||||
|
if (!this.config.channelDescEnabled || this.permDenied.channelDesc) return;
|
||||||
|
const oldChannelId = this.channelDescCid;
|
||||||
|
if (oldChannelId === newChannelId) return;
|
||||||
|
try {
|
||||||
|
if (oldChannelId !== null) {
|
||||||
|
await this.tsClient.sendCommandNoWait(
|
||||||
|
`channeledit cid=${oldChannelId} channel_description=`,
|
||||||
|
);
|
||||||
|
this.channelDescCid = null;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
this.handleFeatureError("channelDesc", err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (this.currentSong) {
|
||||||
|
await this.updateChannelDescription(this.currentSong, newChannelId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
getConfig(): ProfileConfig {
|
getConfig(): ProfileConfig {
|
||||||
return { ...this.config };
|
return { ...this.config };
|
||||||
}
|
}
|
||||||
@@ -386,18 +434,25 @@ export class BotProfileManager {
|
|||||||
return str.slice(0, end) + ellipsis;
|
return str.slice(0, end) + ellipsis;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async updateChannelDescription(song: QueuedSong | null): Promise<void> {
|
private async updateChannelDescription(
|
||||||
|
song: QueuedSong | null,
|
||||||
|
targetChannelId?: bigint,
|
||||||
|
): Promise<void> {
|
||||||
if (!this.config.channelDescEnabled || this.permDenied.channelDesc) return;
|
if (!this.config.channelDescEnabled || this.permDenied.channelDesc) return;
|
||||||
try {
|
try {
|
||||||
const channelId = this.tsClient.getChannelId();
|
const channelId = targetChannelId ?? this.tsClient.getChannelId();
|
||||||
if (channelId === 0n) return; // unknown channel
|
|
||||||
|
|
||||||
if (!song) {
|
if (!song) {
|
||||||
|
// Prefer the channel we actually wrote to, in case a move event
|
||||||
|
// was missed and the bot is somewhere else now.
|
||||||
|
const target = this.channelDescCid ?? channelId;
|
||||||
|
if (target === 0n) return; // unknown channel
|
||||||
await this.tsClient.sendCommandNoWait(
|
await this.tsClient.sendCommandNoWait(
|
||||||
`channeledit cid=${channelId} channel_description=`,
|
`channeledit cid=${target} channel_description=`,
|
||||||
);
|
);
|
||||||
|
this.channelDescCid = null;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (channelId === 0n) return; // unknown channel
|
||||||
|
|
||||||
const lines = [
|
const lines = [
|
||||||
`\u266A \u6B63\u5728\u64AD\u653E: ${song.name} - ${song.artist}`, // ♪ 正在播放:
|
`\u266A \u6B63\u5728\u64AD\u653E: ${song.name} - ${song.artist}`, // ♪ 正在播放:
|
||||||
@@ -408,6 +463,7 @@ export class BotProfileManager {
|
|||||||
await this.tsClient.sendCommandNoWait(
|
await this.tsClient.sendCommandNoWait(
|
||||||
`channeledit cid=${channelId} channel_description=${escapeTS3(desc)}`,
|
`channeledit cid=${channelId} channel_description=${escapeTS3(desc)}`,
|
||||||
);
|
);
|
||||||
|
this.channelDescCid = channelId;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.handleFeatureError("channelDesc", err);
|
this.handleFeatureError("channelDesc", err);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,3 +37,128 @@ describe("BiliBiliProvider.search pagination", () => {
|
|||||||
expect(searchParams(get).page).toBe("1");
|
expect(searchParams(get).page).toBe("1");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("BiliBiliProvider multi-P support", () => {
|
||||||
|
it("parseBilibiliId extracts bvid and page correctly", async () => {
|
||||||
|
const { parseBilibiliId } = await import("./bilibili.js");
|
||||||
|
expect(parseBilibiliId("BV1yxHQeYEuE")).toEqual({ bvid: "BV1yxHQeYEuE", page: 1 });
|
||||||
|
expect(parseBilibiliId("BV1yxHQeYEuE?p=3")).toEqual({ bvid: "BV1yxHQeYEuE", page: 3 });
|
||||||
|
expect(parseBilibiliId("BV1yxHQeYEuE:p2")).toEqual({ bvid: "BV1yxHQeYEuE", page: 2 });
|
||||||
|
expect(parseBilibiliId("https://www.bilibili.com/video/BV1yxHQeYEuE?p=5")).toEqual({
|
||||||
|
bvid: "BV1yxHQeYEuE",
|
||||||
|
page: 5,
|
||||||
|
});
|
||||||
|
expect(parseBilibiliId("some-other-id")).toEqual({ bvid: "some-other-id", page: 1 });
|
||||||
|
});
|
||||||
|
|
||||||
|
function mockViewProvider(viewData: any, playUrlData?: any) {
|
||||||
|
const p = new BiliBiliProvider();
|
||||||
|
const get = vi.fn().mockImplementation((url: string, opts?: any) => {
|
||||||
|
if (url === "/x/web-interface/view") {
|
||||||
|
return Promise.resolve({ data: { data: viewData } });
|
||||||
|
}
|
||||||
|
if (url === "/x/player/playurl") {
|
||||||
|
return Promise.resolve({ data: { data: playUrlData ?? {} } });
|
||||||
|
}
|
||||||
|
return Promise.resolve({ data: {} });
|
||||||
|
});
|
||||||
|
(p as any).buvidInitialized = true;
|
||||||
|
(p as any).api = { get };
|
||||||
|
return { p, get };
|
||||||
|
}
|
||||||
|
|
||||||
|
const multiPViewData = {
|
||||||
|
bvid: "BV1multiP",
|
||||||
|
title: "测试多P教程",
|
||||||
|
pic: "http://i0.hdslb.com/bfs/archive/test.jpg",
|
||||||
|
duration: 300, // 总时长 300 秒 (120 + 180)
|
||||||
|
owner: { name: "UP主测试" },
|
||||||
|
pages: [
|
||||||
|
{ cid: 10001, page: 1, part: "第一讲 入门", duration: 120 },
|
||||||
|
{ cid: 10002, page: 2, part: "第二讲 进阶", duration: 180 },
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
const singlePViewData = {
|
||||||
|
bvid: "BV1singleP",
|
||||||
|
title: "测试单P视频",
|
||||||
|
pic: "http://i0.hdslb.com/bfs/archive/single.jpg",
|
||||||
|
duration: 200,
|
||||||
|
owner: { name: "UP主测试" },
|
||||||
|
pages: [
|
||||||
|
{ cid: 20001, page: 1, part: "测试单P视频", duration: 200 },
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
it("getSongDetail for single-P video returns total duration and clean bvid", async () => {
|
||||||
|
const { p } = mockViewProvider(singlePViewData);
|
||||||
|
const song = await p.getSongDetail("BV1singleP");
|
||||||
|
expect(song).not.toBeNull();
|
||||||
|
expect(song!.id).toBe("BV1singleP");
|
||||||
|
expect(song!.name).toBe("测试单P视频");
|
||||||
|
expect(song!.duration).toBe(200);
|
||||||
|
expect(song!.platform).toBe("bilibili");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("getSongDetail for multi-P video without ?p defaults to P1 with P1 duration", async () => {
|
||||||
|
const { p } = mockViewProvider(multiPViewData);
|
||||||
|
const song = await p.getSongDetail("BV1multiP");
|
||||||
|
expect(song).not.toBeNull();
|
||||||
|
expect(song!.id).toBe("BV1multiP?p=1");
|
||||||
|
expect(song!.name).toBe("测试多P教程 - P1 第一讲 入门");
|
||||||
|
expect(song!.duration).toBe(120); // P1 独立时长,而非总时长 300!
|
||||||
|
expect(song!.platform).toBe("bilibili");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("getSongDetail for multi-P video with ?p=2 returns P2 with P2 duration", async () => {
|
||||||
|
const { p } = mockViewProvider(multiPViewData);
|
||||||
|
const song = await p.getSongDetail("BV1multiP?p=2");
|
||||||
|
expect(song).not.toBeNull();
|
||||||
|
expect(song!.id).toBe("BV1multiP?p=2");
|
||||||
|
expect(song!.name).toBe("测试多P教程 - P2 第二讲 进阶");
|
||||||
|
expect(song!.duration).toBe(180); // P2 独立时长
|
||||||
|
expect(song!.platform).toBe("bilibili");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("getVideoParts returns all parts with duration and cid", async () => {
|
||||||
|
const { p } = mockViewProvider(multiPViewData);
|
||||||
|
const partsResult = await p.getVideoParts("BV1multiP");
|
||||||
|
expect(partsResult).not.toBeNull();
|
||||||
|
expect(partsResult!.bvid).toBe("BV1multiP");
|
||||||
|
expect(partsResult!.title).toBe("测试多P教程");
|
||||||
|
expect(partsResult!.parts).toHaveLength(2);
|
||||||
|
expect(partsResult!.parts[0]).toEqual({
|
||||||
|
part: 1,
|
||||||
|
cid: 10001,
|
||||||
|
title: "第一讲 入门",
|
||||||
|
duration: 120,
|
||||||
|
});
|
||||||
|
expect(partsResult!.parts[1]).toEqual({
|
||||||
|
part: 2,
|
||||||
|
cid: 10002,
|
||||||
|
title: "第二讲 进阶",
|
||||||
|
duration: 180,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("getSongUrl requests playurl with correct cid for specific part", async () => {
|
||||||
|
const playUrlResponse = {
|
||||||
|
dash: {
|
||||||
|
audio: [
|
||||||
|
{ bandwidth: 64000, baseUrl: "http://audio.64k.test" },
|
||||||
|
{ bandwidth: 320000, baseUrl: "http://audio.320k.test" },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const { p, get } = mockViewProvider(multiPViewData, playUrlResponse);
|
||||||
|
|
||||||
|
const result = await p.getSongUrl("BV1multiP?p=2");
|
||||||
|
expect(result).not.toBeNull();
|
||||||
|
expect(result!.url).toBe("http://audio.320k.test");
|
||||||
|
|
||||||
|
const playurlCall = get.mock.calls.find((c: any[]) => c[0] === "/x/player/playurl");
|
||||||
|
expect(playurlCall).toBeTruthy();
|
||||||
|
expect(playurlCall![1].params.cid).toBe(10002); // 准确传入 P2 的 cid
|
||||||
|
expect(playurlCall![1].params.bvid).toBe("BV1multiP"); // 纯净 bvid
|
||||||
|
});
|
||||||
|
});
|
||||||
+106
-11
@@ -27,6 +27,38 @@ const WBI_MIXIN_KEY_ENC_TAB = [
|
|||||||
|
|
||||||
const WBI_KEY_TTL_MS = 6 * 60 * 60 * 1000; // wbi keys rotate ~daily; refresh every 6h
|
const WBI_KEY_TTL_MS = 6 * 60 * 60 * 1000; // wbi keys rotate ~daily; refresh every 6h
|
||||||
|
|
||||||
|
export interface BiliVideoPart {
|
||||||
|
part: number;
|
||||||
|
cid: number;
|
||||||
|
title: string;
|
||||||
|
duration: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface BiliVideoPartsResult {
|
||||||
|
bvid: string;
|
||||||
|
title: string;
|
||||||
|
coverUrl: string;
|
||||||
|
artist: string;
|
||||||
|
parts: BiliVideoPart[];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析带有分P信息的 B站 ID 或 URL。
|
||||||
|
* 支持形如 "BVxxxx", "BVxxxx?p=2", "BVxxxx:p2" 以及完整 URL 等格式,默认 page 为 1。
|
||||||
|
*/
|
||||||
|
export function parseBilibiliId(songId: string): { bvid: string; page: number } {
|
||||||
|
const str = (songId ?? "").trim();
|
||||||
|
const bvMatch = str.match(/BV[0-9A-Za-z]+/i);
|
||||||
|
if (!bvMatch) {
|
||||||
|
return { bvid: str, page: 1 };
|
||||||
|
}
|
||||||
|
const bvid = bvMatch[0];
|
||||||
|
const pageMatch = str.match(/[?&]p=(\d+)|:p?(\d+)/i);
|
||||||
|
const pageStr = pageMatch ? (pageMatch[1] ?? pageMatch[2]) : undefined;
|
||||||
|
const page = pageStr ? parseInt(pageStr, 10) : 1;
|
||||||
|
return { bvid, page: Math.max(1, page) };
|
||||||
|
}
|
||||||
|
|
||||||
export class BiliBiliProvider implements MusicProvider {
|
export class BiliBiliProvider implements MusicProvider {
|
||||||
readonly platform = "bilibili" as const;
|
readonly platform = "bilibili" as const;
|
||||||
private api: AxiosInstance;
|
private api: AxiosInstance;
|
||||||
@@ -192,18 +224,41 @@ export class BiliBiliProvider implements MusicProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getSongDetail(songId: string): Promise<Song | null> {
|
async getSongDetail(songId: string): Promise<Song | null> {
|
||||||
|
const { bvid, page } = parseBilibiliId(songId);
|
||||||
try {
|
try {
|
||||||
const res = await this.api.get("/x/web-interface/view", {
|
const res = await this.api.get("/x/web-interface/view", {
|
||||||
params: { bvid: songId },
|
params: { bvid },
|
||||||
headers: this.cookieHeaders,
|
headers: this.cookieHeaders,
|
||||||
});
|
});
|
||||||
|
|
||||||
const data = res.data?.data;
|
const data = res.data?.data;
|
||||||
if (!data) return null;
|
if (!data) return null;
|
||||||
|
|
||||||
// Cache cid for later audio URL fetching
|
const pages = data.pages ?? [];
|
||||||
if (data.pages?.[0]?.cid) {
|
// 缓存所有分P的 cid 映射
|
||||||
this.cidCache.set(songId, data.pages[0].cid);
|
for (const p of pages) {
|
||||||
|
this.cidCache.set(`${bvid}?p=${p.page}`, p.cid);
|
||||||
|
}
|
||||||
|
if (pages[0]?.cid) {
|
||||||
|
this.cidCache.set(bvid, pages[0].cid);
|
||||||
|
}
|
||||||
|
|
||||||
|
const targetPage = pages.find((p: any) => p.page === page) ?? pages[0];
|
||||||
|
|
||||||
|
// 若为多P视频,返回对应分P的名称与独立时长
|
||||||
|
if (pages.length > 1 && targetPage) {
|
||||||
|
const partTitle = targetPage.part && targetPage.part !== data.title
|
||||||
|
? `${data.title} - P${targetPage.page} ${targetPage.part}`
|
||||||
|
: `${data.title} (P${targetPage.page})`;
|
||||||
|
return {
|
||||||
|
id: `${bvid}?p=${targetPage.page}`,
|
||||||
|
name: partTitle,
|
||||||
|
artist: data.owner?.name ?? "",
|
||||||
|
album: "",
|
||||||
|
duration: targetPage.duration ?? 0,
|
||||||
|
coverUrl: this.normalizeCover(data.pic ?? ""),
|
||||||
|
platform: "bilibili" as const,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -211,7 +266,7 @@ export class BiliBiliProvider implements MusicProvider {
|
|||||||
name: data.title ?? "",
|
name: data.title ?? "",
|
||||||
artist: data.owner?.name ?? "",
|
artist: data.owner?.name ?? "",
|
||||||
album: "",
|
album: "",
|
||||||
duration: data.duration ?? 0,
|
duration: targetPage?.duration ?? data.duration ?? 0,
|
||||||
coverUrl: this.normalizeCover(data.pic ?? ""),
|
coverUrl: this.normalizeCover(data.pic ?? ""),
|
||||||
platform: "bilibili" as const,
|
platform: "bilibili" as const,
|
||||||
};
|
};
|
||||||
@@ -220,9 +275,47 @@ export class BiliBiliProvider implements MusicProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 获取视频所有分P列表 */
|
||||||
|
async getVideoParts(bvid: string): Promise<BiliVideoPartsResult | null> {
|
||||||
|
const { bvid: cleanBvid } = parseBilibiliId(bvid);
|
||||||
|
try {
|
||||||
|
const res = await this.api.get("/x/web-interface/view", {
|
||||||
|
params: { bvid: cleanBvid },
|
||||||
|
headers: this.cookieHeaders,
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = res.data?.data;
|
||||||
|
if (!data) return null;
|
||||||
|
|
||||||
|
const pages = data.pages ?? [];
|
||||||
|
for (const p of pages) {
|
||||||
|
this.cidCache.set(`${cleanBvid}?p=${p.page}`, p.cid);
|
||||||
|
}
|
||||||
|
if (pages[0]?.cid) {
|
||||||
|
this.cidCache.set(cleanBvid, pages[0].cid);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
bvid: cleanBvid,
|
||||||
|
title: data.title ?? "",
|
||||||
|
coverUrl: this.normalizeCover(data.pic ?? ""),
|
||||||
|
artist: data.owner?.name ?? "",
|
||||||
|
parts: pages.map((p: any) => ({
|
||||||
|
part: p.page,
|
||||||
|
cid: p.cid,
|
||||||
|
title: p.part ?? `P${p.page}`,
|
||||||
|
duration: p.duration ?? 0,
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Get CID for a bvid, using cache when available */
|
/** Get CID for a bvid, using cache when available */
|
||||||
private async getCid(bvid: string): Promise<number | null> {
|
private async getCid(bvid: string, page = 1): Promise<number | null> {
|
||||||
const cached = this.cidCache.get(bvid);
|
const key = page > 1 ? `${bvid}?p=${page}` : bvid;
|
||||||
|
const cached = this.cidCache.get(key) ?? (page === 1 ? this.cidCache.get(`${bvid}?p=1`) : undefined);
|
||||||
if (cached) return cached;
|
if (cached) return cached;
|
||||||
|
|
||||||
// Limit cache size to prevent unbounded growth
|
// Limit cache size to prevent unbounded growth
|
||||||
@@ -231,20 +324,22 @@ export class BiliBiliProvider implements MusicProvider {
|
|||||||
if (firstKey) this.cidCache.delete(firstKey);
|
if (firstKey) this.cidCache.delete(firstKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
const detail = await this.getSongDetail(bvid);
|
const songId = page > 1 ? `${bvid}?p=${page}` : bvid;
|
||||||
|
const detail = await this.getSongDetail(songId);
|
||||||
if (!detail) return null;
|
if (!detail) return null;
|
||||||
return this.cidCache.get(bvid) ?? null;
|
return this.cidCache.get(key) ?? this.cidCache.get(`${bvid}?p=${page}`) ?? this.cidCache.get(bvid) ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getSongUrl(songId: string, _quality?: string): Promise<SongUrlResult | null> {
|
async getSongUrl(songId: string, _quality?: string): Promise<SongUrlResult | null> {
|
||||||
const cid = await this.getCid(songId);
|
const { bvid, page } = parseBilibiliId(songId);
|
||||||
|
const cid = await this.getCid(bvid, page);
|
||||||
if (!cid) return null;
|
if (!cid) return null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await this.api.get("/x/player/playurl", {
|
const res = await this.api.get("/x/player/playurl", {
|
||||||
params: {
|
params: {
|
||||||
cid,
|
cid,
|
||||||
bvid: songId,
|
bvid,
|
||||||
fnval: 16, // DASH format
|
fnval: 16, // DASH format
|
||||||
},
|
},
|
||||||
headers: this.cookieHeaders,
|
headers: this.cookieHeaders,
|
||||||
|
|||||||
@@ -0,0 +1,109 @@
|
|||||||
|
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
|
||||||
|
// unlinkSync/rmdirSync are NOT mocked below, so the test's own fixture
|
||||||
|
// teardown is unaffected by the simulated lock on *.mp4.
|
||||||
|
import { mkdtempSync, statSync, existsSync, readFileSync, unlinkSync, readdirSync, rmdirSync } from "node:fs";
|
||||||
|
import { spawnSync } from "node:child_process";
|
||||||
|
import { createRequire } from "node:module";
|
||||||
|
import { tmpdir } from "node:os";
|
||||||
|
import { join } from "node:path";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* #149: when the audio track is extracted successfully but the source video
|
||||||
|
* cannot be deleted (Windows keeps files locked briefly — rmSync with
|
||||||
|
* force:true still throws EBUSY/EPERM), the record must fall back to the
|
||||||
|
* ORIGINAL container completely: both the path AND the recorded size.
|
||||||
|
*
|
||||||
|
* Committing the size before the delete succeeded would leave the record
|
||||||
|
* claiming the small extracted size while still holding the whole video, so
|
||||||
|
* totalBytes() under-counts and the upload directory grows past its quota.
|
||||||
|
*
|
||||||
|
* This lives in its own file because it partially mocks node:fs, which would
|
||||||
|
* otherwise leak into every other test in local.test.ts.
|
||||||
|
*/
|
||||||
|
vi.mock("node:fs", async (importOriginal) => {
|
||||||
|
const actual = await importOriginal<typeof import("node:fs")>();
|
||||||
|
return {
|
||||||
|
...actual,
|
||||||
|
default: actual,
|
||||||
|
rmSync: (path: string, opts?: object) => {
|
||||||
|
// Simulate the lock on the source video only; every other delete
|
||||||
|
// (the discarded .mka, temp dirs, the reject path) behaves normally.
|
||||||
|
if (typeof path === "string" && path.endsWith(".mp4")) {
|
||||||
|
const err = new Error("EBUSY: resource busy or locked") as NodeJS.ErrnoException;
|
||||||
|
err.code = "EBUSY";
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
return actual.rmSync(path, opts as never);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
const { LocalMusicProvider } = await import("./local.js");
|
||||||
|
|
||||||
|
const ffmpeg: string | null = (() => {
|
||||||
|
try {
|
||||||
|
return createRequire(import.meta.url)("ffmpeg-static") as string;
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
const have = !!ffmpeg && spawnSync(ffmpeg, ["-version"], { stdio: "ignore" }).status === 0;
|
||||||
|
|
||||||
|
let dir: string;
|
||||||
|
beforeEach(() => { dir = mkdtempSync(join(tmpdir(), "local-extract-fallback-")); });
|
||||||
|
afterEach(() => {
|
||||||
|
// Recursive teardown without rmSync (mocked above for *.mp4).
|
||||||
|
for (const f of readdirSync(dir)) {
|
||||||
|
try { unlinkSync(join(dir, f)); } catch { /* best effort */ }
|
||||||
|
}
|
||||||
|
try { rmdirSync(dir); } catch { /* best effort */ }
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("LocalMusicProvider: source video cannot be deleted after extraction (#149)", () => {
|
||||||
|
it.runIf(have)("keeps the original container AND its real size, not the extracted size", async () => {
|
||||||
|
const src = join(dir, "fixture.mp4");
|
||||||
|
const r = spawnSync(ffmpeg!, [
|
||||||
|
"-y", "-hide_banner", "-loglevel", "error",
|
||||||
|
"-f", "lavfi", "-i", "testsrc=s=320x240:r=25:d=3",
|
||||||
|
"-f", "lavfi", "-i", "sine=f=440:d=3",
|
||||||
|
"-c:v", "libx264", "-b:v", "800k", "-c:a", "aac", "-shortest", src,
|
||||||
|
], { stdio: "ignore" });
|
||||||
|
expect(r.status).toBe(0);
|
||||||
|
|
||||||
|
const bytes = readFileSync(src);
|
||||||
|
unlinkSync(src); // uploadAudio writes its own copy under a uuid name
|
||||||
|
|
||||||
|
const p = new LocalMusicProvider(dir);
|
||||||
|
const song = await p.uploadAudio({
|
||||||
|
buffer: bytes, originalName: "fixture.mp4", mimeType: "video/mp4",
|
||||||
|
});
|
||||||
|
|
||||||
|
const resolved = await p.getSongUrl(song.id);
|
||||||
|
expect(resolved).not.toBeNull();
|
||||||
|
|
||||||
|
// Fell back to the original container — the extract was discarded.
|
||||||
|
expect(resolved!.url.endsWith(".mp4")).toBe(true);
|
||||||
|
expect(existsSync(resolved!.url)).toBe(true);
|
||||||
|
expect(existsSync(resolved!.url.replace(/\.mp4$/, ".m4a"))).toBe(false);
|
||||||
|
expect(existsSync(resolved!.url.replace(/\.mp4$/, ".mka"))).toBe(false);
|
||||||
|
|
||||||
|
const onDisk = statSync(resolved!.url).size;
|
||||||
|
expect(onDisk).toBe(bytes.length);
|
||||||
|
|
||||||
|
// The RECORDED size drives the quota (totalBytes()), so it must describe
|
||||||
|
// the file actually retained. It is not exposed through search()/toSong,
|
||||||
|
// but it is persisted to index.json — read it back from there.
|
||||||
|
const record = (JSON.parse(readFileSync(join(dir, "index.json"), "utf8")) as Array<{
|
||||||
|
id: string; size: number; filePath: string;
|
||||||
|
}>).find((r) => r.id === song.id);
|
||||||
|
expect(record).toBeDefined();
|
||||||
|
expect(record!.filePath.endsWith(".mp4")).toBe(true);
|
||||||
|
// Before the fix this was the (much smaller) .mka size while the whole
|
||||||
|
// .mp4 stayed on disk, so the quota under-counted the retained bytes.
|
||||||
|
expect(record!.size).toBe(bytes.length);
|
||||||
|
|
||||||
|
// Sanity: the extract really is much smaller, so a wrong commit order
|
||||||
|
// would have been clearly observable rather than a rounding error.
|
||||||
|
expect(onDisk).toBeGreaterThan(50_000);
|
||||||
|
}, 60000);
|
||||||
|
});
|
||||||
+257
-2
@@ -1,8 +1,11 @@
|
|||||||
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
||||||
import { mkdtempSync, rmSync, existsSync, writeFileSync } from "node:fs";
|
import { mkdtempSync, rmSync, existsSync, writeFileSync, readFileSync, readdirSync, statSync } from "node:fs";
|
||||||
|
import { spawnSync } from "node:child_process";
|
||||||
|
import { createRequire } from "node:module";
|
||||||
|
import { buildFfmpegArgs } from "../audio/player.js";
|
||||||
import { tmpdir } from "node:os";
|
import { tmpdir } from "node:os";
|
||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
import { LocalMusicProvider } from "./local.js";
|
import { LocalMusicProvider, parseMediaProbe } from "./local.js";
|
||||||
|
|
||||||
let dir: string;
|
let dir: string;
|
||||||
|
|
||||||
@@ -149,6 +152,115 @@ describe("LocalMusicProvider upload validation", () => {
|
|||||||
p.uploadAudio({ buffer: Buffer.alloc(0), originalName: "a.mp3" }),
|
p.uploadAudio({ buffer: Buffer.alloc(0), originalName: "a.mp3" }),
|
||||||
).rejects.toThrow();
|
).rejects.toThrow();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// #149: video containers are accepted; only their audio track is kept.
|
||||||
|
it("still rejects a non-media extension after video was allowed", async () => {
|
||||||
|
const p = new LocalMusicProvider(dir);
|
||||||
|
for (const name of ["evil.exe", "evil.html", "evil.mp4.txt", "notes.pdf"]) {
|
||||||
|
await expect(
|
||||||
|
p.uploadAudio({ buffer: Buffer.from("x"), originalName: name, mimeType: "video/mp4" }),
|
||||||
|
).rejects.toThrow();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("accepts every supported video extension at the extension gate", async () => {
|
||||||
|
const p = new LocalMusicProvider(dir);
|
||||||
|
// Junk content: ffmpeg cannot open it, so it is "unrecognised" rather than
|
||||||
|
// "no audio track" and must be accepted exactly like a truncated .mp3
|
||||||
|
// always has been. The extension allowlist is what is under test here.
|
||||||
|
// .m4v is excluded on purpose — see the next test.
|
||||||
|
for (const ext of [".mp4", ".mov", ".avi", ".mkv", ".flv", ".wmv", ".mpg", ".mpeg", ".3gp", ".ts", ".m2ts", ".ogv"]) {
|
||||||
|
const song = await p.uploadAudio({
|
||||||
|
buffer: Buffer.from("not really a video"),
|
||||||
|
originalName: `clip${ext}`,
|
||||||
|
mimeType: "video/mp4",
|
||||||
|
});
|
||||||
|
expect(song.platform).toBe("local");
|
||||||
|
expect(song.name).toBe("clip");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("refuses a .m4v raw video elementary stream, which by definition has no audio", async () => {
|
||||||
|
// .m4v is not a container — ffmpeg's rawvideo demuxer opens arbitrary
|
||||||
|
// bytes as an MPEG-4 video elementary stream, so it IS recognised and
|
||||||
|
// genuinely carries no audio track. Refusing it is the correct outcome,
|
||||||
|
// and it is the one case that distinguishes `recognized` from `probed`.
|
||||||
|
const p = new LocalMusicProvider(dir);
|
||||||
|
await expect(
|
||||||
|
p.uploadAudio({
|
||||||
|
buffer: Buffer.from("not really a video"),
|
||||||
|
originalName: "clip.m4v",
|
||||||
|
mimeType: "video/x-m4v",
|
||||||
|
}),
|
||||||
|
).rejects.toThrow(/音轨/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("the error message names both audio and video formats", async () => {
|
||||||
|
const p = new LocalMusicProvider(dir);
|
||||||
|
await expect(
|
||||||
|
p.uploadAudio({ buffer: Buffer.from("x"), originalName: "a.exe" }),
|
||||||
|
).rejects.toThrow(/视频/);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("parseMediaProbe (#149)", () => {
|
||||||
|
const mp4Banner = `Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'clip.mp4':
|
||||||
|
Duration: 00:03:27.15, start: 0.000000, bitrate: 1105 kb/s
|
||||||
|
Stream #0:0[0x1](und): Video: h264 (High), yuv420p, 1280x720, 30 fps
|
||||||
|
Stream #0:1[0x2](und): Audio: aac (LC), 48000 Hz, stereo, fltp, 192 kb/s`;
|
||||||
|
|
||||||
|
it("reads duration and detects the audio stream in a video container", () => {
|
||||||
|
const r = parseMediaProbe(mp4Banner);
|
||||||
|
expect(r.durationSeconds).toBe(3 * 60 + 27);
|
||||||
|
expect(r.hasAudio).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("reports hasAudio false for a video with only a video stream", () => {
|
||||||
|
const silent = `Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'silent.mp4':
|
||||||
|
Duration: 00:00:02.00, start: 0.000000, bitrate: 29 kb/s
|
||||||
|
Stream #0:0[0x1](und): Video: h264 (High 4:4:4 Predictive), yuv444p, 160x120, 10 fps`;
|
||||||
|
const r = parseMediaProbe(silent);
|
||||||
|
expect(r.durationSeconds).toBe(2);
|
||||||
|
expect(r.hasAudio).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("detects a plain audio file", () => {
|
||||||
|
const r = parseMediaProbe(`Input #0, mp3, from 'a.mp3':
|
||||||
|
Duration: 00:00:30.02, start: 0.000000, bitrate: 128 kb/s
|
||||||
|
Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 128 kb/s`);
|
||||||
|
expect(r.durationSeconds).toBe(30);
|
||||||
|
expect(r.hasAudio).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not mistake an attached cover image for an audio stream", () => {
|
||||||
|
const r = parseMediaProbe(`Input #0, mp3, from 'cover.mp3':
|
||||||
|
Duration: 00:00:10.00, start: 0.000000, bitrate: 130 kb/s
|
||||||
|
Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 128 kb/s
|
||||||
|
Stream #0:1: Video: mjpeg (Baseline), yuvj420p(pc), 100x100 [attached pic]`);
|
||||||
|
expect(r.hasAudio).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns zeros on unparseable output rather than throwing", () => {
|
||||||
|
const r = parseMediaProbe("ffmpeg: command exploded");
|
||||||
|
expect(r.durationSeconds).toBe(0);
|
||||||
|
expect(r.hasAudio).toBe(false);
|
||||||
|
expect(r.recognized).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
// The distinction that decides whether an upload is refused: ffmpeg opened
|
||||||
|
// the file and found no audio (refuse) vs ffmpeg could not open it at all
|
||||||
|
// (accept, as it always has for truncated audio).
|
||||||
|
it("marks a readable container recognized and unreadable bytes not", () => {
|
||||||
|
expect(parseMediaProbe(mp4Banner).recognized).toBe(true);
|
||||||
|
expect(parseMediaProbe(`[mov,mp4,m4a,3gp,3g2,mj2 @ 0x1] moov atom not found
|
||||||
|
[in#0 @ 0x2] Error opening input: Invalid data found when processing input
|
||||||
|
Error opening input file junk.mp4.`).recognized).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rounds fractional durations", () => {
|
||||||
|
expect(parseMediaProbe("Duration: 00:00:03.60,").durationSeconds).toBe(4);
|
||||||
|
expect(parseMediaProbe("Duration: 01:02:03.10,").durationSeconds).toBe(3723);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("LocalMusicProvider quota", () => {
|
describe("LocalMusicProvider quota", () => {
|
||||||
@@ -233,3 +345,146 @@ describe("LocalMusicProvider filename handling", () => {
|
|||||||
expect(await p.getSongUrl(song.id)).not.toBeNull();
|
expect(await p.getSongUrl(song.id)).not.toBeNull();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// #149 end-to-end: build real containers with the bundled ffmpeg and push
|
||||||
|
// them through the actual upload path. Skipped automatically if the binary is
|
||||||
|
// unavailable, so the suite still runs on a machine without it.
|
||||||
|
describe("LocalMusicProvider video upload, end to end (#149)", () => {
|
||||||
|
const ffmpeg: string | null = (() => {
|
||||||
|
try {
|
||||||
|
return createRequire(import.meta.url)("ffmpeg-static") as string;
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
|
const have = !!ffmpeg && spawnSync(ffmpeg, ["-version"], { stdio: "ignore" }).status === 0;
|
||||||
|
|
||||||
|
/** Render a real container into the temp dir and return its bytes. */
|
||||||
|
function render(name: string, args: string[]): Buffer {
|
||||||
|
const out = join(dir, name);
|
||||||
|
const r = spawnSync(ffmpeg!, ["-y", "-hide_banner", "-loglevel", "error", ...args, out], {
|
||||||
|
stdio: "ignore",
|
||||||
|
});
|
||||||
|
if (r.status !== 0) throw new Error(`fixture render failed: ${name}`);
|
||||||
|
const buf = readFileSync(out);
|
||||||
|
rmSync(out, { force: true }); // upload writes its own copy
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
const withAudio = (dur: number, vcodec: string, acodec: string) => [
|
||||||
|
"-f", "lavfi", "-i", `testsrc=s=160x120:r=10:d=${dur}`,
|
||||||
|
"-f", "lavfi", "-i", `sine=f=440:d=${dur}`,
|
||||||
|
"-c:v", vcodec, "-c:a", acodec, "-shortest",
|
||||||
|
];
|
||||||
|
|
||||||
|
it.runIf(have)("accepts an mp4, reads its duration, and keeps only the audio", async () => {
|
||||||
|
const p = new LocalMusicProvider(dir);
|
||||||
|
const mp4 = render("src.mp4", withAudio(3, "libx264", "aac"));
|
||||||
|
const song = await p.uploadAudio({
|
||||||
|
buffer: mp4, originalName: "My Clip.mp4", mimeType: "video/mp4",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(song.name).toBe("My Clip");
|
||||||
|
expect(song.platform).toBe("local");
|
||||||
|
expect(song.duration).toBe(3);
|
||||||
|
|
||||||
|
const resolved = await p.getSongUrl(song.id);
|
||||||
|
expect(resolved).not.toBeNull();
|
||||||
|
// The video container is gone; what remains is the extracted audio track.
|
||||||
|
// AAC (what libx264+aac mp4s carry) goes to .m4a so the encoder-priming
|
||||||
|
// edit list survives — see extractedAudioExt.
|
||||||
|
expect(resolved!.url.endsWith(".m4a")).toBe(true);
|
||||||
|
expect(existsSync(join(dir, `${song.id}.mp4`))).toBe(false);
|
||||||
|
expect(existsSync(resolved!.url)).toBe(true);
|
||||||
|
expect(statSync(resolved!.url).size).toBeGreaterThan(0);
|
||||||
|
expect(statSync(resolved!.url).size).toBeLessThan(mp4.length);
|
||||||
|
}, 60000);
|
||||||
|
|
||||||
|
it.runIf(have)("extracted audio is still decodable by the player's ffmpeg args", async () => {
|
||||||
|
const p = new LocalMusicProvider(dir);
|
||||||
|
const song = await p.uploadAudio({
|
||||||
|
buffer: render("src2.mp4", withAudio(2, "libx264", "aac")),
|
||||||
|
originalName: "clip.mp4",
|
||||||
|
mimeType: "video/mp4",
|
||||||
|
});
|
||||||
|
const url = (await p.getSongUrl(song.id))!.url;
|
||||||
|
|
||||||
|
const decoded = spawnSync(
|
||||||
|
ffmpeg!,
|
||||||
|
[...buildFfmpegArgs(url, 0).slice(0, -1), "-"],
|
||||||
|
{ maxBuffer: 64 * 1024 * 1024 },
|
||||||
|
);
|
||||||
|
expect(decoded.status).toBe(0);
|
||||||
|
// 2s of 48 kHz stereo s16le ≈ 384000 bytes; allow codec priming slack.
|
||||||
|
expect(decoded.stdout.length).toBeGreaterThan(300000);
|
||||||
|
}, 60000);
|
||||||
|
|
||||||
|
it.runIf(have)("aac extraction decodes bit-for-bit identically to the audio inside the video", async () => {
|
||||||
|
// The strongest statement of "lossless": decode the audio track straight
|
||||||
|
// out of the source mp4, decode the stored extract, compare the PCM.
|
||||||
|
// A Matroska remux would NOT pass this — it loses the MP4 edit list that
|
||||||
|
// discards AAC encoder priming, so it decodes ~23 ms longer.
|
||||||
|
const p = new LocalMusicProvider(dir);
|
||||||
|
const bytes = render("bitexact.mp4", withAudio(4, "libx264", "aac"));
|
||||||
|
const sourceCopy = join(dir, "source-kept.mp4");
|
||||||
|
writeFileSync(sourceCopy, bytes);
|
||||||
|
|
||||||
|
const song = await p.uploadAudio({
|
||||||
|
buffer: bytes, originalName: "bitexact.mp4", mimeType: "video/mp4",
|
||||||
|
});
|
||||||
|
const url = (await p.getSongUrl(song.id))!.url;
|
||||||
|
|
||||||
|
const toPcm = (input: string, pre: string[] = []) => spawnSync(
|
||||||
|
ffmpeg!,
|
||||||
|
["-hide_banner", "-loglevel", "error", "-i", input, ...pre,
|
||||||
|
"-f", "s16le", "-ar", "48000", "-ac", "2", "-acodec", "pcm_s16le", "-"],
|
||||||
|
{ maxBuffer: 128 * 1024 * 1024 },
|
||||||
|
);
|
||||||
|
|
||||||
|
const fromVideo = toPcm(sourceCopy, ["-vn", "-map", "0:a:0"]);
|
||||||
|
const fromExtract = toPcm(url);
|
||||||
|
expect(fromVideo.status).toBe(0);
|
||||||
|
expect(fromExtract.status).toBe(0);
|
||||||
|
expect(fromExtract.stdout.length).toBe(fromVideo.stdout.length);
|
||||||
|
expect(fromExtract.stdout.equals(fromVideo.stdout)).toBe(true);
|
||||||
|
}, 90000);
|
||||||
|
|
||||||
|
it.runIf(have)("refuses a video that genuinely has no audio track", async () => {
|
||||||
|
const p = new LocalMusicProvider(dir);
|
||||||
|
const silent = render("silent.mp4", [
|
||||||
|
"-f", "lavfi", "-i", "testsrc=s=160x120:r=10:d=2", "-an",
|
||||||
|
]);
|
||||||
|
await expect(
|
||||||
|
p.uploadAudio({ buffer: silent, originalName: "silent.mp4", mimeType: "video/mp4" }),
|
||||||
|
).rejects.toThrow(/音轨/);
|
||||||
|
// The rejected upload must not leave its bytes behind.
|
||||||
|
expect(readdirSync(dir).filter((f) => f.endsWith(".mp4"))).toEqual([]);
|
||||||
|
}, 60000);
|
||||||
|
|
||||||
|
it.runIf(have)("extracts losslessly from avi/mkv/flv too, not just mp4", async () => {
|
||||||
|
const p = new LocalMusicProvider(dir);
|
||||||
|
const cases: Array<[string, string[]]> = [
|
||||||
|
["a.avi", withAudio(2, "mpeg4", "libmp3lame")],
|
||||||
|
["a.mkv", withAudio(2, "libx264", "libopus")],
|
||||||
|
["a.flv", withAudio(2, "flv", "libmp3lame")],
|
||||||
|
];
|
||||||
|
for (const [name, args] of cases) {
|
||||||
|
const song = await p.uploadAudio({
|
||||||
|
buffer: render(`src-${name}`, args), originalName: name, mimeType: "video/x-msvideo",
|
||||||
|
});
|
||||||
|
const url = (await p.getSongUrl(song.id))!.url;
|
||||||
|
expect(url.endsWith(".mka")).toBe(true);
|
||||||
|
expect(statSync(url).size).toBeGreaterThan(0);
|
||||||
|
}
|
||||||
|
}, 120000);
|
||||||
|
|
||||||
|
it.runIf(have)("a plain audio upload is untouched — no extraction, original extension kept", async () => {
|
||||||
|
const p = new LocalMusicProvider(dir);
|
||||||
|
const mp3 = render("src.mp3", ["-f", "lavfi", "-i", "sine=f=440:d=2", "-c:a", "libmp3lame"]);
|
||||||
|
const song = await p.uploadAudio({ buffer: mp3, originalName: "tune.mp3", mimeType: "audio/mpeg" });
|
||||||
|
const url = (await p.getSongUrl(song.id))!.url;
|
||||||
|
expect(url.endsWith(".mp3")).toBe(true);
|
||||||
|
expect(statSync(url).size).toBe(mp3.length); // byte-identical, not remuxed
|
||||||
|
}, 60000);
|
||||||
|
});
|
||||||
+223
-24
@@ -1,5 +1,5 @@
|
|||||||
import { spawn } from "node:child_process";
|
import { spawn } from "node:child_process";
|
||||||
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
import { existsSync, mkdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
|
||||||
import { createRequire } from "node:module";
|
import { createRequire } from "node:module";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import crypto from "node:crypto";
|
import crypto from "node:crypto";
|
||||||
@@ -34,6 +34,56 @@ const AUDIO_EXTENSIONS = new Set([
|
|||||||
".ape",
|
".ape",
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
/** Video containers accepted for upload (#149). Only the audio track is ever
|
||||||
|
* used — the bot has no video output. Playback would work straight from the
|
||||||
|
* container (ffmpeg selects the audio stream), but we extract the audio on
|
||||||
|
* upload so a 200 MB clip does not sit on disk for a 3 MB song; see
|
||||||
|
* extractAudioTrack. `.webm` is deliberately absent: it is already in
|
||||||
|
* AUDIO_EXTENSIONS and both audio-only and video .webm are handled there. */
|
||||||
|
const VIDEO_EXTENSIONS = new Set([
|
||||||
|
".mp4",
|
||||||
|
".mov",
|
||||||
|
".avi",
|
||||||
|
".mkv",
|
||||||
|
".flv",
|
||||||
|
".wmv",
|
||||||
|
".m4v",
|
||||||
|
".mpg",
|
||||||
|
".mpeg",
|
||||||
|
".3gp",
|
||||||
|
".ts",
|
||||||
|
".m2ts",
|
||||||
|
".ogv",
|
||||||
|
]);
|
||||||
|
|
||||||
|
/** Fallback container for an extracted audio track. Matroska takes
|
||||||
|
* essentially any audio codec, so `-c:a copy` works without knowing what the
|
||||||
|
* source used — no re-encode, no codec/extension table. */
|
||||||
|
const EXTRACTED_AUDIO_EXT = ".mka";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Container to remux an extracted track into, chosen by its codec.
|
||||||
|
*
|
||||||
|
* AAC gets .m4a rather than the Matroska fallback. MP4 stores the AAC encoder
|
||||||
|
* priming (the ~1000 warm-up samples every AAC encoder emits) in an edit list,
|
||||||
|
* and that edit list does NOT survive into Matroska — so an aac→.mka remux
|
||||||
|
* decodes ~23 ms longer than the source, with the priming samples audible at
|
||||||
|
* the head instead of discarded. Measured: −66 dBFS, i.e. inaudible, but the
|
||||||
|
* track is then fractionally out of step with its own reported duration for
|
||||||
|
* no reason. Copying aac into .m4a keeps the edit list and decodes
|
||||||
|
* byte-for-byte identical to the audio inside the original video.
|
||||||
|
*
|
||||||
|
* AAC is worth special-casing because it is what mp4 / mov / m4v — the
|
||||||
|
* formats people actually upload — almost always carry.
|
||||||
|
*/
|
||||||
|
function extractedAudioExt(codec: string | null): string {
|
||||||
|
return codec === "aac" ? ".m4a" : EXTRACTED_AUDIO_EXT;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isSupportedUploadExt(ext: string): boolean {
|
||||||
|
return AUDIO_EXTENSIONS.has(ext) || VIDEO_EXTENSIONS.has(ext);
|
||||||
|
}
|
||||||
|
|
||||||
const DEFAULT_MAX_FILES = 200;
|
const DEFAULT_MAX_FILES = 200;
|
||||||
const DEFAULT_MAX_TOTAL_BYTES = 5 * 1024 * 1024 * 1024; // 5 GiB
|
const DEFAULT_MAX_TOTAL_BYTES = 5 * 1024 * 1024 * 1024; // 5 GiB
|
||||||
|
|
||||||
@@ -71,37 +121,128 @@ function titleFromFileName(name: string): string {
|
|||||||
return safeFileName(name).replace(/\.[^.]+$/, "") || "本地音频";
|
return safeFileName(name).replace(/\.[^.]+$/, "") || "本地音频";
|
||||||
}
|
}
|
||||||
|
|
||||||
async function probeDurationSeconds(filePath: string): Promise<number> {
|
export interface MediaProbe {
|
||||||
|
/** Rounded seconds, 0 when the probe failed or the container has no duration. */
|
||||||
|
durationSeconds: number;
|
||||||
|
/** True when ffmpeg reported at least one audio stream. Only meaningful
|
||||||
|
* together with `recognized` — see the comment there. */
|
||||||
|
hasAudio: boolean;
|
||||||
|
/** Lowercased codec name of the first audio stream ("aac", "mp3", "opus",
|
||||||
|
* …), or null when there is none. Picks the remux container. */
|
||||||
|
audioCodec: string | null;
|
||||||
|
/**
|
||||||
|
* True when ffmpeg actually opened the container and printed its
|
||||||
|
* `Input #0, <format>, from '...'` header.
|
||||||
|
*
|
||||||
|
* This is what separates "ffmpeg looked inside and there is genuinely no
|
||||||
|
* audio track" from "ffmpeg could not make sense of these bytes at all".
|
||||||
|
* Both produce hasAudio === false, but only the first is a file we should
|
||||||
|
* refuse. Unreadable bytes have always been accepted here (a truncated mp3
|
||||||
|
* uploads fine and simply reports duration 0), and that stays true.
|
||||||
|
*/
|
||||||
|
recognized: boolean;
|
||||||
|
/** False when ffmpeg could not be run or timed out, so nothing else in this
|
||||||
|
* object is meaningful and the caller must not reject the file on it. */
|
||||||
|
probed: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Parse `Duration: HH:MM:SS.ss`, the `Input #0,` header and
|
||||||
|
* `Stream #0:N...: Audio:` out of the banner ffmpeg prints on stderr when
|
||||||
|
* asked to open a file with no output. */
|
||||||
|
export function parseMediaProbe(stderr: string): Omit<MediaProbe, "probed"> {
|
||||||
|
const match = stderr.match(/Duration:\s*(\d+):(\d+):(\d+(?:\.\d+)?)/);
|
||||||
|
let durationSeconds = 0;
|
||||||
|
if (match) {
|
||||||
|
const total = Number(match[1]) * 3600 + Number(match[2]) * 60 + Number(match[3]);
|
||||||
|
durationSeconds = Number.isFinite(total) ? Math.round(total) : 0;
|
||||||
|
}
|
||||||
|
// e.g. " Stream #0:1[0x2](und): Audio: aac (LC) ..." — the stream index and
|
||||||
|
// the bracketed id/language vary, so match on the "Audio:" tag itself. An
|
||||||
|
// embedded cover image is a separate "Video: mjpeg ... [attached pic]" line
|
||||||
|
// and never matches this.
|
||||||
|
const audioMatch = stderr.match(/Stream #\d+:\d+[^\n]*:\s*Audio:\s*([A-Za-z0-9_]+)/);
|
||||||
|
const hasAudio = audioMatch !== null;
|
||||||
|
const audioCodec = audioMatch ? audioMatch[1].toLowerCase() : null;
|
||||||
|
// "Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'clip.mp4':" — absent entirely
|
||||||
|
// when ffmpeg bails with "Error opening input: Invalid data found ...".
|
||||||
|
const recognized = /^Input #\d+,/m.test(stderr);
|
||||||
|
return { durationSeconds, hasAudio, audioCodec, recognized };
|
||||||
|
}
|
||||||
|
|
||||||
|
async function probeMedia(filePath: string): Promise<MediaProbe> {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
const ffmpeg = spawn(ffmpegPath || "ffmpeg", ["-hide_banner", "-i", filePath], {
|
const ffmpeg = spawn(ffmpegPath || "ffmpeg", ["-hide_banner", "-i", filePath], {
|
||||||
stdio: ["ignore", "ignore", "pipe"],
|
stdio: ["ignore", "ignore", "pipe"],
|
||||||
});
|
});
|
||||||
let stderr = "";
|
let stderr = "";
|
||||||
|
let settled = false;
|
||||||
|
const done = (probe: MediaProbe) => {
|
||||||
|
if (settled) return;
|
||||||
|
settled = true;
|
||||||
|
resolve(probe);
|
||||||
|
};
|
||||||
|
// Video containers are much larger than the audio files this used to see,
|
||||||
|
// and the probe only reads headers — but a network/USB path can still be
|
||||||
|
// slow, so allow more than the old 5s before giving up.
|
||||||
const timeout = setTimeout(() => {
|
const timeout = setTimeout(() => {
|
||||||
ffmpeg.kill("SIGKILL");
|
ffmpeg.kill("SIGKILL");
|
||||||
resolve(0);
|
done({ durationSeconds: 0, hasAudio: false, audioCodec: null, recognized: false, probed: false });
|
||||||
}, 5000);
|
}, 20000);
|
||||||
ffmpeg.stderr.on("data", (chunk) => {
|
ffmpeg.stderr.on("data", (chunk) => {
|
||||||
stderr += chunk.toString("utf8");
|
stderr += chunk.toString("utf8");
|
||||||
});
|
});
|
||||||
ffmpeg.on("error", () => {
|
ffmpeg.on("error", () => {
|
||||||
clearTimeout(timeout);
|
clearTimeout(timeout);
|
||||||
resolve(0);
|
done({ durationSeconds: 0, hasAudio: false, audioCodec: null, recognized: false, probed: false });
|
||||||
});
|
});
|
||||||
ffmpeg.on("close", () => {
|
ffmpeg.on("close", () => {
|
||||||
clearTimeout(timeout);
|
clearTimeout(timeout);
|
||||||
const match = stderr.match(/Duration:\s*(\d+):(\d+):(\d+(?:\.\d+)?)/);
|
done({ ...parseMediaProbe(stderr), probed: true });
|
||||||
if (!match) {
|
});
|
||||||
resolve(0);
|
});
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
const hours = Number(match[1]);
|
|
||||||
const minutes = Number(match[2]);
|
/**
|
||||||
const seconds = Number(match[3]);
|
* Remux the first audio stream of `source` into `target` (#149).
|
||||||
const total = hours * 3600 + minutes * 60 + seconds;
|
*
|
||||||
resolve(Number.isFinite(total) ? Math.round(total) : 0);
|
* `-c:a copy` — the audio is moved bit-for-bit into a Matroska audio
|
||||||
});
|
* container, so this is fast, lossless, and codec-agnostic. Nothing is
|
||||||
|
* re-encoded, so a 200 MB .mp4 becomes a few MB .mka with the original audio
|
||||||
|
* intact. Video, subtitle and data streams are dropped.
|
||||||
|
*
|
||||||
|
* Returns true only if ffmpeg exited 0 AND produced a non-empty file, so a
|
||||||
|
* partial/zero-byte result can never be mistaken for a successful extraction.
|
||||||
|
* Callers fall back to keeping the original container, which plays fine.
|
||||||
|
*/
|
||||||
|
async function extractAudioTrack(source: string, target: string): Promise<boolean> {
|
||||||
|
const ok = await new Promise<boolean>((resolve) => {
|
||||||
|
const ffmpeg = spawn(
|
||||||
|
ffmpegPath || "ffmpeg",
|
||||||
|
["-hide_banner", "-loglevel", "error", "-y", "-i", source,
|
||||||
|
"-vn", "-sn", "-dn", "-map", "0:a:0", "-c:a", "copy", target],
|
||||||
|
{ stdio: ["ignore", "ignore", "ignore"] },
|
||||||
|
);
|
||||||
|
let settled = false;
|
||||||
|
const done = (v: boolean) => {
|
||||||
|
if (settled) return;
|
||||||
|
settled = true;
|
||||||
|
resolve(v);
|
||||||
|
};
|
||||||
|
// Remuxing is I/O bound, but a multi-GB input on a slow disk still takes
|
||||||
|
// a while. Cap it so a pathological file cannot wedge the upload request.
|
||||||
|
const timeout = setTimeout(() => {
|
||||||
|
ffmpeg.kill("SIGKILL");
|
||||||
|
done(false);
|
||||||
|
}, 120000);
|
||||||
|
ffmpeg.on("error", () => { clearTimeout(timeout); done(false); });
|
||||||
|
ffmpeg.on("close", (code) => { clearTimeout(timeout); done(code === 0); });
|
||||||
});
|
});
|
||||||
|
if (!ok) return false;
|
||||||
|
try {
|
||||||
|
return statSync(target).size > 0;
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class LocalMusicProvider implements MusicProvider {
|
export class LocalMusicProvider implements MusicProvider {
|
||||||
@@ -171,33 +312,91 @@ export class LocalMusicProvider implements MusicProvider {
|
|||||||
const ext = path.extname(originalName).toLowerCase();
|
const ext = path.extname(originalName).toLowerCase();
|
||||||
// Validate by the (sanitised) file extension only — never trust the
|
// Validate by the (sanitised) file extension only — never trust the
|
||||||
// client-supplied Content-Type. This also guarantees the STORED extension
|
// client-supplied Content-Type. This also guarantees the STORED extension
|
||||||
// is one of the known audio types, so a spoofed header cannot persist an
|
// is one of the known audio/video types, so a spoofed header cannot
|
||||||
// arbitrary-extension blob on disk.
|
// persist an arbitrary-extension blob on disk.
|
||||||
if (!AUDIO_EXTENSIONS.has(ext)) {
|
if (!isSupportedUploadExt(ext)) {
|
||||||
throw new Error("只支持常见音频文件,如 mp3、flac、wav、m4a、ogg、opus、aac、webm 等");
|
throw new Error(
|
||||||
|
"只支持常见音频文件(mp3、flac、wav、m4a、ogg、opus、aac、webm 等)" +
|
||||||
|
"和视频文件(mp4、mov、avi、mkv、flv、wmv 等,仅取其中的音轨播放)",
|
||||||
|
);
|
||||||
}
|
}
|
||||||
if (!input.buffer || input.buffer.length === 0) {
|
if (!input.buffer || input.buffer.length === 0) {
|
||||||
throw new Error("上传文件为空");
|
throw new Error("上传文件为空");
|
||||||
}
|
}
|
||||||
|
|
||||||
const id = crypto.randomUUID();
|
const id = crypto.randomUUID();
|
||||||
const storedName = `${id}${ext}`;
|
const isVideo = VIDEO_EXTENSIONS.has(ext);
|
||||||
const filePath = path.join(this.uploadDir, storedName);
|
let filePath = path.join(this.uploadDir, `${id}${ext}`);
|
||||||
writeFileSync(filePath, input.buffer);
|
writeFileSync(filePath, input.buffer);
|
||||||
|
|
||||||
const duration = await probeDurationSeconds(filePath);
|
let probe: MediaProbe;
|
||||||
|
try {
|
||||||
|
probe = await probeMedia(filePath);
|
||||||
|
} catch {
|
||||||
|
probe = { durationSeconds: 0, hasAudio: false, audioCodec: null, recognized: false, probed: false };
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reject a video with no audio track up front (#149). Left to playback it
|
||||||
|
// would produce a silent, zero-byte stream that just looks like a broken
|
||||||
|
// song. Require `recognized` as well as `probed`: bytes ffmpeg cannot open
|
||||||
|
// at all report hasAudio false for a different reason, and those have
|
||||||
|
// always been accepted (a truncated upload lands with duration 0) — this
|
||||||
|
// change must not start rejecting them.
|
||||||
|
if (isVideo && probe.probed && probe.recognized && !probe.hasAudio) {
|
||||||
|
rmSync(filePath, { force: true });
|
||||||
|
throw new Error("这个视频里没有音轨,无法播放");
|
||||||
|
}
|
||||||
|
|
||||||
|
let size = input.buffer.length;
|
||||||
|
if (isVideo) {
|
||||||
|
// Keep only the audio. The video bytes are dead weight against the
|
||||||
|
// upload-directory quota and would never be used.
|
||||||
|
// Preferred container first; if that remux fails (a codec the container
|
||||||
|
// will not take), retry into Matroska, which takes almost anything.
|
||||||
|
const preferredExt = extractedAudioExt(probe.audioCodec);
|
||||||
|
let extracted = path.join(this.uploadDir, `${id}${preferredExt}`);
|
||||||
|
let ok = await extractAudioTrack(filePath, extracted);
|
||||||
|
if (!ok && preferredExt !== EXTRACTED_AUDIO_EXT) {
|
||||||
|
rmSync(extracted, { force: true });
|
||||||
|
extracted = path.join(this.uploadDir, `${id}${EXTRACTED_AUDIO_EXT}`);
|
||||||
|
ok = await extractAudioTrack(filePath, extracted);
|
||||||
|
}
|
||||||
|
if (ok) {
|
||||||
|
try {
|
||||||
|
// Commit filePath and size TOGETHER, and only after the source is
|
||||||
|
// actually gone. rmSync(force) still throws EBUSY/EPERM on Windows,
|
||||||
|
// and assigning size first would leave the record claiming the
|
||||||
|
// small extracted size while still pointing at the whole video —
|
||||||
|
// which makes totalBytes() under-count and lets the upload
|
||||||
|
// directory grow past its quota.
|
||||||
|
const extractedSize = statSync(extracted).size;
|
||||||
|
rmSync(filePath, { force: true });
|
||||||
|
filePath = extracted;
|
||||||
|
size = extractedSize;
|
||||||
|
} catch {
|
||||||
|
// Could not stat/remove (Windows lock) — keep playing the original
|
||||||
|
// container and drop the half-finished extract.
|
||||||
|
rmSync(extracted, { force: true });
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Extraction failed (exotic codec Matroska won't take, timeout, …).
|
||||||
|
// The original container still plays: ffmpeg picks its audio stream.
|
||||||
|
rmSync(extracted, { force: true });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const song: LocalSongRecord = {
|
const song: LocalSongRecord = {
|
||||||
id,
|
id,
|
||||||
name: titleFromFileName(originalName),
|
name: titleFromFileName(originalName),
|
||||||
artist: "本地上传",
|
artist: "本地上传",
|
||||||
album: "本地音乐",
|
album: "本地音乐",
|
||||||
duration,
|
duration: probe.durationSeconds,
|
||||||
coverUrl: "",
|
coverUrl: "",
|
||||||
platform: "local",
|
platform: "local",
|
||||||
filePath,
|
filePath,
|
||||||
originalName,
|
originalName,
|
||||||
uploadedAt: new Date().toISOString(),
|
uploadedAt: new Date().toISOString(),
|
||||||
size: input.buffer.length,
|
size,
|
||||||
mimeType: input.mimeType || "application/octet-stream",
|
mimeType: input.mimeType || "application/octet-stream",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+209
-1
@@ -14,7 +14,7 @@ import { createSessionStore } from "../../data/sessions.js";
|
|||||||
import { createPermissionStore } from "../../data/permissions.js";
|
import { createPermissionStore } from "../../data/permissions.js";
|
||||||
import { createRequireAuth } from "../middleware/requireAuth.js";
|
import { createRequireAuth } from "../middleware/requireAuth.js";
|
||||||
import { SESSION_COOKIE_NAME } from "../auth/validateSession.js";
|
import { SESSION_COOKIE_NAME } from "../auth/validateSession.js";
|
||||||
import { createMusicRouter } from "./music.js";
|
import { createMusicRouter, createLocalUploadBody } from "./music.js";
|
||||||
|
|
||||||
const empty: SearchResult = { songs: [], albums: [], playlists: [] };
|
const empty: SearchResult = { songs: [], albums: [], playlists: [] };
|
||||||
|
|
||||||
@@ -295,3 +295,211 @@ describe("music router POST /quality — persistence (#125)", () => {
|
|||||||
expect(config.audioQuality.netease).toBe("lossless");
|
expect(config.audioQuality.netease).toBe("lossless");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// #149: video containers must survive the transport layer. Before this the
|
||||||
|
// express.raw type filter only matched audio/*, video/webm and
|
||||||
|
// application/octet-stream, so a browser-sent video/mp4 body was never parsed
|
||||||
|
// and the handler answered 400 "raw audio body is required".
|
||||||
|
describe("music router POST /local/upload — content types and size cap (#149)", () => {
|
||||||
|
let app: express.Express;
|
||||||
|
let botDb: BotDatabase;
|
||||||
|
let cookie: string;
|
||||||
|
let uploadAudio: ReturnType<typeof vi.fn>;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
botDb = createDatabase(":memory:");
|
||||||
|
const users = createUserStore(botDb.db);
|
||||||
|
const sessions = createSessionStore(botDb.db);
|
||||||
|
const admin = await users.createUser("admin", "pw-admin", "admin");
|
||||||
|
cookie = `${SESSION_COOKIE_NAME}=${sessions.createSession(admin.id).token}`;
|
||||||
|
|
||||||
|
uploadAudio = vi.fn(async (input: { originalName: string }) => ({
|
||||||
|
id: "local-1", name: input.originalName, artist: "本地上传", album: "本地音乐",
|
||||||
|
duration: 1, coverUrl: "", platform: "local",
|
||||||
|
}));
|
||||||
|
const local = { platform: "local", search: vi.fn().mockResolvedValue(empty), uploadAudio } as unknown as MusicProvider;
|
||||||
|
|
||||||
|
app = express();
|
||||||
|
app.use(express.json());
|
||||||
|
app.use(cookieParser());
|
||||||
|
app.use("/api", createRequireAuth(sessions, createPermissionStore(botDb.db), () => getDefaultConfig().guestMode));
|
||||||
|
app.use("/api/music", createMusicRouter(
|
||||||
|
fakeProvider("netease"), fakeProvider("qq"), fakeProvider("bilibili"),
|
||||||
|
pino({ level: "silent" }), local, getDefaultConfig(),
|
||||||
|
));
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => botDb.close());
|
||||||
|
|
||||||
|
const post = (contentType: string, body: Buffer, name = "clip.mp4") =>
|
||||||
|
request(app)
|
||||||
|
.post("/api/music/local/upload")
|
||||||
|
.set("Cookie", cookie)
|
||||||
|
.set("Content-Type", contentType)
|
||||||
|
.set("X-Filename", encodeURIComponent(name))
|
||||||
|
.send(body);
|
||||||
|
|
||||||
|
it("accepts the video MIME types browsers actually send", async () => {
|
||||||
|
// These are what Chrome/Firefox put on a File for .mp4/.mov/.avi/.mkv.
|
||||||
|
for (const ct of ["video/mp4", "video/quicktime", "video/x-msvideo", "video/x-matroska", "video/webm"]) {
|
||||||
|
uploadAudio.mockClear();
|
||||||
|
const res = await post(ct, Buffer.from("fake video bytes"));
|
||||||
|
expect(res.status, `content-type ${ct}`).toBe(200);
|
||||||
|
expect(uploadAudio).toHaveBeenCalledOnce();
|
||||||
|
expect(res.body.song.platform).toBe("local");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("still accepts audio and octet-stream bodies", async () => {
|
||||||
|
for (const ct of ["audio/mpeg", "audio/flac", "application/octet-stream"]) {
|
||||||
|
uploadAudio.mockClear();
|
||||||
|
const res = await post(ct, Buffer.from("fake audio"), "tune.mp3");
|
||||||
|
expect(res.status, `content-type ${ct}`).toBe(200);
|
||||||
|
expect(uploadAudio).toHaveBeenCalledOnce();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("passes the decoded filename and the content type through to the provider", async () => {
|
||||||
|
await post("video/mp4", Buffer.from("bytes"), "我的 视频.mp4");
|
||||||
|
expect(uploadAudio).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({ originalName: "我的 视频.mp4", mimeType: "video/mp4" }),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("surfaces a provider rejection as a 400 with its message", async () => {
|
||||||
|
uploadAudio.mockRejectedValueOnce(new Error("这个视频里没有音轨,无法播放"));
|
||||||
|
const res = await post("video/mp4", Buffer.from("bytes"));
|
||||||
|
expect(res.status).toBe(400);
|
||||||
|
expect(res.body.error).toBe("这个视频里没有音轨,无法播放");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("requires authentication", async () => {
|
||||||
|
const res = await request(app)
|
||||||
|
.post("/api/music/local/upload")
|
||||||
|
.set("Content-Type", "video/mp4")
|
||||||
|
.send(Buffer.from("bytes"));
|
||||||
|
expect(res.status).toBe(401);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects an oversize body as JSON, not an HTML stack trace", async () => {
|
||||||
|
// Same middleware the route mounts, built with a small limit so the test
|
||||||
|
// does not have to allocate half a gigabyte to reach the cap.
|
||||||
|
const tiny = express();
|
||||||
|
const reached = vi.fn();
|
||||||
|
tiny.post("/u", createLocalUploadBody("1kb"), (_req, res) => { reached(); res.json({ ok: true }); });
|
||||||
|
|
||||||
|
const res = await request(tiny)
|
||||||
|
.post("/u")
|
||||||
|
.set("Content-Type", "video/mp4")
|
||||||
|
.send(Buffer.alloc(4096, 1));
|
||||||
|
|
||||||
|
expect(res.status).toBe(413);
|
||||||
|
expect(res.headers["content-type"]).toMatch(/application\/json/);
|
||||||
|
expect(res.body.error).toContain("文件太大");
|
||||||
|
// The HTML default handler leaked absolute server paths and a stack.
|
||||||
|
expect(res.text).not.toMatch(/node_modules|<\/pre>|at read/);
|
||||||
|
expect(reached).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("lets a body under the cap through the same middleware", async () => {
|
||||||
|
const tiny = express();
|
||||||
|
tiny.post("/u", createLocalUploadBody("1kb"), (req, res) => {
|
||||||
|
res.json({ bytes: (req.body as Buffer).length });
|
||||||
|
});
|
||||||
|
const res = await request(tiny)
|
||||||
|
.post("/u")
|
||||||
|
.set("Content-Type", "video/mp4")
|
||||||
|
.send(Buffer.alloc(512, 1));
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.body.bytes).toBe(512);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects local uploads when the feature is switched off", async () => {
|
||||||
|
const off = getDefaultConfig();
|
||||||
|
off.localAudioEnabled = false;
|
||||||
|
const users = createUserStore(botDb.db);
|
||||||
|
const sessions = createSessionStore(botDb.db);
|
||||||
|
const a2 = await users.createUser("admin2", "pw-admin2", "admin");
|
||||||
|
const c2 = `${SESSION_COOKIE_NAME}=${sessions.createSession(a2.id).token}`;
|
||||||
|
const app2 = express();
|
||||||
|
app2.use(cookieParser());
|
||||||
|
app2.use("/api", createRequireAuth(sessions, createPermissionStore(botDb.db), () => getDefaultConfig().guestMode));
|
||||||
|
app2.use("/api/music", createMusicRouter(
|
||||||
|
fakeProvider("netease"), fakeProvider("qq"), fakeProvider("bilibili"),
|
||||||
|
pino({ level: "silent" }),
|
||||||
|
{ platform: "local", search: vi.fn(), uploadAudio } as unknown as MusicProvider, off,
|
||||||
|
));
|
||||||
|
const res = await request(app2)
|
||||||
|
.post("/api/music/local/upload")
|
||||||
|
.set("Cookie", c2)
|
||||||
|
.set("Content-Type", "video/mp4")
|
||||||
|
.send(Buffer.from("bytes"));
|
||||||
|
expect(res.status).toBe(403);
|
||||||
|
expect(uploadAudio).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("music router GET /bilibili/parts", () => {
|
||||||
|
it("returns 400 when bvid is missing", async () => {
|
||||||
|
const router = createMusicRouter(
|
||||||
|
fakeProvider("netease"),
|
||||||
|
fakeProvider("qq"),
|
||||||
|
fakeProvider("bilibili"),
|
||||||
|
pino({ level: "silent" })
|
||||||
|
);
|
||||||
|
const app = express();
|
||||||
|
app.use("/api/music", router);
|
||||||
|
|
||||||
|
const res = await request(app).get("/api/music/bilibili/parts");
|
||||||
|
expect(res.status).toBe(400);
|
||||||
|
expect(res.body.error).toBe("bvid is required");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns parts from bilibili provider when available", async () => {
|
||||||
|
const mockBilibili = {
|
||||||
|
platform: "bilibili" as const,
|
||||||
|
search: vi.fn(),
|
||||||
|
getVideoParts: vi.fn().mockResolvedValue({
|
||||||
|
bvid: "BV1test",
|
||||||
|
title: "多P视频测试",
|
||||||
|
parts: [
|
||||||
|
{ part: 1, cid: 101, title: "P1", duration: 100 },
|
||||||
|
{ part: 2, cid: 102, title: "P2", duration: 200 },
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
const router = createMusicRouter(
|
||||||
|
fakeProvider("netease"),
|
||||||
|
fakeProvider("qq"),
|
||||||
|
mockBilibili as unknown as MusicProvider,
|
||||||
|
pino({ level: "silent" })
|
||||||
|
);
|
||||||
|
const app = express();
|
||||||
|
app.use("/api/music", router);
|
||||||
|
|
||||||
|
const res = await request(app).get("/api/music/bilibili/parts?bvid=BV1test");
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.body.bvid).toBe("BV1test");
|
||||||
|
expect(res.body.parts).toHaveLength(2);
|
||||||
|
expect(mockBilibili.getVideoParts).toHaveBeenCalledWith("BV1test");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns 404 when getVideoParts returns null", async () => {
|
||||||
|
const mockBilibili = {
|
||||||
|
platform: "bilibili" as const,
|
||||||
|
search: vi.fn(),
|
||||||
|
getVideoParts: vi.fn().mockResolvedValue(null),
|
||||||
|
};
|
||||||
|
const router = createMusicRouter(
|
||||||
|
fakeProvider("netease"),
|
||||||
|
fakeProvider("qq"),
|
||||||
|
mockBilibili as unknown as MusicProvider,
|
||||||
|
pino({ level: "silent" })
|
||||||
|
);
|
||||||
|
const app = express();
|
||||||
|
app.use("/api/music", router);
|
||||||
|
|
||||||
|
const res = await request(app).get("/api/music/bilibili/parts?bvid=BV1notfound");
|
||||||
|
expect(res.status).toBe(404);
|
||||||
|
});
|
||||||
|
});
|
||||||
+76
-4
@@ -7,6 +7,56 @@ import { requirePermission } from "../middleware/requirePermission.js";
|
|||||||
import { requireNotGuest } from "../middleware/requireNotGuest.js";
|
import { requireNotGuest } from "../middleware/requireNotGuest.js";
|
||||||
import { authorize } from "../middleware/authorize.js";
|
import { authorize } from "../middleware/authorize.js";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Body cap for a local upload. express.raw buffers the whole body in memory,
|
||||||
|
* so this is also the peak RAM one upload can cost — raised from 200mb for
|
||||||
|
* video (#149), which is far bigger than audio for the same song, but kept
|
||||||
|
* well short of "any video file at all" for that reason. Only the audio track
|
||||||
|
* survives to disk.
|
||||||
|
*/
|
||||||
|
export const LOCAL_UPLOAD_LIMIT = "500mb";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Body parser for the local-upload route.
|
||||||
|
*
|
||||||
|
* `type` includes "video/*" (#149): the browser sends the File's own MIME
|
||||||
|
* type, so an .mp4 arrives as video/mp4 and used to be rejected by this
|
||||||
|
* filter before ever reaching the provider. Only the audio track is kept —
|
||||||
|
* uploadAudio remuxes it out on the way in.
|
||||||
|
*
|
||||||
|
* express.raw hands an oversize body to the default error handler, which
|
||||||
|
* answers with an HTML page carrying a stack trace and absolute server paths
|
||||||
|
* (unless NODE_ENV=production, which this project never sets). Video makes
|
||||||
|
* hitting the cap far more likely than audio did, so that one case is
|
||||||
|
* translated into the same JSON shape the rest of this route returns. Any
|
||||||
|
* other body-parser error is passed on untouched.
|
||||||
|
*
|
||||||
|
* Exported as a factory so tests can drive the identical path with a small
|
||||||
|
* limit instead of allocating half a gigabyte.
|
||||||
|
*/
|
||||||
|
export function createLocalUploadBody(limit: string): express.RequestHandler {
|
||||||
|
const raw = express.raw({
|
||||||
|
type: ["audio/*", "video/*", "application/octet-stream"],
|
||||||
|
limit,
|
||||||
|
});
|
||||||
|
return (req, res, next) => {
|
||||||
|
raw(req, res, (err?: unknown) => {
|
||||||
|
if (!err) {
|
||||||
|
next();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const e = err as { type?: string; status?: number };
|
||||||
|
if (e?.type === "entity.too.large" || e?.status === 413) {
|
||||||
|
res.status(413).json({ error: `文件太大,单个文件上限 ${limit}` });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
next(err);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const localUploadBody = createLocalUploadBody(LOCAL_UPLOAD_LIMIT);
|
||||||
|
|
||||||
export function createMusicRouter(
|
export function createMusicRouter(
|
||||||
neteaseProvider: MusicProvider,
|
neteaseProvider: MusicProvider,
|
||||||
qqProvider: MusicProvider,
|
qqProvider: MusicProvider,
|
||||||
@@ -65,10 +115,7 @@ export function createMusicRouter(
|
|||||||
}
|
}
|
||||||
next();
|
next();
|
||||||
},
|
},
|
||||||
express.raw({
|
localUploadBody,
|
||||||
type: ["audio/*", "video/webm", "application/octet-stream"],
|
|
||||||
limit: "200mb",
|
|
||||||
}),
|
|
||||||
async (req, res) => {
|
async (req, res) => {
|
||||||
try {
|
try {
|
||||||
if (!localProvider) {
|
if (!localProvider) {
|
||||||
@@ -335,6 +382,31 @@ export function createMusicRouter(
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// B站分P列表查询
|
||||||
|
router.get("/bilibili/parts", async (req, res) => {
|
||||||
|
try {
|
||||||
|
const bvid = (req.query.bvid as string)?.trim();
|
||||||
|
if (!bvid) {
|
||||||
|
res.status(400).json({ error: "bvid is required" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const provider = bilibiliProvider as any;
|
||||||
|
if (typeof provider.getVideoParts === "function") {
|
||||||
|
const result = await provider.getVideoParts(bvid);
|
||||||
|
if (!result) {
|
||||||
|
res.status(404).json({ error: "Video not found" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
res.json(result);
|
||||||
|
} else {
|
||||||
|
res.status(501).json({ error: "Not supported" });
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
logger.error({ err }, "Get bilibili parts failed");
|
||||||
|
res.status(500).json({ error: (err as Error).message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Enabled sources + default platform, for the web UI (source tabs, default
|
// Enabled sources + default platform, for the web UI (source tabs, default
|
||||||
// search/playback source). Without a config (unit-test routers) everything
|
// search/playback source). Without a config (unit-test routers) everything
|
||||||
// reports enabled with the legacy netease default.
|
// reports enabled with the legacy netease default.
|
||||||
|
|||||||
@@ -167,7 +167,13 @@ describe("session router", () => {
|
|||||||
expect(meB.status).toBe(401);
|
expect(meB.status).toBe(401);
|
||||||
|
|
||||||
expect(u.id).toBe(meA.body.id);
|
expect(u.id).toBe(meA.body.id);
|
||||||
});
|
// 20s, not the 5s default: this case runs SIX bcryptjs rounds (one hash to
|
||||||
|
// create the user, four verifies, one hash for the new password), and
|
||||||
|
// bcryptjs is pure JS. It takes ~4.5s on an idle machine — close enough to
|
||||||
|
// the default that it tipped over whenever the full suite saturated the
|
||||||
|
// CPU, which made it look like a real intermittent failure. The work is
|
||||||
|
// genuinely slow, not hung, so the timeout is what was wrong.
|
||||||
|
}, 20000);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("session router — guest mode", () => {
|
describe("session router — guest mode", () => {
|
||||||
|
|||||||
@@ -0,0 +1,92 @@
|
|||||||
|
const { Client, generateIdentity } = require('@honeybbq/teamspeak-client');
|
||||||
|
|
||||||
|
function escapeTS3(value) {
|
||||||
|
return value
|
||||||
|
.replace(/\\/g, "\\\\")
|
||||||
|
.replace(/\//g, "\\/")
|
||||||
|
.replace(/ /g, "\\s")
|
||||||
|
.replace(/\|/g, "\\p")
|
||||||
|
.replace(/\n/g, "\\n")
|
||||||
|
.replace(/\r/g, "\\r")
|
||||||
|
.replace(/\t/g, "\\t");
|
||||||
|
}
|
||||||
|
|
||||||
|
function replaceField(cmd, key, value) {
|
||||||
|
const escaped = escapeTS3(value);
|
||||||
|
const regex = new RegExp(key + "=\\S*");
|
||||||
|
if (regex.test(cmd)) return cmd.replace(regex, key + "=" + escaped);
|
||||||
|
return cmd;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Version to test - passed via env
|
||||||
|
const VERSIONS = [
|
||||||
|
{
|
||||||
|
name: "6.0.0-beta2",
|
||||||
|
version: "6.0.0-beta2 [Build: 1737468425]",
|
||||||
|
platform: "Windows",
|
||||||
|
sign: "b5mySiqYAd4Lv5TZNflW+G5Gk8y7Woo9YnJfHRjmzhUyMdGfF1O7fSGJzmx2Hpe/PKaY2aDuKKD6lSxbLYlKCw==",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "3.?.? wildcard",
|
||||||
|
version: "3.?.? [Build: 5680278000]",
|
||||||
|
platform: "Windows",
|
||||||
|
sign: "DX5NIYLvfJEUjuIbCidnoeozxIDRRkpq3I9vVMBmE9L2qnekOoBzSenkzsg2lC9CMv8K5hkEzhr2TYUYSwUXCg==",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "5.0.0-beta77",
|
||||||
|
version: "5.0.0-beta77 [Build: 1702382332]",
|
||||||
|
platform: "Windows",
|
||||||
|
sign: "Ee6DzP16MUXpdKWjiSY0NGb4thN22/Ks0hwNcaMrWoaadgkM6c5477X0IbGFWVjzTWfjFTEad5noYLUPDWSgCQ==",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "3.6.2 (corrected sign)",
|
||||||
|
version: "3.6.2 [Build: 1695203293]",
|
||||||
|
platform: "Windows",
|
||||||
|
sign: "4BdaZpdgUSMCuIs8qcloJPNxNlJ4o7QKnxMCRO60mSOTtJZyKjOrGLAmeAEtLIJjcjmdSpycMbQOIV92K2vXAw==",
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const idx = parseInt(process.env.VERSION_IDX || "0");
|
||||||
|
const V = VERSIONS[idx];
|
||||||
|
|
||||||
|
const identity = generateIdentity(8);
|
||||||
|
const client = new Client(identity, "localhost:9987", "MusicBot", {
|
||||||
|
logger: {
|
||||||
|
debug: () => {},
|
||||||
|
info: (m) => console.log("[INFO]", m),
|
||||||
|
warn: (m) => console.log("[WARN]", m),
|
||||||
|
error: (m) => console.log("[ERROR]", m),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log("Testing version: " + V.name + " -> " + V.version);
|
||||||
|
client.connect().then(() => {
|
||||||
|
const handler = client.handler;
|
||||||
|
const origSendPacket = handler.sendPacket.bind(handler);
|
||||||
|
handler.sendPacket = (pType, data, flags) => {
|
||||||
|
if (pType === 2) {
|
||||||
|
let str = Buffer.from(data).toString("utf-8");
|
||||||
|
if (str.startsWith("clientinit ")) {
|
||||||
|
str = replaceField(str, "client_version", V.version);
|
||||||
|
str = replaceField(str, "client_platform", V.platform);
|
||||||
|
str = replaceField(str, "client_version_sign", V.sign);
|
||||||
|
console.log("[PATCHED] version=" + V.version);
|
||||||
|
origSendPacket(pType, Buffer.from(str), flags);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
origSendPacket(pType, data, flags);
|
||||||
|
};
|
||||||
|
return client.waitConnected();
|
||||||
|
}).then(() => {
|
||||||
|
console.log("SUCCESS! Connected with clientId = " + client.clientID());
|
||||||
|
client.disconnect();
|
||||||
|
process.exit(0);
|
||||||
|
}).catch((err) => {
|
||||||
|
console.log("ERROR:", err && err.message || err);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
setTimeout(() => {
|
||||||
|
console.log("TIMEOUT - version rejected");
|
||||||
|
process.exit(2);
|
||||||
|
}, 10000);
|
||||||
@@ -7,6 +7,7 @@
|
|||||||
<Player />
|
<Player />
|
||||||
<Toast />
|
<Toast />
|
||||||
<Queue class="mobile-queue" :open="mobileQueueOpen" @close="mobileQueueOpen = false" />
|
<Queue class="mobile-queue" :open="mobileQueueOpen" @close="mobileQueueOpen = false" />
|
||||||
|
<BilibiliPartModal />
|
||||||
|
|
||||||
<!-- Mobile mini player -->
|
<!-- Mobile mini player -->
|
||||||
<div v-if="currentSong" class="m-player" @click="onPlayerRowClick">
|
<div v-if="currentSong" class="m-player" @click="onPlayerRowClick">
|
||||||
@@ -102,6 +103,7 @@ import Player from './components/Player.vue';
|
|||||||
import CoverArt from './components/CoverArt.vue';
|
import CoverArt from './components/CoverArt.vue';
|
||||||
import Toast from './components/Toast.vue';
|
import Toast from './components/Toast.vue';
|
||||||
import Queue from './components/Queue.vue';
|
import Queue from './components/Queue.vue';
|
||||||
|
import BilibiliPartModal from './components/BilibiliPartModal.vue';
|
||||||
|
|
||||||
const playerStore = usePlayerStore();
|
const playerStore = usePlayerStore();
|
||||||
const session = useSession();
|
const session = useSession();
|
||||||
|
|||||||
@@ -0,0 +1,293 @@
|
|||||||
|
<template>
|
||||||
|
<div v-if="modal.open" class="edit-modal-overlay" @click.self="store.closeBilibiliPartModal">
|
||||||
|
<div class="edit-modal">
|
||||||
|
<h3 class="modal-title">选择分P</h3>
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<label>视频名称</label>
|
||||||
|
<div class="video-info-box">
|
||||||
|
<CoverArt :url="modal.coverUrl" :size="44" :radius="8" />
|
||||||
|
<div class="video-meta">
|
||||||
|
<div class="video-title" :title="modal.title">{{ modal.title }}</div>
|
||||||
|
<div class="video-hint">共 {{ modal.parts.length }} 个分P · {{ actionHint }}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<label>分P列表</label>
|
||||||
|
<div class="parts-list">
|
||||||
|
<div
|
||||||
|
v-for="part in modal.parts"
|
||||||
|
:key="part.part"
|
||||||
|
class="part-item"
|
||||||
|
:class="{ active: selectedPart?.part === part.part }"
|
||||||
|
@click="selectedPart = part"
|
||||||
|
@dblclick="confirmSelect(part)"
|
||||||
|
>
|
||||||
|
<span class="part-badge">P{{ part.part }}</span>
|
||||||
|
<span class="part-name" :title="part.title">{{ part.title }}</span>
|
||||||
|
<span class="part-duration">{{ formatDuration(part.duration) }}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="modal-actions">
|
||||||
|
<button class="btn-secondary" @click="store.closeBilibiliPartModal">取消</button>
|
||||||
|
<button
|
||||||
|
class="btn-primary"
|
||||||
|
:disabled="!selectedPart"
|
||||||
|
@click="selectedPart && confirmSelect(selectedPart)"
|
||||||
|
>
|
||||||
|
{{ confirmBtnText }}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
import { ref, computed, watch } from 'vue';
|
||||||
|
import { usePlayerStore, type BiliPart } from '../stores/player.js';
|
||||||
|
import CoverArt from './CoverArt.vue';
|
||||||
|
|
||||||
|
const store = usePlayerStore();
|
||||||
|
const modal = computed(() => store.biliPartModal);
|
||||||
|
|
||||||
|
const selectedPart = ref<BiliPart | null>(null);
|
||||||
|
|
||||||
|
// 弹窗打开时默认选中第 1 P
|
||||||
|
watch(
|
||||||
|
() => modal.value.open,
|
||||||
|
(open) => {
|
||||||
|
if (open && modal.value.parts.length > 0) {
|
||||||
|
selectedPart.value = modal.value.parts[0];
|
||||||
|
} else {
|
||||||
|
selectedPart.value = null;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ immediate: true },
|
||||||
|
);
|
||||||
|
|
||||||
|
const actionHint = computed(() => {
|
||||||
|
if (modal.value.action === 'playNext') return '添加到下一首播放';
|
||||||
|
if (modal.value.action === 'add') return '添加到播放队列';
|
||||||
|
return '立即播放';
|
||||||
|
});
|
||||||
|
|
||||||
|
const confirmBtnText = computed(() => {
|
||||||
|
if (modal.value.action === 'playNext') return '下一首播放';
|
||||||
|
if (modal.value.action === 'add') return '添加到队列';
|
||||||
|
return '播放';
|
||||||
|
});
|
||||||
|
|
||||||
|
function confirmSelect(part: BiliPart) {
|
||||||
|
store.selectBilibiliPart(part);
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatDuration(seconds: number): string {
|
||||||
|
if (!seconds || seconds <= 0) return '--:--';
|
||||||
|
const m = Math.floor(seconds / 60);
|
||||||
|
const s = seconds % 60;
|
||||||
|
return `${m}:${s.toString().padStart(2, '0')}`;
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style lang="scss" scoped>
|
||||||
|
.edit-modal-overlay {
|
||||||
|
position: fixed;
|
||||||
|
inset: 0;
|
||||||
|
background: rgba(0, 0, 0, 0.5);
|
||||||
|
z-index: 200;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.edit-modal {
|
||||||
|
background: var(--bg-secondary);
|
||||||
|
border-radius: var(--radius-lg);
|
||||||
|
padding: 28px;
|
||||||
|
width: 480px;
|
||||||
|
max-width: 90vw;
|
||||||
|
max-height: 80vh;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
.modal-title {
|
||||||
|
font-size: 20px;
|
||||||
|
font-weight: 700;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
color: var(--text-primary);
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-group {
|
||||||
|
margin-bottom: 16px;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
min-height: 0;
|
||||||
|
|
||||||
|
label {
|
||||||
|
display: block;
|
||||||
|
font-size: 13px;
|
||||||
|
font-weight: 600;
|
||||||
|
margin-bottom: 6px;
|
||||||
|
opacity: 0.7;
|
||||||
|
color: var(--text-primary);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.video-info-box {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 12px;
|
||||||
|
padding: 10px 14px;
|
||||||
|
background: var(--hover-bg);
|
||||||
|
border: 1px solid var(--border-color);
|
||||||
|
border-radius: var(--radius-sm);
|
||||||
|
|
||||||
|
.video-meta {
|
||||||
|
flex: 1;
|
||||||
|
min-width: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.video-title {
|
||||||
|
font-size: 13px;
|
||||||
|
font-weight: 600;
|
||||||
|
color: var(--text-primary);
|
||||||
|
white-space: nowrap;
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
margin-bottom: 2px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.video-hint {
|
||||||
|
font-size: 12px;
|
||||||
|
color: var(--text-secondary);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.parts-list {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 6px;
|
||||||
|
max-height: 280px;
|
||||||
|
overflow-y: auto;
|
||||||
|
padding-right: 4px;
|
||||||
|
|
||||||
|
&::-webkit-scrollbar {
|
||||||
|
width: 6px;
|
||||||
|
}
|
||||||
|
&::-webkit-scrollbar-thumb {
|
||||||
|
background: var(--border-color);
|
||||||
|
border-radius: var(--radius-sm);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.part-item {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 10px;
|
||||||
|
padding: 10px 14px;
|
||||||
|
background: var(--hover-bg);
|
||||||
|
border: 1px solid var(--border-color);
|
||||||
|
border-radius: var(--radius-sm);
|
||||||
|
cursor: pointer;
|
||||||
|
transition: all var(--transition-fast);
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
border-color: var(--color-primary);
|
||||||
|
background: var(--color-primary-10, rgba(0, 161, 214, 0.08));
|
||||||
|
}
|
||||||
|
|
||||||
|
&.active {
|
||||||
|
border-color: var(--color-primary);
|
||||||
|
background: var(--color-primary-15, rgba(0, 161, 214, 0.15));
|
||||||
|
|
||||||
|
.part-badge {
|
||||||
|
background: var(--color-primary);
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
.part-name {
|
||||||
|
color: var(--color-primary);
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.part-badge {
|
||||||
|
font-size: 11px;
|
||||||
|
font-weight: 700;
|
||||||
|
padding: 2px 6px;
|
||||||
|
border-radius: var(--radius-xs);
|
||||||
|
background: var(--color-primary-10, rgba(0, 161, 214, 0.1));
|
||||||
|
color: var(--color-primary);
|
||||||
|
flex-shrink: 0;
|
||||||
|
transition: all var(--transition-fast);
|
||||||
|
}
|
||||||
|
|
||||||
|
.part-name {
|
||||||
|
flex: 1;
|
||||||
|
font-size: 13px;
|
||||||
|
color: var(--text-primary);
|
||||||
|
white-space: nowrap;
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
}
|
||||||
|
|
||||||
|
.part-duration {
|
||||||
|
font-size: 12px;
|
||||||
|
color: var(--text-secondary);
|
||||||
|
font-variant-numeric: tabular-nums;
|
||||||
|
flex-shrink: 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.modal-actions {
|
||||||
|
display: flex;
|
||||||
|
gap: 10px;
|
||||||
|
justify-content: flex-end;
|
||||||
|
margin-top: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-primary {
|
||||||
|
padding: 10px 20px;
|
||||||
|
background: var(--color-primary);
|
||||||
|
color: white;
|
||||||
|
border-radius: var(--radius-sm);
|
||||||
|
font-size: 13px;
|
||||||
|
font-weight: 600;
|
||||||
|
white-space: nowrap;
|
||||||
|
border: none;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: transform var(--transition-fast);
|
||||||
|
|
||||||
|
&:hover:not(:disabled) {
|
||||||
|
transform: scale(1.02);
|
||||||
|
}
|
||||||
|
&:active:not(:disabled) {
|
||||||
|
transform: scale(0.98);
|
||||||
|
}
|
||||||
|
&:disabled {
|
||||||
|
opacity: 0.5;
|
||||||
|
cursor: not-allowed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-secondary {
|
||||||
|
padding: 10px 20px;
|
||||||
|
background: var(--hover-bg);
|
||||||
|
border-radius: var(--radius-sm);
|
||||||
|
font-size: 13px;
|
||||||
|
font-weight: 600;
|
||||||
|
color: var(--text-primary);
|
||||||
|
border: none;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: opacity var(--transition-fast);
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
opacity: 0.8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
+107
-3
@@ -17,6 +17,24 @@ export interface Song {
|
|||||||
|
|
||||||
export type Source = 'jellyfin' | 'netease' | 'qq' | 'kugou' | 'spotify';
|
export type Source = 'jellyfin' | 'netease' | 'qq' | 'kugou' | 'spotify';
|
||||||
|
|
||||||
|
export interface BiliPart {
|
||||||
|
part: number;
|
||||||
|
cid: number;
|
||||||
|
title: string;
|
||||||
|
duration: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface BiliPartModalState {
|
||||||
|
open: boolean;
|
||||||
|
song: Song | null;
|
||||||
|
action: 'play' | 'playNext' | 'add';
|
||||||
|
bvid: string;
|
||||||
|
title: string;
|
||||||
|
coverUrl: string;
|
||||||
|
artist: string;
|
||||||
|
parts: BiliPart[];
|
||||||
|
}
|
||||||
|
|
||||||
export interface AlbumItem {
|
export interface AlbumItem {
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
@@ -138,6 +156,18 @@ export const usePlayerStore = defineStore('player', {
|
|||||||
// Transient notification for surfacing failures (e.g., "song not playable")
|
// Transient notification for surfacing failures (e.g., "song not playable")
|
||||||
// to a global Toast. Bumped `id` triggers re-render of the same message.
|
// to a global Toast. Bumped `id` triggers re-render of the same message.
|
||||||
notification: null as { id: number; message: string; type: 'error' | 'info' } | null,
|
notification: null as { id: number; message: string; type: 'error' | 'info' } | null,
|
||||||
|
|
||||||
|
// Bilibili 多P分P选择弹窗状态
|
||||||
|
biliPartModal: {
|
||||||
|
open: false,
|
||||||
|
song: null,
|
||||||
|
action: 'play',
|
||||||
|
bvid: '',
|
||||||
|
title: '',
|
||||||
|
coverUrl: '',
|
||||||
|
artist: '',
|
||||||
|
parts: [] as BiliPart[],
|
||||||
|
} as BiliPartModalState,
|
||||||
}),
|
}),
|
||||||
|
|
||||||
getters: {
|
getters: {
|
||||||
@@ -411,8 +441,74 @@ export const usePlayerStore = defineStore('player', {
|
|||||||
this.notification = { id: Date.now(), message, type };
|
this.notification = { id: Date.now(), message, type };
|
||||||
},
|
},
|
||||||
|
|
||||||
async playSong(song: Song) {
|
/**
|
||||||
|
* 检查 B站视频是否为多P,若为多P则弹窗询问,单P则直接修正时长并继续
|
||||||
|
*/
|
||||||
|
async checkBilibiliMultiPart(song: Song, action: 'play' | 'playNext' | 'add'): Promise<boolean> {
|
||||||
|
try {
|
||||||
|
const cleanBvid = song.id.split('?')[0].split(':')[0];
|
||||||
|
const res = await axios.get('/api/music/bilibili/parts', { params: { bvid: cleanBvid } });
|
||||||
|
const parts: BiliPart[] = res.data?.parts ?? [];
|
||||||
|
if (parts.length > 1) {
|
||||||
|
this.biliPartModal = {
|
||||||
|
open: true,
|
||||||
|
song,
|
||||||
|
action,
|
||||||
|
bvid: cleanBvid,
|
||||||
|
title: res.data.title || song.name,
|
||||||
|
coverUrl: res.data.coverUrl || song.coverUrl,
|
||||||
|
artist: res.data.artist || song.artist,
|
||||||
|
parts,
|
||||||
|
};
|
||||||
|
return true; // 弹窗接管
|
||||||
|
}
|
||||||
|
if (parts.length === 1) {
|
||||||
|
song.duration = parts[0].duration;
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// 网络请求异常则降级为正常播放
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
|
||||||
|
selectBilibiliPart(part: BiliPart) {
|
||||||
|
if (!this.biliPartModal.open || !this.biliPartModal.song) return;
|
||||||
|
const { song, action, bvid, title, artist, coverUrl } = this.biliPartModal;
|
||||||
|
this.biliPartModal.open = false;
|
||||||
|
|
||||||
|
const partTitle = part.title && part.title !== title
|
||||||
|
? `${title} - P${part.part} ${part.title}`
|
||||||
|
: `${title} (P${part.part})`;
|
||||||
|
|
||||||
|
const partSong: Song = {
|
||||||
|
...song,
|
||||||
|
id: `${bvid}?p=${part.part}`,
|
||||||
|
name: partTitle,
|
||||||
|
artist: artist || song.artist,
|
||||||
|
coverUrl: coverUrl || song.coverUrl,
|
||||||
|
duration: part.duration,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (action === 'play') {
|
||||||
|
this.playSong(partSong, true);
|
||||||
|
} else if (action === 'playNext') {
|
||||||
|
this.playNextSong(partSong, true);
|
||||||
|
} else if (action === 'add') {
|
||||||
|
this.addSong(partSong, true);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
closeBilibiliPartModal() {
|
||||||
|
this.biliPartModal.open = false;
|
||||||
|
this.biliPartModal.song = null;
|
||||||
|
},
|
||||||
|
|
||||||
|
async playSong(song: Song, skipPartCheck = false) {
|
||||||
if (!this.activeBotId) return;
|
if (!this.activeBotId) return;
|
||||||
|
if (!skipPartCheck && song.platform === 'bilibili' && !song.id.includes('?p=')) {
|
||||||
|
const handled = await this.checkBilibiliMultiPart(song, 'play');
|
||||||
|
if (handled) return;
|
||||||
|
}
|
||||||
// Guests use the non-destructive "play now" (insert-next + skip) so they
|
// Guests use the non-destructive "play now" (insert-next + skip) so they
|
||||||
// can't wipe everyone else's queue; members/admins keep the normal behavior.
|
// can't wipe everyone else's queue; members/admins keep the normal behavior.
|
||||||
const endpoint = useSession().isGuest.value ? 'play-now-song' : 'play-song';
|
const endpoint = useSession().isGuest.value ? 'play-now-song' : 'play-song';
|
||||||
@@ -424,8 +520,12 @@ export const usePlayerStore = defineStore('player', {
|
|||||||
this._syncAfterAction();
|
this._syncAfterAction();
|
||||||
},
|
},
|
||||||
|
|
||||||
async playNextSong(song: Song) {
|
async playNextSong(song: Song, skipPartCheck = false) {
|
||||||
if (!this.activeBotId) return;
|
if (!this.activeBotId) return;
|
||||||
|
if (!skipPartCheck && song.platform === 'bilibili' && !song.id.includes('?p=')) {
|
||||||
|
const handled = await this.checkBilibiliMultiPart(song, 'playNext');
|
||||||
|
if (handled) return;
|
||||||
|
}
|
||||||
const res = await axios.post(`/api/player/${this.activeBotId}/play-next-song`, { song });
|
const res = await axios.post(`/api/player/${this.activeBotId}/play-next-song`, { song });
|
||||||
if (res.data?.message) {
|
if (res.data?.message) {
|
||||||
this.notify(res.data.message, res.data.ok === false ? 'error' : 'info');
|
this.notify(res.data.message, res.data.ok === false ? 'error' : 'info');
|
||||||
@@ -444,8 +544,12 @@ export const usePlayerStore = defineStore('player', {
|
|||||||
await axios.post(`/api/player/${this.activeBotId}/add-by-id`, { songId, platform });
|
await axios.post(`/api/player/${this.activeBotId}/add-by-id`, { songId, platform });
|
||||||
},
|
},
|
||||||
|
|
||||||
async addSong(song: Song) {
|
async addSong(song: Song, skipPartCheck = false) {
|
||||||
if (!this.activeBotId) return;
|
if (!this.activeBotId) return;
|
||||||
|
if (!skipPartCheck && song.platform === 'bilibili' && !song.id.includes('?p=')) {
|
||||||
|
const handled = await this.checkBilibiliMultiPart(song, 'add');
|
||||||
|
if (handled) return;
|
||||||
|
}
|
||||||
await axios.post(`/api/player/${this.activeBotId}/add-song`, { song });
|
await axios.post(`/api/player/${this.activeBotId}/add-song`, { song });
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
+47
-10
@@ -28,18 +28,18 @@
|
|||||||
>
|
>
|
||||||
<Icon icon="mdi:tray-arrow-up" class="upload-icon" />
|
<Icon icon="mdi:tray-arrow-up" class="upload-icon" />
|
||||||
<div class="upload-copy">
|
<div class="upload-copy">
|
||||||
<div class="upload-title">拖拽本地音频到这里上传</div>
|
<div class="upload-title">拖拽本地音频 / 视频到这里上传</div>
|
||||||
<div class="upload-subtitle">支持 mp3、flac、wav、m4a、ogg、opus、aac、webm 等格式,上传后可直接播放或加入队列</div>
|
<div class="upload-subtitle">音频支持 mp3、flac、wav、m4a、ogg、opus、aac、webm 等,视频支持 mp4、mov、avi、mkv、flv、wmv 等(只取其中的音轨播放);上传后可直接播放或加入队列</div>
|
||||||
</div>
|
</div>
|
||||||
<button class="upload-btn" :disabled="uploading" @click="fileInput?.click()">
|
<button class="upload-btn" :disabled="uploading" @click="fileInput?.click()">
|
||||||
{{ uploading ? '上传中...' : '选择音频' }}
|
{{ uploading ? '上传中...' : '选择文件' }}
|
||||||
</button>
|
</button>
|
||||||
<input
|
<input
|
||||||
ref="fileInput"
|
ref="fileInput"
|
||||||
class="file-input"
|
class="file-input"
|
||||||
type="file"
|
type="file"
|
||||||
multiple
|
multiple
|
||||||
accept="audio/*,.mp3,.flac,.wav,.m4a,.aac,.ogg,.opus,.webm,.wma,.alac,.aiff,.ape"
|
accept="audio/*,video/*,.mp3,.flac,.wav,.m4a,.aac,.ogg,.opus,.webm,.wma,.alac,.aiff,.ape,.mp4,.mov,.avi,.mkv,.flv,.wmv,.m4v,.mpg,.mpeg,.3gp,.ts,.m2ts,.ogv"
|
||||||
@change="handleFileSelect"
|
@change="handleFileSelect"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
@@ -404,8 +404,17 @@ async function doSearch() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function isAudioFile(file: File): boolean {
|
/** Must match LOCAL_UPLOAD_LIMIT in src/web/api/music.ts. */
|
||||||
return file.type.startsWith('audio/') || /\.(mp3|flac|wav|m4a|aac|ogg|opus|webm|wma|alac|aiff|ape)$/i.test(file.name);
|
const UPLOAD_MAX_MB = 500;
|
||||||
|
const UPLOAD_MAX_BYTES = UPLOAD_MAX_MB * 1024 * 1024;
|
||||||
|
|
||||||
|
// Video is accepted too (#149) — the server keeps only the audio track.
|
||||||
|
// Keep the extension list in sync with AUDIO_EXTENSIONS / VIDEO_EXTENSIONS in
|
||||||
|
// src/music/local.ts; the server re-validates, this just avoids a round-trip.
|
||||||
|
function isMediaFile(file: File): boolean {
|
||||||
|
return file.type.startsWith('audio/')
|
||||||
|
|| file.type.startsWith('video/')
|
||||||
|
|| /\.(mp3|flac|wav|m4a|aac|ogg|opus|webm|wma|alac|aiff|ape|mp4|mov|avi|mkv|flv|wmv|m4v|mpg|mpeg|3gp|ts|m2ts|ogv)$/i.test(file.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function uploadLocalFiles(fileList: File[]) {
|
async function uploadLocalFiles(fileList: File[]) {
|
||||||
@@ -414,10 +423,22 @@ async function uploadLocalFiles(fileList: File[]) {
|
|||||||
uploadMessage.value = '本地音频播放已关闭';
|
uploadMessage.value = '本地音频播放已关闭';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const files = fileList.filter(isAudioFile);
|
const candidates = fileList.filter(isMediaFile);
|
||||||
|
if (candidates.length === 0) {
|
||||||
|
uploadMessageType.value = 'error';
|
||||||
|
uploadMessage.value = '没有找到可上传的音频 / 视频文件';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reject oversize files before spending minutes uploading them (#149).
|
||||||
|
// The server enforces the same cap (LOCAL_UPLOAD_LIMIT in
|
||||||
|
// src/web/api/music.ts) and answers 413 — this only saves the round-trip,
|
||||||
|
// which matters now that a single video can be hundreds of megabytes.
|
||||||
|
const files = candidates.filter((f) => f.size <= UPLOAD_MAX_BYTES);
|
||||||
|
const oversize = candidates.filter((f) => f.size > UPLOAD_MAX_BYTES);
|
||||||
if (files.length === 0) {
|
if (files.length === 0) {
|
||||||
uploadMessageType.value = 'error';
|
uploadMessageType.value = 'error';
|
||||||
uploadMessage.value = '没有找到可上传的音频文件';
|
uploadMessage.value = `文件太大,单个文件上限 ${UPLOAD_MAX_MB} MB:${oversize[0].name}`;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -426,8 +447,19 @@ async function uploadLocalFiles(fileList: File[]) {
|
|||||||
uploadMessage.value = `正在上传 ${files.length} 个文件...`;
|
uploadMessage.value = `正在上传 ${files.length} 个文件...`;
|
||||||
|
|
||||||
const uploaded: Song[] = [];
|
const uploaded: Song[] = [];
|
||||||
const failed: string[] = [];
|
const failed: string[] = oversize.map((f) => `${f.name}: 超过 ${UPLOAD_MAX_MB} MB 上限`);
|
||||||
for (const file of files) {
|
for (const [i, file] of files.entries()) {
|
||||||
|
// Videos are orders of magnitude bigger than the audio files this used to
|
||||||
|
// handle (#149), so a silent "正在上传..." can sit there for minutes and
|
||||||
|
// look hung. Report per-file percentage while the bytes are in flight, and
|
||||||
|
// switch to a processing note once the server takes over (it still has to
|
||||||
|
// probe the file and remux the audio track out).
|
||||||
|
const label = files.length > 1 ? `(${i + 1}/${files.length})` : '';
|
||||||
|
const setProgress = (text: string) => {
|
||||||
|
uploadMessageType.value = 'info';
|
||||||
|
uploadMessage.value = `${text}${label}:${file.name}`;
|
||||||
|
};
|
||||||
|
setProgress('正在上传');
|
||||||
try {
|
try {
|
||||||
const res = await axios.post('/api/music/local/upload', file, {
|
const res = await axios.post('/api/music/local/upload', file, {
|
||||||
headers: {
|
headers: {
|
||||||
@@ -435,6 +467,11 @@ async function uploadLocalFiles(fileList: File[]) {
|
|||||||
'X-Filename': encodeURIComponent(file.name),
|
'X-Filename': encodeURIComponent(file.name),
|
||||||
},
|
},
|
||||||
maxBodyLength: Infinity,
|
maxBodyLength: Infinity,
|
||||||
|
onUploadProgress: (e) => {
|
||||||
|
if (!e.total) return;
|
||||||
|
const pct = Math.round((e.loaded / e.total) * 100);
|
||||||
|
setProgress(pct >= 100 ? '服务端处理中' : `正在上传 ${pct}%`);
|
||||||
|
},
|
||||||
});
|
});
|
||||||
if (res.data?.song) uploaded.push(res.data.song as Song);
|
if (res.data?.song) uploaded.push(res.data.song as Song);
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
|
|||||||
Reference in new issue
Block a user