2021-09-28 21:17:54 +09:30
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2021-09-29 23:15:44 +09:30
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2021-09-28 21:17:54 +09:30
|
|
|
"log"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2021-09-28 22:09:12 +09:30
|
|
|
Port int `yaml:"port" json:"port"`
|
|
|
|
Address string `yaml:"address" json:"address"`
|
|
|
|
DownloadPath string `yaml:"download_path" json:"download_path"`
|
2021-09-28 21:17:54 +09:30
|
|
|
}
|
|
|
|
|
|
|
|
type DownloadProfile struct {
|
2021-09-28 22:09:12 +09:30
|
|
|
Name string `yaml:"name" json:"name"`
|
|
|
|
Command string `yaml:"command" json:"command"`
|
|
|
|
Args []string `yaml:"args" json:"args"`
|
2021-09-28 21:17:54 +09:30
|
|
|
}
|
|
|
|
|
|
|
|
type UI struct {
|
2021-09-28 22:09:12 +09:30
|
|
|
PopupWidth int `yaml:"popup_width" json:"popup_width"`
|
|
|
|
PopupHeight int `yaml:"popup_height" json:"popup_height"`
|
2021-09-28 21:17:54 +09:30
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
2021-09-28 22:09:12 +09:30
|
|
|
Server Server `yaml:"server" json:"server"`
|
|
|
|
UI UI `yaml:"ui" json:"ui"`
|
|
|
|
DownloadProfiles []DownloadProfile `yaml:"profiles" json:"profiles"`
|
|
|
|
ConfigVersion int `yaml:"config_version" json:"config_version"`
|
2021-09-28 21:17:54 +09:30
|
|
|
}
|
|
|
|
|
2021-09-29 23:15:44 +09:30
|
|
|
func DefaultConfig() *Config {
|
2021-09-28 21:17:54 +09:30
|
|
|
defaultConfig := Config{}
|
|
|
|
stdProfile := DownloadProfile{Name: "standard youtube-dl video", Command: "youtube-dl", Args: []string{
|
|
|
|
"--newline",
|
|
|
|
"--write-info-json",
|
|
|
|
"-f",
|
|
|
|
"bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best",
|
|
|
|
}}
|
|
|
|
|
|
|
|
defaultConfig.DownloadProfiles = append(defaultConfig.DownloadProfiles, stdProfile)
|
2021-09-29 23:15:44 +09:30
|
|
|
defaultConfig.DownloadProfiles = append(defaultConfig.DownloadProfiles, stdProfile)
|
|
|
|
|
2021-09-28 21:17:54 +09:30
|
|
|
defaultConfig.Server.Port = 6123
|
2021-09-28 22:09:12 +09:30
|
|
|
defaultConfig.Server.Address = "http://localhost:6123"
|
2021-09-28 21:17:54 +09:30
|
|
|
defaultConfig.Server.DownloadPath = "./"
|
2021-09-28 22:09:12 +09:30
|
|
|
|
2021-09-28 21:17:54 +09:30
|
|
|
defaultConfig.UI.PopupWidth = 500
|
|
|
|
defaultConfig.UI.PopupHeight = 500
|
|
|
|
|
2021-09-28 22:09:12 +09:30
|
|
|
defaultConfig.ConfigVersion = 1
|
|
|
|
|
2021-09-29 23:15:44 +09:30
|
|
|
return &defaultConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) UpdateFromJSON(j []byte) error {
|
|
|
|
newConfig := Config{}
|
|
|
|
err := json.Unmarshal(j, &newConfig)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Unmarshal error in config: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Printf("Config is unmarshalled ok")
|
|
|
|
|
|
|
|
// other checks
|
|
|
|
if newConfig.UI.PopupHeight < 100 || newConfig.UI.PopupHeight > 2000 {
|
|
|
|
return errors.New("bad popup height")
|
|
|
|
}
|
|
|
|
|
|
|
|
*c = newConfig
|
|
|
|
return nil
|
2021-09-28 21:17:54 +09:30
|
|
|
}
|
|
|
|
|
|
|
|
func WriteDefaultConfig(path string) {
|
|
|
|
defaultConfig := DefaultConfig()
|
|
|
|
s, err := yaml.Marshal(&defaultConfig)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
log.Print(string(s))
|
|
|
|
}
|