优化扫描
All checks were successful
Go CI / test-and-build (push) Successful in 14s
Web CI / lint-test-build (push) Successful in 28s

This commit is contained in:
2026-04-11 13:57:48 +08:00
parent 169b2c1858
commit 3f82e29c6c
11 changed files with 392 additions and 76 deletions

View File

@@ -7,15 +7,25 @@ import (
"net/http"
"strconv"
"sync"
"time"
"github.com/go-chi/chi/v5"
)
type ScanStatus struct {
Running bool `json:"running"`
LibraryID int `json:"library_id"`
Report *service.ScanReport `json:"report,omitempty"`
Error string `json:"error,omitempty"`
StartedAt *time.Time `json:"started_at,omitempty"`
FinishedAt *time.Time `json:"finished_at,omitempty"`
}
type LibraryController struct {
libraryService *service.LibraryService
coverService *service.CoverService
scanMutex sync.Mutex
isScanning bool
scanStatus ScanStatus
}
func NewLibraryController(libraryService *service.LibraryService, coverSvc *service.CoverService) *LibraryController {
@@ -112,49 +122,91 @@ func (c *LibraryController) UpdatePath(w http.ResponseWriter, r *http.Request) {
}
func (c *LibraryController) Scan(w http.ResponseWriter, r *http.Request) {
c.scanMutex.Lock()
if c.isScanning {
c.scanMutex.Unlock()
jsonError(w, "Another scan is already in progress", http.StatusConflict)
return
}
c.isScanning = true
c.scanMutex.Unlock()
idParam := chi.URLParam(r, "id")
id, err := strconv.Atoi(idParam)
if err != nil {
c.scanMutex.Lock()
c.isScanning = false
c.scanMutex.Unlock()
jsonError(w, err.Error(), http.StatusBadRequest)
return
}
go func() {
defer func() {
c.scanMutex.Lock()
c.isScanning = false
c.scanMutex.Unlock()
}()
c.scanMutex.Lock()
if c.scanStatus.Running {
c.scanMutex.Unlock()
jsonError(w, "Another scan is already in progress", http.StatusConflict)
return
}
report, err := c.libraryService.Scan(id)
startedAt := time.Now()
c.scanStatus = ScanStatus{
Running: true,
LibraryID: id,
Report: &service.ScanReport{},
StartedAt: &startedAt,
}
startedStatus := c.cloneScanStatusLocked()
c.scanMutex.Unlock()
go func() {
report, err := c.libraryService.Scan(id, func(progress service.ScanReport) {
c.scanMutex.Lock()
if c.scanStatus.LibraryID == id {
progressCopy := progress
c.scanStatus.Report = &progressCopy
}
c.scanMutex.Unlock()
})
c.scanMutex.Lock()
defer c.scanMutex.Unlock()
finishedAt := time.Now()
c.scanStatus.Running = false
c.scanStatus.FinishedAt = &finishedAt
if report != nil {
reportCopy := *report
c.scanStatus.Report = &reportCopy
}
if err != nil {
c.scanStatus.Error = err.Error()
log.Printf("Scan failed for library %d: %v", id, err)
return
}
c.scanStatus.Error = ""
log.Printf("Scan completed for library %d: %+v", id, report)
}()
jsonMsg(w, "Library scan started.")
jsonResponse(w, startedStatus, http.StatusAccepted)
}
func (c *LibraryController) GetScanStatus(w http.ResponseWriter, r *http.Request) {
idParam := chi.URLParam(r, "id")
id, err := strconv.Atoi(idParam)
if err != nil {
jsonError(w, err.Error(), http.StatusBadRequest)
return
}
c.scanMutex.Lock()
scanning := c.isScanning
status := c.cloneScanStatusLocked()
c.scanMutex.Unlock()
jsonResponse(w, map[string]bool{"scanning": scanning}, http.StatusOK)
if status.LibraryID != id {
jsonResponse(w, ScanStatus{LibraryID: id, Report: &service.ScanReport{}}, http.StatusOK)
return
}
jsonResponse(w, status, http.StatusOK)
}
func (c *LibraryController) cloneScanStatusLocked() ScanStatus {
status := c.scanStatus
if c.scanStatus.Report != nil {
reportCopy := *c.scanStatus.Report
reportCopy.FailedFiles = append([]string(nil), c.scanStatus.Report.FailedFiles...)
status.Report = &reportCopy
}
return status
}
func (c *LibraryController) Delete(w http.ResponseWriter, r *http.Request) {

View File

@@ -5,6 +5,12 @@ import (
"database/sql"
)
type ExistingMediaFile struct {
Path string
MediaFileID int
HasSong bool
}
type MediaRepository struct {
db *sql.DB
}
@@ -63,6 +69,31 @@ func (r *MediaRepository) Create(path string, libraryID int) (model.MediaFile, e
}, nil
}
func (r *MediaRepository) ListExistingByLibrary(libraryID int) ([]ExistingMediaFile, error) {
rows, err := r.db.Query(`
SELECT mf.path, mf.id, CASE WHEN s.id IS NULL THEN 0 ELSE 1 END AS has_song
FROM media_files mf
LEFT JOIN songs s ON s.media_file_id = mf.id
WHERE mf.library_id = ?
`, libraryID)
if err != nil {
return nil, err
}
defer rows.Close()
existing := []ExistingMediaFile{}
for rows.Next() {
var item ExistingMediaFile
var hasSong int
if err := rows.Scan(&item.Path, &item.MediaFileID, &hasSong); err != nil {
return nil, err
}
item.HasSong = hasSong == 1
existing = append(existing, item)
}
return existing, nil
}
func (r *MediaRepository) GetSongsByLibraryWithDetails(libraryID int) ([]model.SongDetail, error) {
rows, err := r.db.Query(`
SELECT s.id, s.title, a.name as artist_name, al.title as album_title, s.duration, mf.path

View File

@@ -95,6 +95,18 @@ func (r *SongRepository) Get(id int) (model.Song, error) {
return song, nil
}
func (r *SongRepository) HasByMediaFileID(mediaFileID int) (bool, error) {
var exists int
err := r.db.QueryRow("SELECT 1 FROM songs WHERE media_file_id = ? LIMIT 1", mediaFileID).Scan(&exists)
if err == sql.ErrNoRows {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
func (r *SongRepository) Create(title string, artistID, albumID, duration, mediaFileID int) (model.Song, error) {
result, err := r.db.Exec(
"INSERT INTO songs (title, artist_id, album_id, duration, media_file_id) VALUES (?, ?, ?, ?, ?)",

View File

@@ -31,12 +31,26 @@ type ffprobeOutput struct {
}
func ScanDirectory(dirPath string) ([]ScannedSong, error) {
songs := []ScannedSong{}
if _, err := exec.LookPath("ffprobe"); err != nil {
return songs, fmt.Errorf("ffprobe not found in PATH: %v", err)
paths, err := ListAudioFiles(dirPath)
if err != nil {
return nil, err
}
songs := make([]ScannedSong, 0, len(paths))
for _, path := range paths {
song, err := ProbeAudioFile(path)
if err != nil {
continue
}
songs = append(songs, song)
}
return songs, nil
}
func ListAudioFiles(dirPath string) ([]string, error) {
paths := []string{}
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
@@ -50,16 +64,19 @@ func ScanDirectory(dirPath string) ([]ScannedSong, error) {
return nil
}
if song, err := processAudioFile(path); err != nil {
// Log error but continue scanning other files
return nil
} else {
songs = append(songs, song)
}
paths = append(paths, path)
return nil
})
return songs, err
return paths, err
}
func ProbeAudioFile(filePath string) (ScannedSong, error) {
if _, err := exec.LookPath("ffprobe"); err != nil {
return ScannedSong{}, fmt.Errorf("ffprobe not found in PATH: %v", err)
}
return processAudioFile(filePath)
}
func processAudioFile(filePath string) (ScannedSong, error) {

View File

@@ -13,12 +13,15 @@ import (
// ScanReport holds the result of a library scan.
type ScanReport struct {
TotalFiles int
Added int
Skipped int
FailedFiles []string
TotalFiles int `json:"total_files"`
Processed int `json:"processed"`
Added int `json:"added"`
Skipped int `json:"skipped"`
FailedFiles []string `json:"failed_files"`
}
type ScanProgressFunc func(ScanReport)
type LibraryService struct {
libRepo *repository.LibraryRepository
artistRepo *repository.ArtistRepository
@@ -166,35 +169,92 @@ func (s *LibraryService) GetAlbum(id int) (model.AlbumDetail, error) {
}, nil
}
func (s *LibraryService) Scan(id int) (*ScanReport, error) {
func (s *LibraryService) Scan(id int, onProgress ScanProgressFunc) (*ScanReport, error) {
lib, err := s.libRepo.GetByID(id)
if err != nil {
return nil, fmt.Errorf("library %d not found: %w", id, err)
}
scannedSongs, err := scanner.ScanDirectory(lib.Path)
paths, err := scanner.ListAudioFiles(lib.Path)
if err != nil {
return nil, fmt.Errorf("scan directory %s: %w", lib.Path, err)
}
report := &ScanReport{TotalFiles: len(scannedSongs)}
existingFiles, err := s.mediaRepo.ListExistingByLibrary(lib.ID)
if err != nil {
return nil, fmt.Errorf("load existing media files: %w", err)
}
for _, song := range scannedSongs {
if err := s.addScannedSong(song, lib.ID, report); err != nil {
existingByPath := make(map[string]repository.ExistingMediaFile, len(existingFiles))
for _, existing := range existingFiles {
existingByPath[existing.Path] = existing
}
report := &ScanReport{TotalFiles: len(paths)}
notifyProgress(report, onProgress)
for _, path := range paths {
existing, found := existingByPath[path]
if found && existing.HasSong {
report.Skipped++
report.Processed++
notifyProgress(report, onProgress)
continue
}
song, err := scanner.ProbeAudioFile(path)
if err != nil {
log.Printf("Failed to probe %s: %v", filepath.Base(path), err)
report.FailedFiles = append(report.FailedFiles, path)
report.Processed++
notifyProgress(report, onProgress)
continue
}
if err := s.addScannedSong(song, lib.ID, report, found, existing.MediaFileID); err != nil {
log.Printf("Failed to add %s: %v", filepath.Base(song.Path), err)
report.FailedFiles = append(report.FailedFiles, song.Path)
}
report.Processed++
notifyProgress(report, onProgress)
}
log.Printf("Scan complete for library %d: %+v", id, report)
return report, nil
}
func notifyProgress(report *ScanReport, onProgress ScanProgressFunc) {
if onProgress == nil {
return
}
copyReport := ScanReport{
TotalFiles: report.TotalFiles,
Processed: report.Processed,
Added: report.Added,
Skipped: report.Skipped,
}
copyReport.FailedFiles = append([]string(nil), report.FailedFiles...)
onProgress(copyReport)
}
// addScannedSong upserts a scanned song and its related artist, album, and media file.
func (s *LibraryService) addScannedSong(song scanner.ScannedSong, libraryID int, report *ScanReport) error {
mediaFile, err := s.getOrCreateMediaFile(song.Path, libraryID)
if err != nil {
return fmt.Errorf("media file: %w", err)
func (s *LibraryService) addScannedSong(song scanner.ScannedSong, libraryID int, report *ScanReport, hasExistingMedia bool, mediaFileID int) error {
mediaFile := model.MediaFile{ID: mediaFileID, Path: song.Path, LibraryID: libraryID}
if !hasExistingMedia {
var err error
mediaFile, err = s.getOrCreateMediaFile(song.Path, libraryID)
if err != nil {
return fmt.Errorf("media file: %w", err)
}
}
if hasSong, err := s.songRepo.HasByMediaFileID(mediaFile.ID); err != nil {
return fmt.Errorf("song lookup: %w", err)
} else if hasSong {
report.Skipped++
return nil
}
artist, err := s.getOrCreateArtist(song.Artist)