feat: retrieve modified file paths using hashes
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/mathismqn/godeez/internal/utils"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
@@ -17,23 +16,11 @@ type Config struct {
|
||||
|
||||
var Cfg Config
|
||||
|
||||
func Init(cfgFile string) {
|
||||
func Init(cfgFile, cfgDir string) {
|
||||
if cfgFile != "" {
|
||||
viper.SetConfigFile(cfgFile)
|
||||
} else {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
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")
|
||||
cfgPath := path.Join(cfgDir, "config.toml")
|
||||
if _, err := os.Stat(cfgPath); os.IsNotExist(err) {
|
||||
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")
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ type DownloadInfo struct {
|
||||
SongID string `json:"song_id"`
|
||||
Quality string `json:"quality"`
|
||||
Path string `json:"path"`
|
||||
Hash string `json:"hash"`
|
||||
Downloaded time.Time `json:"downloaded_at"`
|
||||
}
|
||||
|
||||
|
||||
+3
-14
@@ -5,7 +5,6 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/mathismqn/godeez/internal/utils"
|
||||
bolt "go.etcd.io/bbolt"
|
||||
)
|
||||
|
||||
@@ -14,20 +13,10 @@ var (
|
||||
trackBucket = []byte("tracks")
|
||||
)
|
||||
|
||||
func Init() {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: could not get home directory: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
func Init(cfgDir string) {
|
||||
var err error
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
dbPath := path.Join(appDir, "tracks.db")
|
||||
dbPath := path.Join(cfgDir, "tracks.db")
|
||||
db, err = bolt.Open(dbPath, 0600, nil)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: could not open database: %v\n", err)
|
||||
|
||||
+57
-1
@@ -1,6 +1,13 @@
|
||||
package utils
|
||||
|
||||
import "os"
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func FileExists(path string) bool {
|
||||
info, err := os.Stat(path)
|
||||
@@ -15,3 +22,52 @@ func DeleteFile(path string) error {
|
||||
|
||||
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