diff --git a/internal/sorter.go b/internal/sorter.go index fde3fe8..aa9e67b 100644 --- a/internal/sorter.go +++ b/internal/sorter.go @@ -3,6 +3,7 @@ package ripsort import ( "fmt" "io" + "log/slog" "os" "path/filepath" "regexp" @@ -27,11 +28,24 @@ func sortSong(src, dst string) error { dstPath := pathForFile(src, *m) finalPath := filepath.Join(dst, dstPath) + slog.Info("Copying song", "file", src, "dst", dstPath) err = copyFile(src, finalPath) if err != nil { return err } + extraFiles := checkForExtraFiles(src) + + for _, extraFile := range extraFiles { + extraFilePath := pathForFile(extraFile, *m) + finalExtraFilePath := filepath.Join(dst, extraFilePath) + slog.Info("Copy extra file", "file", extraFile, "dst", extraFilePath) + err = copyFile(extraFile, finalExtraFilePath) + if err != nil { + slog.Warn("Failed to copy extra file", "file", extraFile, "err", err) + } + } + return nil } @@ -132,3 +146,19 @@ func copyFile(src, dst string) error { return nil } + +func checkForExtraFiles(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 extraFiles +}