stash/pkg/manager/manager.go

117 lines
2.9 KiB
Go
Raw Normal View History

2019-02-09 12:30:49 +00:00
package manager
import (
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
2019-02-14 23:42:52 +00:00
"github.com/stashapp/stash/pkg/ffmpeg"
"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/manager/config"
2019-02-14 23:42:52 +00:00
"github.com/stashapp/stash/pkg/manager/paths"
"github.com/stashapp/stash/pkg/utils"
2019-02-09 12:30:49 +00:00
"sync"
)
type singleton struct {
Status JobStatus
Paths *paths.Paths
JSON *jsonUtils
FFMPEGPath string
FFProbePath string
2019-02-09 12:30:49 +00:00
}
var instance *singleton
var once sync.Once
func GetInstance() *singleton {
Initialize()
return instance
}
func Initialize() *singleton {
once.Do(func() {
_ = utils.EnsureDir(paths.GetConfigDirectory())
initConfig()
2019-02-09 12:30:49 +00:00
instance = &singleton{
Status: Idle,
Paths: paths.NewPaths(),
JSON: &jsonUtils{},
2019-02-09 12:30:49 +00:00
}
2019-02-11 06:39:21 +00:00
instance.refreshConfig()
2019-02-11 10:49:39 +00:00
2019-02-11 06:39:21 +00:00
initFFMPEG()
2019-02-09 12:30:49 +00:00
})
return instance
}
func initConfig() {
// The config file is called config. Leave off the file extension.
viper.SetConfigName("config")
viper.AddConfigPath("$HOME/.stash") // Look for the config in the home directory
viper.AddConfigPath(".") // Look for config in the working directory
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
_ = utils.Touch(paths.GetDefaultConfigFilePath())
if err = viper.ReadInConfig(); err != nil {
panic(err)
}
}
viper.SetDefault(config.Database, paths.GetDefaultDatabaseFilePath())
// Set generated to the metadata path for backwards compat
viper.SetDefault(config.Generated, viper.GetString(config.Metadata))
// Watch for changes
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("Config file changed:", e.Name)
instance.refreshConfig()
})
//viper.Set("stash", []string{"/", "/stuff"})
//viper.WriteConfig()
}
2019-02-11 06:39:21 +00:00
func initFFMPEG() {
configDirectory := paths.GetConfigDirectory()
ffmpegPath, ffprobePath := ffmpeg.GetPaths(configDirectory)
2019-02-11 06:39:21 +00:00
if ffmpegPath == "" || ffprobePath == "" {
logger.Infof("couldn't find FFMPEG, attempting to download it")
if err := ffmpeg.Download(configDirectory); err != nil {
2019-02-11 06:39:21 +00:00
msg := `Unable to locate / automatically download FFMPEG
2019-02-09 12:30:49 +00:00
2019-02-11 06:39:21 +00:00
Check the readme for download links.
The FFMPEG and FFProbe binaries should be placed in %s
2019-02-09 12:30:49 +00:00
2019-02-11 06:39:21 +00:00
The error was: %s
`
logger.Fatalf(msg, configDirectory, err)
2019-02-09 12:30:49 +00:00
}
}
2019-02-11 07:35:53 +00:00
// TODO: is this valid after download?
instance.FFMPEGPath = ffmpegPath
instance.FFProbePath = ffprobePath
2019-02-11 10:49:39 +00:00
}
func (s *singleton) refreshConfig() {
s.Paths = paths.NewPaths()
if config.IsValid() {
2019-02-11 10:49:39 +00:00
_ = utils.EnsureDir(s.Paths.Generated.Screenshots)
_ = utils.EnsureDir(s.Paths.Generated.Vtt)
_ = utils.EnsureDir(s.Paths.Generated.Markers)
_ = utils.EnsureDir(s.Paths.Generated.Transcodes)
_ = utils.EnsureDir(s.Paths.JSON.Performers)
_ = utils.EnsureDir(s.Paths.JSON.Scenes)
_ = utils.EnsureDir(s.Paths.JSON.Galleries)
_ = utils.EnsureDir(s.Paths.JSON.Studios)
}
}