优化扫描
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user