i love slop

This commit is contained in:
Gabe Farrell 2026-04-04 00:21:03 -04:00
parent 853f08221f
commit 1d69eeb573
9 changed files with 975 additions and 85 deletions

View file

@ -2,22 +2,35 @@ package main
import (
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"
)
// moveToLibrary moves a file to {libDir}/{artist}/[{year}] {album}/filename.
// moveToLibrary moves a file to {libDir}/{artist}/[{date}] {album} [{quality}]/filename.
func moveToLibrary(libDir string, md *MusicMetadata, srcPath string) error {
targetDir := filepath.Join(libDir, sanitize(md.Artist), sanitize(fmt.Sprintf("[%s] %s", md.Year, md.Album)))
date := md.Date
if date == "" {
date = md.Year
}
albumDir := fmt.Sprintf("[%s] %s", date, md.Album)
if md.Quality != "" {
albumDir += fmt.Sprintf(" [%s]", md.Quality)
}
targetDir := filepath.Join(libDir, sanitize(md.Artist), sanitize(albumDir))
if err := os.MkdirAll(targetDir, 0755); err != nil {
return err
}
dst := filepath.Join(targetDir, filepath.Base(srcPath))
fmt.Println("→ Moving:", srcPath, "→", dst)
return os.Rename(srcPath, dst)
if strings.ToLower(os.Getenv("COPYMODE")) == "true" {
return copy(srcPath, dst)
} else {
return os.Rename(srcPath, dst)
}
}
// cluster moves all top-level audio files in dir into subdirectories named
@ -101,3 +114,63 @@ func sanitize(s string) string {
)
return r.Replace(s)
}
// CopyFile copies a file from src to dst. If src and dst files exist, and are
// the same, then return success. Otherise, attempt to create a hard link
// between the two files. If that fail, copy the file contents from src to dst.
func copy(src, dst string) (err error) {
sfi, err := os.Stat(src)
if err != nil {
return
}
if !sfi.Mode().IsRegular() {
// cannot copy non-regular files (e.g., directories,
// symlinks, devices, etc.)
return fmt.Errorf("CopyFile: non-regular source file %s (%q)", sfi.Name(), sfi.Mode().String())
}
dfi, err := os.Stat(dst)
if err != nil {
if !os.IsNotExist(err) {
return
}
} else {
if !(dfi.Mode().IsRegular()) {
return fmt.Errorf("CopyFile: non-regular destination file %s (%q)", dfi.Name(), dfi.Mode().String())
}
if os.SameFile(sfi, dfi) {
return
}
}
if err = os.Link(src, dst); err == nil {
return
}
err = copyFileContents(src, dst)
return
}
// copyFileContents copies the contents of the file named src to the file named
// by dst. The file will be created if it does not already exist. If the
// destination file exists, all it's contents will be replaced by the contents
// of the source file.
func copyFileContents(src, dst string) (err error) {
in, err := os.Open(src)
if err != nil {
return
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return
}
defer func() {
cerr := out.Close()
if err == nil {
err = cerr
}
}()
if _, err = io.Copy(out, in); err != nil {
return
}
err = out.Sync()
return
}