owncast/core/core.go
Bradley Hilton dd9267f1ee
Reset the directories when the stream gets disconnected (#152)
* Reset the directories when the stream gets disconnected

* Cleanup after a delay

Co-authored-by: Gabe Kangas <gabek@real-ity.com>
2020-09-16 13:31:21 -07:00

102 lines
2.3 KiB
Go

package core
import (
"os"
"path"
"strconv"
"time"
log "github.com/sirupsen/logrus"
"github.com/gabek/owncast/config"
"github.com/gabek/owncast/core/chat"
"github.com/gabek/owncast/core/ffmpeg"
"github.com/gabek/owncast/models"
"github.com/gabek/owncast/utils"
)
var (
_stats *models.Stats
_storage models.ChunkStorageProvider
_cleanupTicker *time.Ticker
)
//Start starts up the core processing
func Start() error {
resetDirectories()
if err := setupStats(); err != nil {
log.Error("failed to setup the stats")
return err
}
if err := setupStorage(); err != nil {
log.Error("failed to setup the storage")
return err
}
if err := createInitialOfflineState(); err != nil {
log.Error("failed to create the initial offline state")
return err
}
chat.Setup(ChatListenerImpl{})
return nil
}
func createInitialOfflineState() error {
// Provide default files
if !utils.DoesFileExists("webroot/thumbnail.jpg") {
if err := utils.Copy("static/logo.png", "webroot/thumbnail.jpg"); err != nil {
return err
}
}
ffmpeg.ShowStreamOfflineState()
return nil
}
func startCleanupTimer() {
_cleanupTicker := time.NewTicker(5 * time.Minute)
go func() {
for {
select {
case <-_cleanupTicker.C:
resetDirectories()
}
}
}()
}
// StopCleanupTimer will stop the previous cleanup timer
func stopCleanupTimer() {
if _cleanupTicker != nil {
_cleanupTicker.Stop()
}
}
func resetDirectories() {
log.Trace("Resetting file directories to a clean slate.")
// Wipe the public, web-accessible hls data directory
os.RemoveAll(config.Config.GetPublicHLSSavePath())
os.RemoveAll(config.Config.GetPrivateHLSSavePath())
os.MkdirAll(config.Config.GetPublicHLSSavePath(), 0777)
os.MkdirAll(config.Config.GetPrivateHLSSavePath(), 0777)
// Remove the previous thumbnail
os.Remove("webroot/thumbnail.jpg")
// Create private hls data dirs
if len(config.Config.VideoSettings.StreamQualities) != 0 {
for index := range config.Config.VideoSettings.StreamQualities {
os.MkdirAll(path.Join(config.Config.GetPrivateHLSSavePath(), strconv.Itoa(index)), 0777)
os.MkdirAll(path.Join(config.Config.GetPublicHLSSavePath(), strconv.Itoa(index)), 0777)
}
} else {
os.MkdirAll(path.Join(config.Config.GetPrivateHLSSavePath(), strconv.Itoa(0)), 0777)
os.MkdirAll(path.Join(config.Config.GetPublicHLSSavePath(), strconv.Itoa(0)), 0777)
}
}