diff --git a/internal/controller/library.go b/internal/controller/library.go index 8de1860..74621b0 100644 --- a/internal/controller/library.go +++ b/internal/controller/library.go @@ -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) { diff --git a/internal/repository/media_repo.go b/internal/repository/media_repo.go index 3d2eac3..2445045 100644 --- a/internal/repository/media_repo.go +++ b/internal/repository/media_repo.go @@ -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 diff --git a/internal/repository/song_repo.go b/internal/repository/song_repo.go index 8f636c7..99fd238 100644 --- a/internal/repository/song_repo.go +++ b/internal/repository/song_repo.go @@ -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 (?, ?, ?, ?, ?)", diff --git a/internal/scanner/scanner.go b/internal/scanner/scanner.go index 95ec942..f4f8bfd 100644 --- a/internal/scanner/scanner.go +++ b/internal/scanner/scanner.go @@ -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) { diff --git a/internal/service/library.go b/internal/service/library.go index 2799ed5..bf99954 100644 --- a/internal/service/library.go +++ b/internal/service/library.go @@ -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) diff --git a/main.go b/main.go index 68c1ba1..49acef8 100644 --- a/main.go +++ b/main.go @@ -45,8 +45,8 @@ func main() { r.Put("/{id}/name", libraryController.UpdateName) r.Put("/{id}/path", libraryController.UpdatePath) r.Post("/{id}/scan", libraryController.Scan) + r.Get("/{id}/scan-status", libraryController.GetScanStatus) r.Get("/{id}/songs", libraryController.GetSongs) - r.Get("/scan-status", libraryController.GetScanStatus) }) r.Route("/api/songs", func(r chi.Router) { diff --git a/web/src/components/LibraryCard.vue b/web/src/components/LibraryCard.vue index 6e26a51..0659e8a 100644 --- a/web/src/components/LibraryCard.vue +++ b/web/src/components/LibraryCard.vue @@ -4,6 +4,20 @@

{{ props.name }}

+ +
+ + {{ scanStatus.running ? '扫描中' : scanStatus.error ? '扫描失败' : '最近一次扫描' }} + + + {{ scanStatus.report.processed }} / {{ scanStatus.report.total_files || 0 }} + + + 新增 {{ scanStatus.report.added }} · 跳过 {{ scanStatus.report.skipped }} · 失败 {{ scanStatus.report.failed_files?.length || 0 }} + + {{ scanStatus.error }} +
+