实现增量扫描
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"log"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ScanReport holds the result of a library scan.
|
||||
@@ -16,6 +17,8 @@ type ScanReport struct {
|
||||
TotalFiles int `json:"total_files"`
|
||||
Processed int `json:"processed"`
|
||||
Added int `json:"added"`
|
||||
Updated int `json:"updated"`
|
||||
Deleted int `json:"deleted"`
|
||||
Skipped int `json:"skipped"`
|
||||
FailedFiles []string `json:"failed_files"`
|
||||
}
|
||||
@@ -175,7 +178,7 @@ func (s *LibraryService) Scan(id int, onProgress ScanProgressFunc) (*ScanReport,
|
||||
return nil, fmt.Errorf("library %d not found: %w", id, err)
|
||||
}
|
||||
|
||||
paths, err := scanner.ListAudioFiles(lib.Path)
|
||||
files, err := scanner.ListAudioFiles(lib.Path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("scan directory %s: %w", lib.Path, err)
|
||||
}
|
||||
@@ -190,29 +193,43 @@ func (s *LibraryService) Scan(id int, onProgress ScanProgressFunc) (*ScanReport,
|
||||
existingByPath[existing.Path] = existing
|
||||
}
|
||||
|
||||
report := &ScanReport{TotalFiles: len(paths)}
|
||||
seenAt := time.Now()
|
||||
seenPaths := make(map[string]struct{}, len(files))
|
||||
report := &ScanReport{TotalFiles: len(files)}
|
||||
notifyProgress(report, onProgress)
|
||||
|
||||
for _, path := range paths {
|
||||
existing, found := existingByPath[path]
|
||||
if found && existing.HasSong {
|
||||
report.Skipped++
|
||||
for _, file := range files {
|
||||
seenPaths[file.Path] = struct{}{}
|
||||
existing, found := existingByPath[file.Path]
|
||||
|
||||
if found && existing.HasSong && existing.FileSize == file.FileSize && existing.FileMtimeNs == file.FileMtimeNs {
|
||||
if err := s.mediaRepo.TouchLastSeen(existing.MediaFileID, seenAt); err != nil {
|
||||
log.Printf("Failed to refresh last_seen_at for %s: %v", filepath.Base(file.Path), err)
|
||||
report.FailedFiles = append(report.FailedFiles, file.Path)
|
||||
} else {
|
||||
report.Skipped++
|
||||
}
|
||||
report.Processed++
|
||||
notifyProgress(report, onProgress)
|
||||
continue
|
||||
}
|
||||
|
||||
song, err := scanner.ProbeAudioFile(path)
|
||||
song, err := scanner.ProbeAudioFile(file.Path)
|
||||
if err != nil {
|
||||
log.Printf("Failed to probe %s: %v", filepath.Base(path), err)
|
||||
report.FailedFiles = append(report.FailedFiles, path)
|
||||
log.Printf("Failed to probe %s: %v", filepath.Base(file.Path), err)
|
||||
if found {
|
||||
if touchErr := s.mediaRepo.TouchLastSeen(existing.MediaFileID, seenAt); touchErr != nil {
|
||||
log.Printf("Failed to refresh last_seen_at for %s after probe failure: %v", filepath.Base(file.Path), touchErr)
|
||||
}
|
||||
}
|
||||
report.FailedFiles = append(report.FailedFiles, file.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)
|
||||
if err := s.upsertScannedSong(song, file, lib.ID, report, seenAt, found, existing); err != nil {
|
||||
log.Printf("Failed to upsert %s: %v", filepath.Base(song.Path), err)
|
||||
report.FailedFiles = append(report.FailedFiles, song.Path)
|
||||
}
|
||||
|
||||
@@ -220,6 +237,13 @@ func (s *LibraryService) Scan(id int, onProgress ScanProgressFunc) (*ScanReport,
|
||||
notifyProgress(report, onProgress)
|
||||
}
|
||||
|
||||
deleted, err := s.deleteMissingMediaFiles(existingFiles, seenPaths)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("delete missing media files: %w", err)
|
||||
}
|
||||
report.Deleted = deleted
|
||||
notifyProgress(report, onProgress)
|
||||
|
||||
log.Printf("Scan complete for library %d: %+v", id, report)
|
||||
return report, nil
|
||||
}
|
||||
@@ -233,30 +257,15 @@ func notifyProgress(report *ScanReport, onProgress ScanProgressFunc) {
|
||||
TotalFiles: report.TotalFiles,
|
||||
Processed: report.Processed,
|
||||
Added: report.Added,
|
||||
Updated: report.Updated,
|
||||
Deleted: report.Deleted,
|
||||
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, 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
|
||||
}
|
||||
|
||||
func (s *LibraryService) upsertScannedSong(song scanner.ScannedSong, file scanner.DiscoveredAudioFile, libraryID int, report *ScanReport, seenAt time.Time, hasExistingMedia bool, existing repository.ExistingMediaFile) error {
|
||||
artist, err := s.getOrCreateArtist(song.Artist)
|
||||
if err != nil {
|
||||
return fmt.Errorf("artist: %w", err)
|
||||
@@ -267,20 +276,67 @@ func (s *LibraryService) addScannedSong(song scanner.ScannedSong, libraryID int,
|
||||
return fmt.Errorf("album: %w", err)
|
||||
}
|
||||
|
||||
mediaFile := model.MediaFile{
|
||||
ID: existing.MediaFileID,
|
||||
Path: song.Path,
|
||||
LibraryID: libraryID,
|
||||
FileSize: file.FileSize,
|
||||
FileMtimeNs: file.FileMtimeNs,
|
||||
Format: song.Format,
|
||||
BitRate: song.BitRate,
|
||||
SampleRate: song.SampleRate,
|
||||
LastSeenAt: seenAt,
|
||||
}
|
||||
|
||||
if !hasExistingMedia {
|
||||
mediaFile, err = s.mediaRepo.Create(mediaFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("media file: %w", err)
|
||||
}
|
||||
} else if err := s.mediaRepo.UpdateMetadata(mediaFile); err != nil {
|
||||
return fmt.Errorf("media file: %w", err)
|
||||
}
|
||||
|
||||
if hasExistingMedia && existing.HasSong {
|
||||
if err := s.songRepo.UpdateByMediaFileID(song.Title, artist.ID, album.ID, song.Duration, mediaFile.ID); err != nil {
|
||||
return fmt.Errorf("song: %w", err)
|
||||
}
|
||||
report.Updated++
|
||||
return nil
|
||||
}
|
||||
|
||||
if _, err := s.songRepo.Create(song.Title, artist.ID, album.ID, song.Duration, mediaFile.ID); err != nil {
|
||||
return fmt.Errorf("song: %w", err)
|
||||
}
|
||||
|
||||
report.Added++
|
||||
if hasExistingMedia {
|
||||
report.Updated++
|
||||
} else {
|
||||
report.Added++
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *LibraryService) getOrCreateMediaFile(path string, libraryID int) (model.MediaFile, error) {
|
||||
mf, err := s.mediaRepo.GetByPath(path)
|
||||
if err == nil {
|
||||
return mf, nil
|
||||
func (s *LibraryService) deleteMissingMediaFiles(existingFiles []repository.ExistingMediaFile, seenPaths map[string]struct{}) (int, error) {
|
||||
deleted := 0
|
||||
for _, existing := range existingFiles {
|
||||
if _, ok := seenPaths[existing.Path]; ok {
|
||||
continue
|
||||
}
|
||||
if err := s.mediaRepo.Delete(existing.MediaFileID); err != nil {
|
||||
return deleted, err
|
||||
}
|
||||
deleted++
|
||||
}
|
||||
return s.mediaRepo.Create(path, libraryID)
|
||||
|
||||
if deleted == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
if err := s.libRepo.CleanupOrphans(); err != nil {
|
||||
return deleted, err
|
||||
}
|
||||
return deleted, nil
|
||||
}
|
||||
|
||||
func (s *LibraryService) getOrCreateArtist(name string) (model.Artist, error) {
|
||||
|
||||
Reference in New Issue
Block a user