feat: retrieve modified file paths using hashes
This commit is contained in:
+22
-21
@@ -16,8 +16,10 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
var outputDir string
|
var (
|
||||||
var quality string
|
outputDir string
|
||||||
|
quality string
|
||||||
|
)
|
||||||
|
|
||||||
var downloadCmd = &cobra.Command{
|
var downloadCmd = &cobra.Command{
|
||||||
Use: "download",
|
Use: "download",
|
||||||
@@ -32,23 +34,7 @@ func init() {
|
|||||||
|
|
||||||
func validateInput() {
|
func validateInput() {
|
||||||
if outputDir == "" {
|
if outputDir == "" {
|
||||||
homeDir, err := os.UserHomeDir()
|
outputDir = appDir
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if quality == "" {
|
if quality == "" {
|
||||||
@@ -171,11 +157,20 @@ func downloadContent(contentType string, args []string) {
|
|||||||
fileName, _ = filenamify.Filenamify(fileName, filenamify.Options{})
|
fileName, _ = filenamify.Filenamify(fileName, filenamify.Options{})
|
||||||
filePath := path.Join(output, fileName)
|
filePath := path.Join(output, fileName)
|
||||||
|
|
||||||
if existing, err := db.Get(song.ID); err == nil {
|
if existing, err := db.Get(song.ID); err == nil && existing.Quality == media.Data[0].Media[0].Format {
|
||||||
if existing.Quality == media.Data[0].Media[0].Format && utils.FileExists(existing.Path) {
|
if utils.FileExists(existing.Path) {
|
||||||
fmt.Printf(" Skipping %s (already downloaded at %s)\n", songTitle, existing.Path)
|
fmt.Printf(" Skipping %s (already downloaded at %s)\n", songTitle, existing.Path)
|
||||||
continue
|
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)
|
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)
|
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{
|
info := &db.DownloadInfo{
|
||||||
SongID: song.ID,
|
SongID: song.ID,
|
||||||
Quality: media.Data[0].Media[0].Format,
|
Quality: media.Data[0].Media[0].Format,
|
||||||
Path: filePath,
|
Path: filePath,
|
||||||
|
Hash: hash,
|
||||||
Downloaded: time.Now(),
|
Downloaded: time.Now(),
|
||||||
}
|
}
|
||||||
if err := info.Save(); err != nil {
|
if err := info.Save(); err != nil {
|
||||||
|
|||||||
+41
-3
@@ -1,12 +1,23 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/mathismqn/godeez/internal/config"
|
"github.com/mathismqn/godeez/internal/config"
|
||||||
"github.com/mathismqn/godeez/internal/db"
|
"github.com/mathismqn/godeez/internal/db"
|
||||||
|
"github.com/mathismqn/godeez/internal/utils"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
var cfgFile string
|
var (
|
||||||
|
cfgDir string
|
||||||
|
musicDir string
|
||||||
|
appDir string
|
||||||
|
cfgFile string
|
||||||
|
)
|
||||||
|
|
||||||
var RootCmd = &cobra.Command{
|
var RootCmd = &cobra.Command{
|
||||||
Use: "godeez",
|
Use: "godeez",
|
||||||
@@ -19,7 +30,34 @@ var RootCmd = &cobra.Command{
|
|||||||
func init() {
|
func init() {
|
||||||
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.godeez)")
|
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.godeez)")
|
||||||
cobra.OnInitialize(func() {
|
cobra.OnInitialize(func() {
|
||||||
config.Init(cfgFile)
|
initDirs()
|
||||||
db.Init()
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
"github.com/mathismqn/godeez/internal/utils"
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -17,23 +16,11 @@ type Config struct {
|
|||||||
|
|
||||||
var Cfg Config
|
var Cfg Config
|
||||||
|
|
||||||
func Init(cfgFile string) {
|
func Init(cfgFile, cfgDir string) {
|
||||||
if cfgFile != "" {
|
if cfgFile != "" {
|
||||||
viper.SetConfigFile(cfgFile)
|
viper.SetConfigFile(cfgFile)
|
||||||
} else {
|
} else {
|
||||||
homeDir, err := os.UserHomeDir()
|
cfgPath := path.Join(cfgDir, "config.toml")
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "Error: could not get home directory: %v\n", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
appDir := path.Join(homeDir, ".godeez")
|
|
||||||
if err := utils.EnsureDir(appDir); err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "Error: could not create app directory: %v\n", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
cfgPath := path.Join(appDir, "config.toml")
|
|
||||||
if _, err := os.Stat(cfgPath); os.IsNotExist(err) {
|
if _, err := os.Stat(cfgPath); os.IsNotExist(err) {
|
||||||
fmt.Printf("Config file not found, creating one at %s\n", cfgPath)
|
fmt.Printf("Config file not found, creating one at %s\n", cfgPath)
|
||||||
|
|
||||||
@@ -44,7 +31,7 @@ func Init(cfgFile string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
viper.AddConfigPath(appDir)
|
viper.AddConfigPath(cfgDir)
|
||||||
viper.SetConfigName("config.toml")
|
viper.SetConfigName("config.toml")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ type DownloadInfo struct {
|
|||||||
SongID string `json:"song_id"`
|
SongID string `json:"song_id"`
|
||||||
Quality string `json:"quality"`
|
Quality string `json:"quality"`
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
|
Hash string `json:"hash"`
|
||||||
Downloaded time.Time `json:"downloaded_at"`
|
Downloaded time.Time `json:"downloaded_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+3
-14
@@ -5,7 +5,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
"github.com/mathismqn/godeez/internal/utils"
|
|
||||||
bolt "go.etcd.io/bbolt"
|
bolt "go.etcd.io/bbolt"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -14,20 +13,10 @@ var (
|
|||||||
trackBucket = []byte("tracks")
|
trackBucket = []byte("tracks")
|
||||||
)
|
)
|
||||||
|
|
||||||
func Init() {
|
func Init(cfgDir string) {
|
||||||
homeDir, err := os.UserHomeDir()
|
var err error
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "Error: could not get home directory: %v\n", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
appDir := path.Join(homeDir, ".godeez")
|
dbPath := path.Join(cfgDir, "tracks.db")
|
||||||
if err := utils.EnsureDir(appDir); err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "Error: could not create app directory: %v\n", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
dbPath := path.Join(appDir, "tracks.db")
|
|
||||||
db, err = bolt.Open(dbPath, 0600, nil)
|
db, err = bolt.Open(dbPath, 0600, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Error: could not open database: %v\n", err)
|
fmt.Fprintf(os.Stderr, "Error: could not open database: %v\n", err)
|
||||||
|
|||||||
+57
-1
@@ -1,6 +1,13 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import "os"
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
func FileExists(path string) bool {
|
func FileExists(path string) bool {
|
||||||
info, err := os.Stat(path)
|
info, err := os.Stat(path)
|
||||||
@@ -15,3 +22,52 @@ func DeleteFile(path string) error {
|
|||||||
|
|
||||||
return os.Remove(path)
|
return os.Remove(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetFileHash(path string) (string, error) {
|
||||||
|
file, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
hash := sha256.New()
|
||||||
|
if _, err := io.Copy(hash, file); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("%x", hash.Sum(nil)), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func FindFileByHash(root, targetHash string) (string, error) {
|
||||||
|
var found string
|
||||||
|
|
||||||
|
if err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if info.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
h := sha256.New()
|
||||||
|
if _, err := io.Copy(h, f); err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
sum := hex.EncodeToString(h.Sum(nil))
|
||||||
|
if sum == targetHash {
|
||||||
|
found = path
|
||||||
|
return filepath.SkipDir
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return found, nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user