2020-10-15 00:07:38 +03:00
|
|
|
package storageproviders
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
2020-10-16 03:50:17 +03:00
|
|
|
"time"
|
2020-10-15 00:07:38 +03:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/owncast/owncast/config"
|
2021-02-19 10:05:52 +03:00
|
|
|
"github.com/owncast/owncast/core/transcoder"
|
2020-10-15 00:07:38 +03:00
|
|
|
"github.com/owncast/owncast/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LocalStorage struct {
|
|
|
|
}
|
|
|
|
|
2020-10-16 03:50:17 +03:00
|
|
|
// Cleanup old public HLS content every N min from the webroot.
|
|
|
|
var _onlineCleanupTicker *time.Ticker
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// Setup configures this storage provider.
|
2020-10-15 00:07:38 +03:00
|
|
|
func (s *LocalStorage) Setup() error {
|
2020-10-16 03:50:17 +03:00
|
|
|
// NOTE: This cleanup timer will have to be disabled to support recordings in the future
|
|
|
|
// as all HLS segments have to be publicly available on disk to keep a recording of them.
|
|
|
|
_onlineCleanupTicker = time.NewTicker(1 * time.Minute)
|
|
|
|
go func() {
|
2020-11-15 05:39:53 +03:00
|
|
|
for range _onlineCleanupTicker.C {
|
2021-02-19 10:05:52 +03:00
|
|
|
transcoder.CleanupOldContent(config.PublicHLSStoragePath)
|
2020-10-16 03:50:17 +03:00
|
|
|
}
|
|
|
|
}()
|
2020-10-15 00:07:38 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// SegmentWritten is called when a single segment of video is written.
|
2020-10-15 00:07:38 +03:00
|
|
|
func (s *LocalStorage) SegmentWritten(localFilePath string) {
|
2020-11-15 05:39:53 +03:00
|
|
|
_, err := s.Save(localFilePath, 0)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnln(err)
|
|
|
|
}
|
2020-10-15 00:07:38 +03:00
|
|
|
}
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// VariantPlaylistWritten is called when a variant hls playlist is written.
|
2020-10-15 00:07:38 +03:00
|
|
|
func (s *LocalStorage) VariantPlaylistWritten(localFilePath string) {
|
2020-10-17 01:04:31 +03:00
|
|
|
_, err := s.Save(localFilePath, 0)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
2020-10-15 00:07:38 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// MasterPlaylistWritten is called when the master hls playlist is written.
|
2020-10-15 00:07:38 +03:00
|
|
|
func (s *LocalStorage) MasterPlaylistWritten(localFilePath string) {
|
2020-11-15 05:39:53 +03:00
|
|
|
_, err := s.Save(localFilePath, 0)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnln(err)
|
|
|
|
}
|
2020-10-15 00:07:38 +03:00
|
|
|
}
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// Save will save a local filepath using the storage provider.
|
2020-10-15 00:07:38 +03:00
|
|
|
func (s *LocalStorage) Save(filePath string, retryCount int) (string, error) {
|
|
|
|
newPath := ""
|
|
|
|
|
|
|
|
// This is a hack
|
|
|
|
if filePath == "hls/stream.m3u8" {
|
|
|
|
newPath = filepath.Join(config.PublicHLSStoragePath, filepath.Base(filePath))
|
|
|
|
} else {
|
|
|
|
newPath = filepath.Join(config.WebRoot, filePath)
|
|
|
|
}
|
|
|
|
|
2020-11-15 05:39:53 +03:00
|
|
|
err := utils.Copy(filePath, newPath)
|
|
|
|
return newPath, err
|
2020-10-15 00:07:38 +03:00
|
|
|
}
|