Fix duplicate download insert: handle NULL columns in download scan, check by torrent ID, enrich MonitorAlbum response, recover orphaned downloads on startup

This commit is contained in:
Alexander
2026-05-09 20:13:43 +02:00
parent cca404bcc0
commit 3ce6e23421
2 changed files with 29 additions and 13 deletions
+14 -11
View File
@@ -170,7 +170,7 @@ func (service *MusicAgregatorService) buildAlbumsForArtist(ctx context.Context,
State: best.State,
Format: best.Format,
Quality: best.Quality,
SavePath: best.SavePath,
SavePath: derefStr(best.SavePath),
}
}
} else {
@@ -228,7 +228,7 @@ func (service *MusicAgregatorService) GetAlbum(ctx context.Context, req *pb.GetA
State: best.State,
Format: best.Format,
Quality: best.Quality,
SavePath: best.SavePath,
SavePath: derefStr(best.SavePath),
}
}
@@ -514,14 +514,10 @@ func (service *MusicAgregatorService) saveTorrentAndDownload(ctx context.Context
return
}
existingDownloads, err := service.downloads.GetByAlbumID(ctx, dbAlbumID)
if err == nil {
for _, d := range existingDownloads {
if d.TorrentID == savedTorrent.ID && d.State != "failed" {
log.Info().Str("hash", best.rel.InfoHash).Str("state", d.State).Msg("active download already exists, skipping")
return
}
}
existingDownload, err := service.downloads.GetActiveByTorrentID(ctx, savedTorrent.ID)
if err == nil && existingDownload != nil {
log.Info().Str("hash", best.rel.InfoHash).Str("state", existingDownload.State).Msg("active download already exists, skipping")
return
}
download := &database.Download{
@@ -647,7 +643,7 @@ func (service *MusicAgregatorService) buildAlbumDetail(ctx context.Context, dbAl
State: best.State,
Format: best.Format,
Quality: best.Quality,
SavePath: best.SavePath,
SavePath: derefStr(best.SavePath),
}
}
@@ -667,6 +663,13 @@ func toProtoMonitorState(state database.MonitorState) pb.MonitorState {
}
}
func derefStr(s *string) string {
if s == nil {
return ""
}
return *s
}
func downloadTorrentData(url string) ([]byte, error) {
client := &http.Client{Timeout: 30 * time.Second}
resp, err := client.Get(url)