Spaces:
Sleeping
Sleeping
Update music_api.py
Browse files- music_api.py +496 -592
music_api.py
CHANGED
|
@@ -1,592 +1,496 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
"""
|
| 3 |
-
修复版音乐API -
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
import os
|
| 7 |
-
import sqlite3
|
| 8 |
-
|
| 9 |
-
from
|
| 10 |
-
import
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
from fastapi
|
| 14 |
-
from fastapi.
|
| 15 |
-
from fastapi.
|
| 16 |
-
import
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
""
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
#
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
)
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
"""
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
}
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
)
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
)
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
@app.get("/api/music
|
| 481 |
-
async def
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
encoded_filename = urllib.parse.quote(song['file_name'])
|
| 498 |
-
|
| 499 |
-
return FileResponse(
|
| 500 |
-
file_path,
|
| 501 |
-
filename=encoded_filename,
|
| 502 |
-
media_type="audio/mpeg",
|
| 503 |
-
headers={
|
| 504 |
-
"Content-Disposition": f"inline; filename*=UTF-8''{encoded_filename}",
|
| 505 |
-
"Accept-Ranges": "bytes"
|
| 506 |
-
}
|
| 507 |
-
)
|
| 508 |
-
|
| 509 |
-
@app.get("/api/music/{music_id}/download")
|
| 510 |
-
async def download_music(music_id: int):
|
| 511 |
-
song = Database.get_song_by_id(music_id)
|
| 512 |
-
if not song:
|
| 513 |
-
raise HTTPException(status_code=404, detail="歌曲不存在")
|
| 514 |
-
|
| 515 |
-
file_path = config.MUSIC_DIR / song['file_name']
|
| 516 |
-
if not file_path.exists():
|
| 517 |
-
raise HTTPException(status_code=404, detail="音乐文件不存在")
|
| 518 |
-
|
| 519 |
-
encoded_filename = urllib.parse.quote(song['file_name'])
|
| 520 |
-
|
| 521 |
-
return FileResponse(
|
| 522 |
-
file_path,
|
| 523 |
-
filename=encoded_filename,
|
| 524 |
-
media_type="application/octet-stream",
|
| 525 |
-
headers={
|
| 526 |
-
"Content-Disposition": f"attachment; filename*=UTF-8''{encoded_filename}"
|
| 527 |
-
}
|
| 528 |
-
)
|
| 529 |
-
|
| 530 |
-
@app.get("/api/music/{music_id}/default-cover")
|
| 531 |
-
async def get_default_cover(music_id: int):
|
| 532 |
-
from io import BytesIO
|
| 533 |
-
from PIL import Image, ImageDraw, ImageFont
|
| 534 |
-
import base64
|
| 535 |
-
|
| 536 |
-
img = Image.new('RGB', (300, 300), color=(106, 17, 203))
|
| 537 |
-
draw = ImageDraw.Draw(img)
|
| 538 |
-
try:
|
| 539 |
-
font = ImageFont.truetype("arial.ttf", 100)
|
| 540 |
-
except:
|
| 541 |
-
font = ImageFont.load_default()
|
| 542 |
-
draw.text((150, 150), "♪", fill=(255, 255, 255), font=font, anchor="mm")
|
| 543 |
-
|
| 544 |
-
buffer = BytesIO()
|
| 545 |
-
img.save(buffer, format="PNG")
|
| 546 |
-
img_base64 = base64.b64encode(buffer.getvalue()).decode()
|
| 547 |
-
|
| 548 |
-
return {
|
| 549 |
-
"image": f"data:image/png;base64,{img_base64}",
|
| 550 |
-
"song_id": music_id,
|
| 551 |
-
"is_default": True
|
| 552 |
-
}
|
| 553 |
-
|
| 554 |
-
@app.get("/api/stats")
|
| 555 |
-
async def get_stats():
|
| 556 |
-
conn = Database.get_connection()
|
| 557 |
-
cursor = conn.cursor()
|
| 558 |
-
|
| 559 |
-
cursor.execute("SELECT COUNT(*) as total FROM music")
|
| 560 |
-
total = cursor.fetchone()[0]
|
| 561 |
-
|
| 562 |
-
cursor.execute("SELECT SUM(file_size) as total_size FROM music")
|
| 563 |
-
total_size = cursor.fetchone()[0] or 0
|
| 564 |
-
|
| 565 |
-
cover_files = list(config.COVERS_DIR.glob("*.jpg")) + list(config.COVERS_DIR.glob("*.png"))
|
| 566 |
-
cover_count = len(cover_files)
|
| 567 |
-
|
| 568 |
-
conn.close()
|
| 569 |
-
|
| 570 |
-
return {
|
| 571 |
-
"total_songs": total,
|
| 572 |
-
"total_size_gb": round(total_size / (1024**3), 2),
|
| 573 |
-
"cover_count": cover_count,
|
| 574 |
-
"cover_percentage": round(cover_count / total * 100, 1) if total > 0 else 0
|
| 575 |
-
}
|
| 576 |
-
|
| 577 |
-
@app.get("/api/health")
|
| 578 |
-
async def health_check():
|
| 579 |
-
return {
|
| 580 |
-
"status": "ok",
|
| 581 |
-
"service": config.APP_NAME,
|
| 582 |
-
"version": config.VERSION,
|
| 583 |
-
"endpoints": {
|
| 584 |
-
"API文档": "/",
|
| 585 |
-
"扫描歌曲": "/api/scan",
|
| 586 |
-
"Swagger交互文档": "/docs",
|
| 587 |
-
"搜索音乐": "/api/music?q=关键词",
|
| 588 |
-
"播放歌曲": "/api/music/{id}/play",
|
| 589 |
-
"下载歌曲": "/api/music/{id}/download",
|
| 590 |
-
"系统统计": "/api/stats"
|
| 591 |
-
}
|
| 592 |
-
}
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
修复版音乐API - 支持Dataset读取 + API文件上传(双源存储)
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
import sqlite3
|
| 8 |
+
import shutil
|
| 9 |
+
from pathlib import Path
|
| 10 |
+
from typing import Optional, List, Dict, Union
|
| 11 |
+
import urllib.parse
|
| 12 |
+
|
| 13 |
+
from fastapi import FastAPI, Query, HTTPException, UploadFile, File, Form
|
| 14 |
+
from fastapi.responses import FileResponse, HTMLResponse, JSONResponse
|
| 15 |
+
from fastapi.staticfiles import StaticFiles
|
| 16 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 17 |
+
import uvicorn
|
| 18 |
+
from mutagen import File as MutagenFile
|
| 19 |
+
|
| 20 |
+
# ==================== 配置部分 ====================
|
| 21 |
+
|
| 22 |
+
class Config:
|
| 23 |
+
"""配置类"""
|
| 24 |
+
APP_NAME = "音乐API"
|
| 25 |
+
VERSION = "4.5.0"
|
| 26 |
+
|
| 27 |
+
BASE_DIR = Path(__file__).resolve().parent
|
| 28 |
+
|
| 29 |
+
# ===== 挂载的 Dataset (只读) =====
|
| 30 |
+
# 假设挂载点为 /dataset
|
| 31 |
+
DATASET_DIR = Path("/dataset")
|
| 32 |
+
|
| 33 |
+
# ===== 持久存储 (可写) =====
|
| 34 |
+
# 如果开启了 Space 的 Persistent Storage,它是 /data,重启后数据保留
|
| 35 |
+
# 如果没开启,请改为 /tmp (但上传文件重启后会丢失)
|
| 36 |
+
STORAGE_DIR = Path("/data")
|
| 37 |
+
|
| 38 |
+
# ===== 双源目录定义 =====
|
| 39 |
+
# 逻辑:优先在 STORAGE_DIR 读取,找不到再去 DATASET_DIR 读取
|
| 40 |
+
|
| 41 |
+
# 音乐目录列表 (优先级从前到后)
|
| 42 |
+
MUSIC_DIRS = [
|
| 43 |
+
STORAGE_DIR / "static" / "music", # 新上传的文件放这里
|
| 44 |
+
DATASET_DIR / "static" / "music" # 原始 Dataset 的文件
|
| 45 |
+
]
|
| 46 |
+
|
| 47 |
+
# 封面目录列表
|
| 48 |
+
COVER_DIRS = [
|
| 49 |
+
STORAGE_DIR / "static" / "covers",
|
| 50 |
+
DATASET_DIR / "static" / "covers"
|
| 51 |
+
]
|
| 52 |
+
|
| 53 |
+
# 数据库路径 (放在持久存储里,重启不丢)
|
| 54 |
+
DATABASE_PATH = STORAGE_DIR / "music.db"
|
| 55 |
+
|
| 56 |
+
HOST = "0.0.0.0"
|
| 57 |
+
PORT = int(os.environ.get("PORT", 7860))
|
| 58 |
+
DEBUG = True
|
| 59 |
+
|
| 60 |
+
def __init__(self):
|
| 61 |
+
# 确保可写目录存在
|
| 62 |
+
for d in self.MUSIC_DIRS + self.COVER_DIRS:
|
| 63 |
+
d.mkdir(parents=True, exist_ok=True)
|
| 64 |
+
# 确保数据库目录存在
|
| 65 |
+
self.DATABASE_PATH.parent.mkdir(parents=True, exist_ok=True)
|
| 66 |
+
|
| 67 |
+
config = Config()
|
| 68 |
+
|
| 69 |
+
# ==================== 辅助函数 ====================
|
| 70 |
+
|
| 71 |
+
def find_file(filename: str, directories: List[Path]) -> Optional[Path]:
|
| 72 |
+
"""在多个目录中查找文件"""
|
| 73 |
+
for directory in directories:
|
| 74 |
+
path = directory / filename
|
| 75 |
+
if path.exists() and path.is_file():
|
| 76 |
+
return path
|
| 77 |
+
return None
|
| 78 |
+
|
| 79 |
+
def extract_audio_metadata(file_path: Path) -> Dict:
|
| 80 |
+
"""使用 mutagen 提取音频文件的元数据"""
|
| 81 |
+
try:
|
| 82 |
+
audio = MutagenFile(file_path)
|
| 83 |
+
if audio is None: return None
|
| 84 |
+
title = file_path.stem
|
| 85 |
+
artist = "Unknown Artist"
|
| 86 |
+
album = ""
|
| 87 |
+
genre = ""
|
| 88 |
+
duration = 0
|
| 89 |
+
if hasattr(audio, 'info') and audio.info.length:
|
| 90 |
+
duration = int(audio.info.length)
|
| 91 |
+
common_keys = {
|
| 92 |
+
'title': ['TIT2', '\xa9nam', 'TITLE', 'Title', 'title'],
|
| 93 |
+
'artist': ['TPE1', '\xa9ART', 'ARTIST', 'Artist', 'artist'],
|
| 94 |
+
'album': ['TALB', '\xa9alb', 'ALBUM', 'Album', 'album'],
|
| 95 |
+
'genre': ['TCON', '\xa9gen', 'GENRE', 'Genre', 'genre']
|
| 96 |
+
}
|
| 97 |
+
def get_tag(target_type):
|
| 98 |
+
for k in common_keys.get(target_type, []):
|
| 99 |
+
if k in audio:
|
| 100 |
+
val = audio[k]
|
| 101 |
+
if isinstance(val, list) and len(val) > 0:
|
| 102 |
+
return str(val[0])
|
| 103 |
+
return str(val)
|
| 104 |
+
return None
|
| 105 |
+
if get_tag('title'): title = get_tag('title')
|
| 106 |
+
if get_tag('artist'): artist = get_tag('artist')
|
| 107 |
+
if get_tag('album'): album = get_tag('album')
|
| 108 |
+
if get_tag('genre'): genre = get_tag('genre')
|
| 109 |
+
return {"title": title, "artist": artist, "album": album, "genre": genre, "duration": duration, "file_size": file_path.stat().st_size}
|
| 110 |
+
except Exception as e:
|
| 111 |
+
print(f"⚠️ 无法读取 {file_path.name}: {e}")
|
| 112 |
+
return None
|
| 113 |
+
|
| 114 |
+
# ==================== 数据库部分 ====================
|
| 115 |
+
|
| 116 |
+
class Database:
|
| 117 |
+
"""数据库管理类"""
|
| 118 |
+
@staticmethod
|
| 119 |
+
def get_connection():
|
| 120 |
+
conn = sqlite3.connect(str(config.DATABASE_PATH))
|
| 121 |
+
conn.row_factory = sqlite3.Row
|
| 122 |
+
return conn
|
| 123 |
+
|
| 124 |
+
@staticmethod
|
| 125 |
+
def init_database():
|
| 126 |
+
conn = Database.get_connection()
|
| 127 |
+
cursor = conn.cursor()
|
| 128 |
+
cursor.execute('''
|
| 129 |
+
CREATE TABLE IF NOT EXISTS music (
|
| 130 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 131 |
+
title TEXT NOT NULL,
|
| 132 |
+
artist TEXT NOT NULL,
|
| 133 |
+
album TEXT,
|
| 134 |
+
genre TEXT,
|
| 135 |
+
duration INTEGER,
|
| 136 |
+
file_name TEXT NOT NULL UNIQUE,
|
| 137 |
+
file_size INTEGER,
|
| 138 |
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
| 139 |
+
)
|
| 140 |
+
''')
|
| 141 |
+
cursor.execute('CREATE INDEX IF NOT EXISTS idx_title ON music(title)')
|
| 142 |
+
cursor.execute('CREATE INDEX IF NOT EXISTS idx_filename ON music(file_name)')
|
| 143 |
+
conn.commit()
|
| 144 |
+
conn.close()
|
| 145 |
+
|
| 146 |
+
@staticmethod
|
| 147 |
+
def add_song(title: str, artist: str, album: str, genre: str, duration: int, file_name: str, file_size: int) -> int:
|
| 148 |
+
"""添加一首歌到数据库"""
|
| 149 |
+
conn = Database.get_connection()
|
| 150 |
+
cursor = conn.cursor()
|
| 151 |
+
try:
|
| 152 |
+
cursor.execute('''
|
| 153 |
+
INSERT INTO music (title, artist, album, genre, duration, file_name, file_size)
|
| 154 |
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
| 155 |
+
''', (title, artist, album, genre, duration, file_name, file_size))
|
| 156 |
+
song_id = cursor.lastrowid
|
| 157 |
+
conn.commit()
|
| 158 |
+
return song_id
|
| 159 |
+
except sqlite3.IntegrityError:
|
| 160 |
+
return 0
|
| 161 |
+
finally:
|
| 162 |
+
conn.close()
|
| 163 |
+
|
| 164 |
+
@staticmethod
|
| 165 |
+
def scan_music_files() -> Dict:
|
| 166 |
+
"""扫描所有定义的音乐目录并更新数据库"""
|
| 167 |
+
added_count = 0
|
| 168 |
+
skipped_count = 0
|
| 169 |
+
audio_extensions = ['.mp3', '.flac', '.wav', '.ogg', '.m4a', '.wma']
|
| 170 |
+
conn = Database.get_connection()
|
| 171 |
+
cursor = conn.cursor()
|
| 172 |
+
|
| 173 |
+
# 遍历所有定义的目录
|
| 174 |
+
for base_dir in config.MUSIC_DIRS:
|
| 175 |
+
if not base_dir.exists(): continue
|
| 176 |
+
print(f"正在扫描目录: {base_dir}")
|
| 177 |
+
files = list(base_dir.rglob('*'))
|
| 178 |
+
|
| 179 |
+
for file_path in files:
|
| 180 |
+
if not file_path.is_file(): continue
|
| 181 |
+
if file_path.suffix.lower() not in audio_extensions: continue
|
| 182 |
+
|
| 183 |
+
file_name = file_path.name
|
| 184 |
+
meta = extract_audio_metadata(file_path)
|
| 185 |
+
|
| 186 |
+
# 如果提取失败,使用默认值
|
| 187 |
+
if not meta:
|
| 188 |
+
meta = {"title": file_path.stem, "artist": "Unknown", "album": "", "genre": "", "duration": 0, "file_size": file_path.stat().st_size}
|
| 189 |
+
|
| 190 |
+
try:
|
| 191 |
+
cursor.execute('''
|
| 192 |
+
INSERT OR IGNORE INTO music
|
| 193 |
+
(title, artist, album, genre, duration, file_name, file_size)
|
| 194 |
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
| 195 |
+
''', (meta['title'], meta['artist'], meta['album'], meta['genre'], meta['duration'], file_name, meta['file_size']))
|
| 196 |
+
|
| 197 |
+
if cursor.rowcount > 0:
|
| 198 |
+
added_count += 1
|
| 199 |
+
print(f"✅ 新增: {meta['title']} - {meta['artist']} (来源: {base_dir.name})")
|
| 200 |
+
else:
|
| 201 |
+
skipped_count += 1
|
| 202 |
+
except Exception as e:
|
| 203 |
+
print(f"❌ 错误 ({file_name}): {e}")
|
| 204 |
+
|
| 205 |
+
conn.commit()
|
| 206 |
+
conn.close()
|
| 207 |
+
return {"status": "success", "added": added_count, "skipped": skipped_count, "message": f"扫描完成,新增 {added_count} 首"}
|
| 208 |
+
|
| 209 |
+
@staticmethod
|
| 210 |
+
def get_all_songs(limit: int = 1000) -> List[Dict]:
|
| 211 |
+
conn = Database.get_connection()
|
| 212 |
+
cursor = conn.cursor()
|
| 213 |
+
cursor.execute("SELECT * FROM music ORDER BY id DESC LIMIT ?", (limit,))
|
| 214 |
+
rows = cursor.fetchall()
|
| 215 |
+
conn.close()
|
| 216 |
+
return [dict(row) for row in rows]
|
| 217 |
+
|
| 218 |
+
@staticmethod
|
| 219 |
+
def get_song_by_id(song_id: int) -> Optional[Dict]:
|
| 220 |
+
conn = Database.get_connection()
|
| 221 |
+
cursor = conn.cursor()
|
| 222 |
+
cursor.execute("SELECT * FROM music WHERE id = ?", (song_id,))
|
| 223 |
+
row = cursor.fetchone()
|
| 224 |
+
conn.close()
|
| 225 |
+
return dict(row) if row else None
|
| 226 |
+
|
| 227 |
+
# ==================== FastAPI应用部分 ====================
|
| 228 |
+
|
| 229 |
+
app = FastAPI(title=config.APP_NAME, version=config.VERSION, docs_url="/docs")
|
| 230 |
+
|
| 231 |
+
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"])
|
| 232 |
+
|
| 233 |
+
# ==================== 页面 ====================
|
| 234 |
+
|
| 235 |
+
API_DOCS_HTML = """
|
| 236 |
+
<!DOCTYPE html>
|
| 237 |
+
<html lang="zh-CN">
|
| 238 |
+
<head>
|
| 239 |
+
<meta charset="UTF-8">
|
| 240 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 241 |
+
<title>音乐API - 管理面板</title>
|
| 242 |
+
<style>
|
| 243 |
+
:root { --bg: #1a1a2e; --card: #16213e; --text: #e0e0e0; --accent: #e94560; --input: #0f3460; }
|
| 244 |
+
body { font-family: sans-serif; background: var(--bg); color: var(--text); padding: 20px; max-width: 800px; margin: auto; }
|
| 245 |
+
h1 { text-align: center; }
|
| 246 |
+
.section { background: var(--card); padding: 20px; border-radius: 8px; margin-bottom: 20px; }
|
| 247 |
+
.section h2 { border-bottom: 1px solid #333; padding-bottom: 10px; margin-bottom: 15px; }
|
| 248 |
+
label { display: block; margin: 10px 0 5px; font-weight: bold; }
|
| 249 |
+
input[type="text"], input[type="file"] { width: 100%; padding: 10px; background: #333; border: 1px solid #444; color: white; border-radius: 4px; box-sizing: border-box; margin-bottom: 15px;}
|
| 250 |
+
button { background: var(--accent); color: white; border: none; padding: 12px 24px; border-radius: 4px; cursor: pointer; width: 100%; font-size: 16px; }
|
| 251 |
+
button:hover { opacity: 0.9; }
|
| 252 |
+
.btn-secondary { background: #444; margin-top: 10px; }
|
| 253 |
+
.result { margin-top: 15px; padding: 10px; background: #0f0f1a; border-radius: 4px; display: none; }
|
| 254 |
+
pre { white-space: pre-wrap; word-break: break-all; }
|
| 255 |
+
</style>
|
| 256 |
+
</head>
|
| 257 |
+
<body>
|
| 258 |
+
<h1>🎵 音乐 API 管理面板</h1>
|
| 259 |
+
|
| 260 |
+
<!-- 扫描区域 -->
|
| 261 |
+
<div class="section">
|
| 262 |
+
<h2>📂 扫描库</h2>
|
| 263 |
+
<p>点击下方按钮,扫描 /data 和 /dataset 目录下的所有音乐文件并更新数据库。</p>
|
| 264 |
+
<button onclick="scanLibrary()">扫描并更新数据库</button>
|
| 265 |
+
<div id="scanResult" class="result"><pre></pre></div>
|
| 266 |
+
</div>
|
| 267 |
+
|
| 268 |
+
<!-- 上传区域 -->
|
| 269 |
+
<div class="section">
|
| 270 |
+
<h2>⬆️ 上传新歌</h2>
|
| 271 |
+
<p>上传 MP3 文件和封面。文件将保存到可写存储 (/data/static)。</p>
|
| 272 |
+
<form id="uploadForm" onsubmit="uploadSong(event)">
|
| 273 |
+
<label>音乐文件 (MP3)</label>
|
| 274 |
+
<input type="file" id="file" accept="audio/*" required>
|
| 275 |
+
|
| 276 |
+
<label>封面图片 (可选, JPG/PNG)</label>
|
| 277 |
+
<input type="file" id="cover" accept="image/*">
|
| 278 |
+
|
| 279 |
+
<label>标题 (可选,留空自动读取)</label>
|
| 280 |
+
<input type="text" id="title" placeholder="自动读取...">
|
| 281 |
+
|
| 282 |
+
<label>艺术家 (可选,留空自动读取)</label>
|
| 283 |
+
<input type="text" id="artist" placeholder="自动读取...">
|
| 284 |
+
|
| 285 |
+
<button type="submit">开始上传</button>
|
| 286 |
+
</form>
|
| 287 |
+
<div id="uploadResult" class="result"><pre></pre></div>
|
| 288 |
+
</div>
|
| 289 |
+
|
| 290 |
+
<!-- 歌曲列表 -->
|
| 291 |
+
<div class="section">
|
| 292 |
+
<h2>📋 最近添加</h2>
|
| 293 |
+
<div id="songsList">加载中...</div>
|
| 294 |
+
</div>
|
| 295 |
+
|
| 296 |
+
<script>
|
| 297 |
+
const API_BASE = window.location.origin;
|
| 298 |
+
|
| 299 |
+
function showResult(id, msg) {
|
| 300 |
+
const el = document.getElementById(id);
|
| 301 |
+
el.style.display = 'block';
|
| 302 |
+
el.innerText = msg;
|
| 303 |
+
}
|
| 304 |
+
|
| 305 |
+
async function scanLibrary() {
|
| 306 |
+
showResult('scanResult', '正在扫描...');
|
| 307 |
+
try {
|
| 308 |
+
const res = await fetch(`${API_BASE}/api/scan`);
|
| 309 |
+
const data = await res.json();
|
| 310 |
+
showResult('scanResult', JSON.stringify(data, null, 2));
|
| 311 |
+
loadSongs(); // 刷新列表
|
| 312 |
+
} catch(e) {
|
| 313 |
+
showResult('scanResult', 'Error: ' + e);
|
| 314 |
+
}
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
+
async function uploadSong(e) {
|
| 318 |
+
e.preventDefault();
|
| 319 |
+
showResult('uploadResult', '正在上传...');
|
| 320 |
+
|
| 321 |
+
const formData = new FormData();
|
| 322 |
+
formData.append('file', document.getElementById('file').files[0]);
|
| 323 |
+
if(document.getElementById('cover').files[0]) {
|
| 324 |
+
formData.append('cover', document.getElementById('cover').files[0]);
|
| 325 |
+
}
|
| 326 |
+
formData.append('title', document.getElementById('title').value);
|
| 327 |
+
formData.append('artist', document.getElementById('artist').value);
|
| 328 |
+
|
| 329 |
+
try {
|
| 330 |
+
const res = await fetch(`${API_BASE}/api/upload`, { method: 'POST', body: formData });
|
| 331 |
+
const data = await res.json();
|
| 332 |
+
if(res.ok) {
|
| 333 |
+
showResult('uploadResult', `上传成功! ID: ${data.id}\n` + JSON.stringify(data, null, 2));
|
| 334 |
+
document.getElementById('uploadForm').reset();
|
| 335 |
+
loadSongs();
|
| 336 |
+
} else {
|
| 337 |
+
showResult('uploadResult', `上传失败: ${data.detail}`);
|
| 338 |
+
}
|
| 339 |
+
} catch(e) {
|
| 340 |
+
showResult('uploadResult', 'Error: ' + e);
|
| 341 |
+
}
|
| 342 |
+
}
|
| 343 |
+
|
| 344 |
+
async function loadSongs() {
|
| 345 |
+
try {
|
| 346 |
+
const res = await fetch(`${API_BASE}/api/music?limit=10`);
|
| 347 |
+
const songs = await res.json();
|
| 348 |
+
const html = songs.map(s =>
|
| 349 |
+
`<div style="padding:10px; border-bottom:1px solid #333;">
|
| 350 |
+
<b>${s.title}</b> - ${s.artist}<br>
|
| 351 |
+
<small>File: ${s.file_name} | Size: ${(s.file_size/1024/1024).toFixed(2)}MB</small>
|
| 352 |
+
</div>`
|
| 353 |
+
).join('');
|
| 354 |
+
document.getElementById('songsList').innerHTML = html || '暂无歌曲,请扫描或上传';
|
| 355 |
+
} catch(e) {
|
| 356 |
+
document.getElementById('songsList').innerText = '加载失败';
|
| 357 |
+
}
|
| 358 |
+
}
|
| 359 |
+
|
| 360 |
+
loadSongs();
|
| 361 |
+
</script>
|
| 362 |
+
</body>
|
| 363 |
+
</html>
|
| 364 |
+
"""
|
| 365 |
+
|
| 366 |
+
# ==================== 路由部分 ====================
|
| 367 |
+
|
| 368 |
+
@app.on_event("startup")
|
| 369 |
+
def startup_event():
|
| 370 |
+
Database.init_database()
|
| 371 |
+
|
| 372 |
+
@app.get("/")
|
| 373 |
+
async def get_frontend():
|
| 374 |
+
return HTMLResponse(API_DOCS_HTML)
|
| 375 |
+
|
| 376 |
+
# --- 资源服务 (核心逻辑: 优先本地,其次 Dataset) ---
|
| 377 |
+
@app.get("/static/{type}/{filename}")
|
| 378 |
+
async def serve_static(type: str, filename: str):
|
| 379 |
+
"""
|
| 380 |
+
统一的静态资源服务接口
|
| 381 |
+
type: 'music' or 'covers'
|
| 382 |
+
"""
|
| 383 |
+
dirs = config.MUSIC_DIRS if type == 'music' else config.COVER_DIRS
|
| 384 |
+
filepath = find_file(filename, dirs)
|
| 385 |
+
|
| 386 |
+
if not filepath:
|
| 387 |
+
raise HTTPException(status_code=404, detail="文件未找到")
|
| 388 |
+
|
| 389 |
+
return FileResponse(filepath)
|
| 390 |
+
|
| 391 |
+
# --- API: 扫描 ---
|
| 392 |
+
@app.get("/api/scan")
|
| 393 |
+
async def scan_library():
|
| 394 |
+
return JSONResponse(Database.scan_music_files())
|
| 395 |
+
|
| 396 |
+
# --- API: 上传 ---
|
| 397 |
+
@app.post("/api/upload")
|
| 398 |
+
async def upload_song(
|
| 399 |
+
file: UploadFile = File(..., description="MP3 文件"),
|
| 400 |
+
cover: Optional[UploadFile] = File(None, description="封面图片"),
|
| 401 |
+
title: Optional[str] = Form(None, description="标题"),
|
| 402 |
+
artist: Optional[str] = Form(None, description="艺术家")
|
| 403 |
+
):
|
| 404 |
+
"""上传歌曲并更新数据库"""
|
| 405 |
+
|
| 406 |
+
# 1. 准备保存路径 (保存到 STORAGE_DIR)
|
| 407 |
+
write_music_dir = config.STORAGE_DIR / "static" / "music"
|
| 408 |
+
write_cover_dir = config.STORAGE_DIR / "static" / "covers"
|
| 409 |
+
|
| 410 |
+
write_music_dir.mkdir(parents=True, exist_ok=True)
|
| 411 |
+
write_cover_dir.mkdir(parents=True, exist_ok=True)
|
| 412 |
+
|
| 413 |
+
# 2. 保存音乐文件
|
| 414 |
+
# 使用安全文件名
|
| 415 |
+
safe_filename = urllib.parse.quote(file.filename).replace('/', '-')
|
| 416 |
+
music_path = write_music_dir / safe_filename
|
| 417 |
+
|
| 418 |
+
try:
|
| 419 |
+
with open(music_path, "wb") as buffer:
|
| 420 |
+
shutil.copyfileobj(file.file, buffer)
|
| 421 |
+
except Exception as e:
|
| 422 |
+
raise HTTPException(status_code=500, detail=f"保存音乐文件失败: {e}")
|
| 423 |
+
|
| 424 |
+
# 3. 提取元数据 (如果没提供)
|
| 425 |
+
if not title or not artist:
|
| 426 |
+
meta = extract_audio_metadata(music_path)
|
| 427 |
+
if meta:
|
| 428 |
+
if not title: title = meta['title']
|
| 429 |
+
if not artist: artist = meta['artist']
|
| 430 |
+
|
| 431 |
+
# 4. 保存封面 (如果有)
|
| 432 |
+
if cover:
|
| 433 |
+
# 封面文件名暂时用歌曲ID还不知道,所以先随机名或原始名,这里简单用原始名
|
| 434 |
+
safe_cover_name = urllib.parse.quote(cover.filename).replace('/', '-')
|
| 435 |
+
cover_path = write_cover_dir / safe_cover_name
|
| 436 |
+
try:
|
| 437 |
+
with open(cover_path, "wb") as buffer:
|
| 438 |
+
shutil.copyfileobj(cover.file, buffer)
|
| 439 |
+
# TODO: 这里有个逻辑问题,封面通常对应 ID,上传时还没 ID。
|
| 440 |
+
# 解决方案:暂时存为原名,上传后获取 ID 再重命名,或者前端调用单独的更新封面接口。
|
| 441 |
+
# 为简化,这里暂不重命名为 ID.jpg,而是依赖用户后续手动管理或者扫描逻辑适配。
|
| 442 |
+
# *改进策略*:将封面重命名为 {音乐ID}.jpg (插入数据库后执行)
|
| 443 |
+
except Exception as e:
|
| 444 |
+
print(f"保存封面失败: {e}")
|
| 445 |
+
|
| 446 |
+
# 5. 插入数据库
|
| 447 |
+
song_id = Database.add_song(
|
| 448 |
+
title=title or "未知标题",
|
| 449 |
+
artist=artist or "未知艺术家",
|
| 450 |
+
album="", # 上传接口暂未提供专辑字段
|
| 451 |
+
genre="",
|
| 452 |
+
duration=0, # 如果已提取 meta 可以填入
|
| 453 |
+
file_name=safe_filename,
|
| 454 |
+
file_size=music_path.stat().st_size
|
| 455 |
+
)
|
| 456 |
+
|
| 457 |
+
if song_id == 0:
|
| 458 |
+
# 插入失败(文件名冲突),删除刚才上传的文件
|
| 459 |
+
music_path.unlink()
|
| 460 |
+
raise HTTPException(status_code=400, detail="该文件名已存在数据库中")
|
| 461 |
+
|
| 462 |
+
# 6. 如果有封面,重命名为 ID.ext
|
| 463 |
+
if cover:
|
| 464 |
+
try:
|
| 465 |
+
ext = Path(safe_cover_name).suffix
|
| 466 |
+
target_cover_path = write_cover_dir / f"{song_id}{ext}"
|
| 467 |
+
cover_path.rename(target_cover_path)
|
| 468 |
+
except:
|
| 469 |
+
pass
|
| 470 |
+
|
| 471 |
+
return JSONResponse({
|
| 472 |
+
"id": song_id,
|
| 473 |
+
"title": title,
|
| 474 |
+
"artist": artist,
|
| 475 |
+
"file_name": safe_filename,
|
| 476 |
+
"message": "上传成功"
|
| 477 |
+
})
|
| 478 |
+
|
| 479 |
+
# --- 其他 API ---
|
| 480 |
+
@app.get("/api/music")
|
| 481 |
+
async def get_music(limit: int = 100):
|
| 482 |
+
songs = Database.get_all_songs(limit)
|
| 483 |
+
# 补全 URL
|
| 484 |
+
for s in songs:
|
| 485 |
+
s['cover_url'] = f"/static/covers/{s['id']}.jpg" # 假设 jpg,实际可以遍历后缀
|
| 486 |
+
s['play_url'] = f"/api/play/{s['id']}"
|
| 487 |
+
return songs
|
| 488 |
+
|
| 489 |
+
@app.get("/api/play/{song_id}")
|
| 490 |
+
async def play_music(song_id: int):
|
| 491 |
+
song = Database.get_song_by_id(song_id)
|
| 492 |
+
if not song: raise HTTPException(404)
|
| 493 |
+
# 查找实际文件
|
| 494 |
+
filepath = find_file(song['file_name'], config.MUSIC_DIRS)
|
| 495 |
+
if not filepath: raise HTTPException(404, detail="音乐文件丢失")
|
| 496 |
+
return FileResponse(filepath, media_type="audio/mpeg")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|