ci / contract / contract (сгенерированный код совпадает с openapi.yaml) (push) Successful in 1m17s
ci / ts / ts (packages + web) (push) Successful in 3m13s
ci / api / api (push) Successful in 59s
ci / mobile / mobile (Metro-бандл Android) (push) Successful in 1m31s
ci / db / db (push) Successful in 2m50s
50 lines
1.6 KiB
Go
50 lines
1.6 KiB
Go
package library
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"pipu/api/internal/api/gen"
|
|
)
|
|
|
|
type CoverStats struct {
|
|
FilesTotal int64
|
|
FilesWithCover int64
|
|
AlbumsPending int64
|
|
}
|
|
|
|
// SourceCoverStats counts physical audio files, including alternate encodings.
|
|
// Only completed local covers count; external image URLs are intentionally ignored.
|
|
func (st *Store) SourceCoverStats(ctx context.Context, id uuid.UUID) (CoverStats, error) {
|
|
var stats CoverStats
|
|
err := st.pool.QueryRow(ctx, `
|
|
SELECT count(*), count(*) FILTER (WHERE al.cover_path IS NOT NULL),
|
|
count(DISTINCT al.id) FILTER (WHERE al.cover_checked_at IS NULL)
|
|
FROM media_files mf
|
|
JOIN library_sources s ON s.id = mf.source_id
|
|
LEFT JOIN tracks t ON t.id = mf.track_id
|
|
LEFT JOIN albums al ON al.id = t.album_id
|
|
WHERE mf.source_id = $1 AND mf.missing_since IS NULL AND mf.scan_error IS NULL
|
|
AND NOT library_path_excluded(mf.rel_path, s.excluded_dirs)`, id).
|
|
Scan(&stats.FilesTotal, &stats.FilesWithCover, &stats.AlbumsPending)
|
|
if err != nil {
|
|
return CoverStats{}, fmt.Errorf("library: source cover stats: %w", err)
|
|
}
|
|
return stats, nil
|
|
}
|
|
|
|
func (h *Handler) GetSourceCoverStats(ctx context.Context, req gen.GetSourceCoverStatsRequestObject) (gen.GetSourceCoverStatsResponseObject, error) {
|
|
if _, err := h.svc.GetSource(ctx, req.Id); err != nil {
|
|
return nil, err
|
|
}
|
|
stats, err := h.store.SourceCoverStats(ctx, req.Id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return gen.GetSourceCoverStats200JSONResponse{
|
|
FilesTotal: stats.FilesTotal, FilesWithCover: stats.FilesWithCover, AlbumsPending: stats.AlbumsPending,
|
|
}, nil
|
|
}
|