fix: B站封面 — http转https防止mixed content,CDN参数@300w_300h_1c正方形裁切

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
saopig1andClaude Opus 4.6 committed 2026-03-30 19:40:07 +08:00
1 parent a6881403ef
commit 341faffdbb
1 file changed
+16 -6
+16 -6
View File
@@ -53,6 +53,20 @@ export class BiliBiliProvider implements MusicProvider {
return str.replace(/<[^>]+>/g, ""); return str.replace(/<[^>]+>/g, "");
} }
/** Normalize B站 cover URL: fix protocol, add square crop via CDN param */
private normalizeCover(url: string): string {
if (!url) return "";
let fixed = url;
if (fixed.startsWith("//")) fixed = `https:${fixed}`;
else if (fixed.startsWith("http://")) fixed = fixed.replace("http://", "https://");
// B站 CDN supports @{w}w_{h}h_1c for center crop
// Append square crop if no @ params exist
if (!fixed.includes("@") && (fixed.includes("hdslb.com") || fixed.includes("bilibili.com"))) {
fixed += "@300w_300h_1c";
}
return fixed;
}
async search(query: string, limit = 20): Promise<SearchResult> { async search(query: string, limit = 20): Promise<SearchResult> {
const res = await this.api.get("/x/web-interface/search/type", { const res = await this.api.get("/x/web-interface/search/type", {
params: { params: {
@@ -74,11 +88,7 @@ export class BiliBiliProvider implements MusicProvider {
? this.parseDurationString(v.duration) ? this.parseDurationString(v.duration)
: v.duration : v.duration
: 0, : 0,
coverUrl: v.pic coverUrl: this.normalizeCover(v.pic ?? ""),
? v.pic.startsWith("//")
? `https:${v.pic}`
: v.pic
: "",
platform: "bilibili" as const, platform: "bilibili" as const,
})); }));
@@ -115,7 +125,7 @@ export class BiliBiliProvider implements MusicProvider {
artist: data.owner?.name ?? "", artist: data.owner?.name ?? "",
album: "", album: "",
duration: data.duration ?? 0, duration: data.duration ?? 0,
coverUrl: data.pic ?? "", coverUrl: this.normalizeCover(data.pic ?? ""),
platform: "bilibili" as const, platform: "bilibili" as const,
}; };
} catch { } catch {