feat: retrieve modified file paths using hashes
This commit is contained in:
+22
-21
@@ -16,8 +16,10 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var outputDir string
|
||||
var quality string
|
||||
var (
|
||||
outputDir string
|
||||
quality string
|
||||
)
|
||||
|
||||
var downloadCmd = &cobra.Command{
|
||||
Use: "download",
|
||||
@@ -32,23 +34,7 @@ func init() {
|
||||
|
||||
func validateInput() {
|
||||
if outputDir == "" {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: could not get home directory: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
musicDir := path.Join(homeDir, "Music")
|
||||
if err := utils.EnsureDir(musicDir); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: could not create music directory: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
outputDir = path.Join(musicDir, "GoDeez")
|
||||
if err := utils.EnsureDir(outputDir); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: could not create GoDeez directory: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
outputDir = appDir
|
||||
}
|
||||
|
||||
if quality == "" {
|
||||
@@ -171,11 +157,20 @@ func downloadContent(contentType string, args []string) {
|
||||
fileName, _ = filenamify.Filenamify(fileName, filenamify.Options{})
|
||||
filePath := path.Join(output, fileName)
|
||||
|
||||
if existing, err := db.Get(song.ID); err == nil {
|
||||
if existing.Quality == media.Data[0].Media[0].Format && utils.FileExists(existing.Path) {
|
||||
if existing, err := db.Get(song.ID); err == nil && existing.Quality == media.Data[0].Media[0].Format {
|
||||
if utils.FileExists(existing.Path) {
|
||||
fmt.Printf(" Skipping %s (already downloaded at %s)\n", songTitle, existing.Path)
|
||||
continue
|
||||
}
|
||||
if existing.Hash != "" {
|
||||
if foundPath, err := utils.FindFileByHash(appDir, existing.Hash); err == nil && foundPath != "" {
|
||||
existing.Path = foundPath
|
||||
_ = existing.Save()
|
||||
fmt.Printf(" Recovered %s at %s, skipping download\n", songTitle, foundPath)
|
||||
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf(" Downloading %s...", songTitle)
|
||||
@@ -202,10 +197,16 @@ func downloadContent(contentType string, args []string) {
|
||||
fmt.Fprintf(os.Stderr, "Warning: could not add tags to song: %v\n", err)
|
||||
}
|
||||
|
||||
hash, err := utils.GetFileHash(filePath)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: could not get file hash: %v\n", err)
|
||||
}
|
||||
|
||||
info := &db.DownloadInfo{
|
||||
SongID: song.ID,
|
||||
Quality: media.Data[0].Media[0].Format,
|
||||
Path: filePath,
|
||||
Hash: hash,
|
||||
Downloaded: time.Now(),
|
||||
}
|
||||
if err := info.Save(); err != nil {
|
||||
|
||||
+41
-3
@@ -1,12 +1,23 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/mathismqn/godeez/internal/config"
|
||||
"github.com/mathismqn/godeez/internal/db"
|
||||
"github.com/mathismqn/godeez/internal/utils"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var cfgFile string
|
||||
var (
|
||||
cfgDir string
|
||||
musicDir string
|
||||
appDir string
|
||||
cfgFile string
|
||||
)
|
||||
|
||||
var RootCmd = &cobra.Command{
|
||||
Use: "godeez",
|
||||
@@ -19,7 +30,34 @@ var RootCmd = &cobra.Command{
|
||||
func init() {
|
||||
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.godeez)")
|
||||
cobra.OnInitialize(func() {
|
||||
config.Init(cfgFile)
|
||||
db.Init()
|
||||
initDirs()
|
||||
config.Init(cfgFile, cfgDir)
|
||||
db.Init(cfgDir)
|
||||
})
|
||||
}
|
||||
|
||||
func initDirs() {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: could not get home directory: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
cfgDir = filepath.Join(home, ".godeez")
|
||||
if err := utils.EnsureDir(cfgDir); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: could not create app directory: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
musicDir = filepath.Join(home, "Music")
|
||||
if err := utils.EnsureDir(musicDir); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: could not create music directory: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
appDir = path.Join(musicDir, "GoDeez")
|
||||
if err := utils.EnsureDir(appDir); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: could not create GoDeez directory: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user