copy cover art to album dir

This commit is contained in:
2026-04-19 11:50:20 +02:00
parent cd7c3057b6
commit 5373a828e3

View File

@@ -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
}