2019-02-09 12:30:49 +00:00
|
|
|
package manager
|
|
|
|
|
|
|
|
import (
|
2019-03-23 14:56:59 +00:00
|
|
|
"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"
|
2019-03-23 14:56:59 +00:00
|
|
|
"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 {
|
2019-03-23 14:56:59 +00:00
|
|
|
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() {
|
2019-03-23 14:56:59 +00:00
|
|
|
_ = utils.EnsureDir(paths.GetConfigDirectory())
|
|
|
|
initConfig()
|
2019-02-09 12:30:49 +00:00
|
|
|
instance = &singleton{
|
2019-03-23 14:56:59 +00:00
|
|
|
Status: Idle,
|
|
|
|
Paths: paths.NewPaths(),
|
|
|
|
JSON: &jsonUtils{},
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
2019-02-11 06:39:21 +00:00
|
|
|
|
2019-03-23 14:56:59 +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
|
|
|
|
}
|
|
|
|
|
2019-03-23 14:56:59 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-23 21:09:05 +00:00
|
|
|
viper.SetDefault(config.Database, paths.GetDefaultDatabaseFilePath())
|
|
|
|
|
|
|
|
// Set generated to the metadata path for backwards compat
|
|
|
|
viper.SetDefault(config.Generated, viper.GetString(config.Metadata))
|
|
|
|
|
2019-03-23 14:56:59 +00:00
|
|
|
// 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() {
|
2019-03-23 14:56:59 +00:00
|
|
|
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")
|
2019-03-23 14:56:59 +00:00
|
|
|
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
|
|
|
|
`
|
2019-03-23 14:56:59 +00:00
|
|
|
logger.Fatalf(msg, configDirectory, err)
|
2019-02-09 12:30:49 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-11 07:35:53 +00:00
|
|
|
|
2019-03-23 14:56:59 +00:00
|
|
|
// TODO: is this valid after download?
|
|
|
|
instance.FFMPEGPath = ffmpegPath
|
|
|
|
instance.FFProbePath = ffprobePath
|
2019-02-11 10:49:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-23 14:56:59 +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)
|
|
|
|
}
|
|
|
|
}
|