Skip to content

Commit a44be46

Browse files
committed
feat: add setting file option
1 parent 30d28af commit a44be46

File tree

8 files changed

+440
-13
lines changed

8 files changed

+440
-13
lines changed

backend/src/core/syncfile.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from pathlib import Path
22
import os
3+
4+
from sqlalchemy import func
35
from db import get_db
46
from core.fileparser import FileParser, FileType
57
from sqlmodel import Session, select, union
@@ -63,6 +65,23 @@ def sync_video_file(metadata: dict, db: Session):
6365
raise Exception(f"同步影片檔案失敗: {str(e)}")
6466

6567

68+
def sync_album_data(album_id: int, db: Session):
69+
"""更新專輯的資料 專輯數量等..."""
70+
album = db.exec(select(Album).where(Album.id == album_id)).first()
71+
if not album:
72+
raise ValueError("Album not found")
73+
74+
album.total_tracks = db.exec(
75+
select(func.count())
76+
.select_from(MusicTrack)
77+
.where(MusicTrack.album_id == album_id)
78+
).first()
79+
80+
db.add(album)
81+
db.commit()
82+
return album
83+
84+
6685
def sync_music_file(metadata: dict, db: Session):
6786
"""同步音樂檔案到資料庫"""
6887
track = MusicTrack(
@@ -113,6 +132,9 @@ def sync_music_file(metadata: dict, db: Session):
113132
db.add(track_file)
114133
db.commit()
115134

135+
if track.album_id:
136+
sync_album_data(track.album_id, db)
137+
116138
return track_file
117139

118140
except Exception as e:

backend/src/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from routers.file import file_router
1010
from routers.stream import stream_router
1111
from routers.user import user_router
12+
from routers.setting import setting_router
1213
from core.logger import logger
1314
from starlette.middleware.cors import CORSMiddleware
1415

@@ -35,6 +36,7 @@ def ping():
3536
app.include_router(file_router)
3637
app.include_router(stream_router)
3738
app.include_router(user_router)
39+
app.include_router(setting_router)
3840
logger.info("Server started")
3941

4042
app.add_middleware(

backend/src/routers/file.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
from pydantic import BaseModel
1818

1919

20+
class FilePathRequest(BaseModel):
21+
file_path: str
22+
23+
2024
class AlbumResponse(BaseModel):
2125
id: Optional[int] = None
2226
created_at: Optional[datetime] = None
@@ -196,21 +200,21 @@ async def get_file(
196200

197201

198202
@file_router.post("/parse_file")
199-
async def parse_one_file(file_path: str):
203+
async def parse_one_file(request: FilePathRequest):
200204
"""解析1個檔案"""
201205
try:
202-
data = sync_one_file(Path(file_path))
206+
data = sync_one_file(Path(request.file_path))
203207
except ValueError as e:
204208
raise HTTPException(status_code=400, detail=str(e))
205209
print(data)
206210
return data
207211

208212

209213
@file_router.post("/scanall")
210-
async def scan_all_files(dir_path: Optional[str] = None):
214+
async def scan_all_files(request: FilePathRequest):
211215
"""掃描所有檔案"""
212-
if dir_path:
213-
sync_dir_file(Path(dir_path))
216+
if Path(request.file_path):
217+
sync_dir_file(Path(request.file_path))
214218
else:
215219
for dir in load_setting().storages:
216220
sync_dir_file(dir.path)

backend/src/routers/setting.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from typing import Annotated
2+
from fastapi import APIRouter, Depends
3+
from sqlmodel import Session
4+
5+
from db import get_db
6+
import os
7+
8+
9+
setting_router = APIRouter(prefix="/setting", tags=["setting"])
10+
11+
SessionDep = Annotated[Session, Depends(get_db)]
12+
13+
14+
@setting_router.get("/dir")
15+
async def get_system_dir(path: str = "/") -> dict:
16+
try:
17+
items = os.listdir(path)
18+
files = []
19+
dirs = []
20+
21+
for item in items:
22+
full_path = os.path.join(path, item)
23+
if os.path.isfile(full_path):
24+
files.append(item)
25+
elif os.path.isdir(full_path):
26+
dirs.append(item)
27+
28+
except NotADirectoryError:
29+
return {"error": "該路徑不是目錄"}
30+
except FileNotFoundError:
31+
return {"error": "找不到路徑"}
32+
except PermissionError:
33+
return {"error": "沒有權限訪問該路徑"}
34+
35+
return {"files": files, "dirs": dirs, "path": path}

docker-compose-dev.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,37 @@ services:
1313
networks:
1414
- lithium-network
1515

16+
backend:
17+
build:
18+
context: ./backend
19+
container_name: lithium-backend
20+
volumes:
21+
- backend:/app/data
22+
ports:
23+
- "8000:8000"
24+
environment:
25+
SQLIP: postgres
26+
networks:
27+
- lithium-network
28+
depends_on:
29+
- postgres
30+
31+
frontend:
32+
build:
33+
context: ./frontend/web
34+
container_name: lithium-frontend
35+
volumes:
36+
- ./frontend:/app
37+
ports:
38+
- "3000:3000"
39+
networks:
40+
- lithium-network
41+
depends_on:
42+
- backend
43+
1644
volumes:
1745
postgres_data:
46+
backend:
1847

1948
networks:
2049
lithium-network:

docker-compose.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ services:
1414
- lithium-network
1515

1616
backend:
17-
build:
18-
context: ./backend
17+
image: phillychi3/lithium-player-backend:latest
1918
container_name: lithium-backend
2019
volumes:
2120
- backend:/app/data
@@ -29,8 +28,7 @@ services:
2928
- postgres
3029

3130
frontend:
32-
build:
33-
context: ./frontend/web
31+
image: phillychi3/lithium-player-frontend:latest
3432
container_name: lithium-frontend
3533
volumes:
3634
- ./frontend:/app

frontend/web/src/routes/app/+page.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
<div class="p-4">
7070
<h3 class="truncate text-lg font-bold">{album.title}</h3>
7171
<p class="truncate text-sm opacity-75">{album.artist}</p>
72-
<p class="mt-1 text-xs opacity-60">{album.tracks?.length || 0} 首歌曲</p>
72+
<p class="mt-1 text-xs opacity-60">{album.total_tracks || 0} 首歌曲</p>
7373
</div>
7474
</div>
7575
{/each}

0 commit comments

Comments
 (0)