feat: add local DB to save downloads

This commit is contained in:
Mathis Maquenne
2025-04-26 13:09:59 +02:00
parent 6675834842
commit f931cad746
8 changed files with 215 additions and 64 deletions
+63
View File
@@ -1,5 +1,14 @@
package config
import (
"fmt"
"os"
"path"
"github.com/mathismqn/godeez/internal/utils"
"github.com/spf13/viper"
)
type Config struct {
ArlCookie string `mapstructure:"arl_cookie"`
SecretKey string `mapstructure:"secret_key"`
@@ -7,3 +16,57 @@ type Config struct {
}
var Cfg Config
func Init(cfgFile 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")
if _, err := os.Stat(cfgPath); os.IsNotExist(err) {
fmt.Printf("Config file not found, creating one at %s\n", cfgPath)
content := []byte("arl_cookie = ''\nsecret_key = ''\niv = '0001020304050607'\n")
if err := os.WriteFile(cfgPath, content, 0644); err != nil {
fmt.Fprintf(os.Stderr, "Error: could not create config file: %v\n", err)
os.Exit(1)
}
}
viper.AddConfigPath(appDir)
viper.SetConfigName("config.toml")
}
viper.SetConfigType("toml")
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil {
fmt.Fprintf(os.Stderr, "Error: could not read config file: %v\n", err)
os.Exit(1)
}
cfg := &Cfg
if err := viper.Unmarshal(&cfg); err != nil {
fmt.Fprintf(os.Stderr, "Error: could not unmarshal config file: %v\n", err)
os.Exit(1)
}
if cfg.SecretKey == "" {
fmt.Fprintln(os.Stderr, "Error: secret_key is not set in config file")
os.Exit(1)
}
if cfg.IV == "" {
fmt.Fprintln(os.Stderr, "Error: iv is not set in config file")
os.Exit(1)
}
}
+58
View File
@@ -0,0 +1,58 @@
package db
import (
"encoding/json"
"fmt"
"time"
"go.etcd.io/bbolt"
)
type DownloadInfo struct {
SongID string `json:"song_id"`
Quality string `json:"quality"`
Path string `json:"path"`
Downloaded time.Time `json:"downloaded_at"`
}
func Get(songID string) (*DownloadInfo, error) {
var info DownloadInfo
if err := db.View(func(tx *bbolt.Tx) error {
b := tx.Bucket(trackBucket)
if b == nil {
return fmt.Errorf("bucket not found")
}
data := b.Get([]byte(songID))
if data == nil {
return fmt.Errorf("not found")
}
return json.Unmarshal(data, &info)
}); err != nil {
return nil, err
}
return &info, nil
}
func (d *DownloadInfo) Save() error {
return db.Update(func(tx *bbolt.Tx) error {
b := tx.Bucket(trackBucket)
if b == nil {
return fmt.Errorf("bucket not found")
}
data, err := json.Marshal(d)
if err != nil {
return err
}
err = b.Put([]byte(d.SongID), data)
if err != nil {
return err
}
return nil
})
}
+44
View File
@@ -0,0 +1,44 @@
package db
import (
"fmt"
"os"
"path"
"github.com/mathismqn/godeez/internal/utils"
bolt "go.etcd.io/bbolt"
)
var (
db *bolt.DB
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)
}
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")
db, err = bolt.Open(dbPath, 0600, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: could not open database: %v\n", err)
os.Exit(1)
}
if err := db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists(trackBucket)
return err
}); err != nil {
fmt.Fprintf(os.Stderr, "Error: could not create bucket: %v\n", err)
os.Exit(1)
}
}
+9
View File
@@ -0,0 +1,9 @@
package utils
import "os"
func FileExists(path string) bool {
info, err := os.Stat(path)
return err == nil && !info.IsDir()
}