Deduplicate GetAlbum response: merge release info into AlbumDetail, track release into TrackDetail
This commit is contained in:
+61
-70
@@ -289,6 +289,44 @@ func (service *MusicAgregatorService) buildAlbumInfo(ctx context.Context, dbAlbu
|
||||
dbTracksByExternalID[t.ExternalID] = t
|
||||
}
|
||||
|
||||
var trackReleasesByTrackID map[string]*database.TrackRelease
|
||||
albumReleases, err := service.albumReleases.GetByAlbumID(ctx, dbAlbum.ID)
|
||||
if err == nil && len(albumReleases) > 0 {
|
||||
ar := albumReleases[0]
|
||||
album.Release = &pb.AlbumReleaseDetail{
|
||||
Id: ar.ID,
|
||||
Format: ar.Format,
|
||||
Channels: int32(ar.Channels),
|
||||
IsLossless: ar.IsLossless,
|
||||
TotalSize: ar.TotalSize,
|
||||
TotalDurationMs: int32(ar.TotalDurationMs),
|
||||
TrackCount: int32(ar.TrackCount),
|
||||
HasCoverArt: ar.HasCoverArt,
|
||||
HasCueSheet: ar.HasCueSheet,
|
||||
HasRipLog: ar.HasRipLog,
|
||||
Path: ar.Path,
|
||||
}
|
||||
if ar.BitDepth != nil {
|
||||
album.Release.BitDepth = int32(*ar.BitDepth)
|
||||
}
|
||||
if ar.SampleRate != nil {
|
||||
album.Release.SampleRate = int32(*ar.SampleRate)
|
||||
}
|
||||
if ar.Source != nil {
|
||||
album.Release.Source = *ar.Source
|
||||
}
|
||||
|
||||
trs, err := service.trackReleases.GetByAlbumReleaseID(ctx, ar.ID)
|
||||
if err == nil {
|
||||
trackReleasesByTrackID = make(map[string]*database.TrackRelease, len(trs))
|
||||
for _, tr := range trs {
|
||||
if tr.TrackID != nil {
|
||||
trackReleasesByTrackID[*tr.TrackID] = tr
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tracks := make([]*pb.TrackDetail, 0, len(metadataTracks))
|
||||
for _, mt := range metadataTracks {
|
||||
td := &pb.TrackDetail{
|
||||
@@ -310,85 +348,38 @@ func (service *MusicAgregatorService) buildAlbumInfo(ctx context.Context, dbAlbu
|
||||
|
||||
if dbTrack, ok := dbTracksByExternalID[mt.GetId()]; ok {
|
||||
td.Id = dbTrack.ID
|
||||
if df, ok := downloadFilesByTrackID[dbTrack.ID]; ok {
|
||||
td.File = &pb.TrackFile{
|
||||
Path: df.FilePath,
|
||||
Format: df.FileType,
|
||||
Size: df.FileSize,
|
||||
|
||||
if tr, ok := trackReleasesByTrackID[dbTrack.ID]; ok {
|
||||
td.FilePath = tr.FilePath
|
||||
td.FileSize = tr.FileSize
|
||||
td.Format = tr.Format
|
||||
td.Channels = int32(tr.Channels)
|
||||
if tr.DurationMs != nil {
|
||||
td.DurationMs = int32(*tr.DurationMs)
|
||||
}
|
||||
if tr.BitDepth != nil {
|
||||
td.BitDepth = int32(*tr.BitDepth)
|
||||
}
|
||||
if tr.SampleRate != nil {
|
||||
td.SampleRate = int32(*tr.SampleRate)
|
||||
}
|
||||
if tr.BitrateKbps != nil {
|
||||
td.BitrateKbps = int32(*tr.BitrateKbps)
|
||||
}
|
||||
} else if df, ok := downloadFilesByTrackID[dbTrack.ID]; ok {
|
||||
td.FilePath = df.FilePath
|
||||
td.FileSize = df.FileSize
|
||||
td.Format = df.FileType
|
||||
}
|
||||
}
|
||||
|
||||
tracks = append(tracks, td)
|
||||
}
|
||||
|
||||
info := &pb.AlbumInfo{
|
||||
return &pb.AlbumInfo{
|
||||
Album: album,
|
||||
Tracks: tracks,
|
||||
}
|
||||
|
||||
albumReleases, err := service.albumReleases.GetByAlbumID(ctx, dbAlbum.ID)
|
||||
if err == nil && len(albumReleases) > 0 {
|
||||
ar := albumReleases[0]
|
||||
releaseDetail := &pb.AlbumReleaseDetail{
|
||||
Id: ar.ID,
|
||||
Format: ar.Format,
|
||||
Channels: int32(ar.Channels),
|
||||
IsLossless: ar.IsLossless,
|
||||
TotalSize: ar.TotalSize,
|
||||
TotalDurationMs: int32(ar.TotalDurationMs),
|
||||
TrackCount: int32(ar.TrackCount),
|
||||
HasCoverArt: ar.HasCoverArt,
|
||||
HasCueSheet: ar.HasCueSheet,
|
||||
HasRipLog: ar.HasRipLog,
|
||||
Path: ar.Path,
|
||||
}
|
||||
if ar.BitDepth != nil {
|
||||
releaseDetail.BitDepth = int32(*ar.BitDepth)
|
||||
}
|
||||
if ar.SampleRate != nil {
|
||||
releaseDetail.SampleRate = int32(*ar.SampleRate)
|
||||
}
|
||||
if ar.Source != nil {
|
||||
releaseDetail.Source = *ar.Source
|
||||
}
|
||||
|
||||
trackReleases, err := service.trackReleases.GetByAlbumReleaseID(ctx, ar.ID)
|
||||
if err == nil {
|
||||
for _, tr := range trackReleases {
|
||||
trd := &pb.TrackReleaseDetail{
|
||||
Id: tr.ID,
|
||||
Title: tr.Title,
|
||||
TrackNumber: int32(tr.TrackNumber),
|
||||
DiscNumber: int32(tr.DiscNumber),
|
||||
Format: tr.Format,
|
||||
Channels: int32(tr.Channels),
|
||||
FileSize: tr.FileSize,
|
||||
FilePath: tr.FilePath,
|
||||
}
|
||||
if tr.TrackID != nil {
|
||||
trd.TrackId = *tr.TrackID
|
||||
}
|
||||
if tr.DurationMs != nil {
|
||||
trd.DurationMs = int32(*tr.DurationMs)
|
||||
}
|
||||
if tr.BitDepth != nil {
|
||||
trd.BitDepth = int32(*tr.BitDepth)
|
||||
}
|
||||
if tr.SampleRate != nil {
|
||||
trd.SampleRate = int32(*tr.SampleRate)
|
||||
}
|
||||
if tr.BitrateKbps != nil {
|
||||
trd.BitrateKbps = int32(*tr.BitrateKbps)
|
||||
}
|
||||
releaseDetail.Tracks = append(releaseDetail.Tracks, trd)
|
||||
}
|
||||
}
|
||||
|
||||
info.Release = releaseDetail
|
||||
}
|
||||
|
||||
return info, nil
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (service *MusicAgregatorService) AnalyzeAlbumRelease(ctx context.Context, req *pb.AnalyzeAlbumReleaseRequest) (*pb.AnalyzeAlbumReleaseResponse, error) {
|
||||
|
||||
Reference in New Issue
Block a user