From 5373a828e3ac63a723887dfd7ba88f1aa8fbfac0 Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Sun, 19 Apr 2026 11:50:20 +0200 Subject: [PATCH] copy cover art to album dir --- internal/sorter.go | 64 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 12 deletions(-) diff --git a/internal/sorter.go b/internal/sorter.go index db89e6c..e28da5e 100644 --- a/internal/sorter.go +++ b/internal/sorter.go @@ -56,18 +56,22 @@ func sortSong(src, dst string, updateMeta bool) error { } } - extraFiles := checkForExtraFiles(src) - - for _, extraFile := range extraFiles { - extraFilePath := pathForFile(extraFile, *m) + lrcFile := checkForLrcFile(src) + if lrcFile != nil { + extraFilePath := pathForFile(*lrcFile, *m) finalExtraFilePath := filepath.Join(dst, extraFilePath) - slog.Info("Copy extra file", "file", extraFile, "dst", extraFilePath) - err = copyFile(extraFile, finalExtraFilePath) + slog.Info("Copy lrc file", "file", lrcFile, "dst", extraFilePath) + err = copyFile(*lrcFile, finalExtraFilePath) if err != nil { - slog.Warn("Failed to copy extra file", "file", extraFile, "err", err) + slog.Warn("Failed to copy lrc file", "file", lrcFile, "err", err) } } + err = copyAlbumCover(src, finalPath) + if err != nil { + return err + } + return nil } @@ -186,20 +190,18 @@ func copyFile(src, dst string) error { return nil } -func checkForExtraFiles(originalPath string) []string { +func checkForLrcFile(originalPath string) *string { dir := filepath.Dir(originalPath) fileName := filepath.Base(originalPath) fileNameNoExt := strings.TrimSuffix(fileName, filepath.Ext(fileName)) - extraFiles := []string{} - lrcPath := filepath.Join(dir, fileNameNoExt+".lrc") stat, err := os.Stat(lrcPath) if err == nil && !stat.IsDir() { - extraFiles = append(extraFiles, lrcPath) + return &lrcPath } - return extraFiles + return nil } func fileExists(file string) (bool, error) { @@ -215,3 +217,41 @@ func fileExists(file string) (bool, error) { return true, nil } + +func copyAlbumCover(originalPath, finalPath string) error { + const coverFilename string = "cover.jpg" + finalAlbumDir := filepath.Dir(finalPath) + finalAlbumCoverPath := filepath.Join(finalAlbumDir, coverFilename) + + alreadyExists, err := fileExists(finalAlbumCoverPath) + if err != nil { + return err + } + + if alreadyExists { + slog.Debug("Cover already exists", "file", finalAlbumCoverPath) + return nil + } + + originalDir := filepath.Dir(originalPath) + originalFilename := filepath.Base(originalPath) + originalFilenameNoExt := strings.TrimSuffix(originalFilename, filepath.Ext(originalFilename)) + originalAlumCover := filepath.Join(originalDir, originalFilenameNoExt+".jpg") + + exists, err := fileExists(originalAlumCover) + if err != nil { + return err + } + + if !exists { + slog.Debug("No cover found", "file", originalPath) + return nil + } + + err = copyFile(originalAlumCover, finalAlbumCoverPath) + if err != nil { + return err + } + + return nil +}