Files
owncast/core/storageproviders/local.go
T
LogalDeveloper 6b93ac0d78
Automated API tests / test (push) Failing after 5s
Browser Tests / cypress-run (push) Failing after 5s
Lint / Go linter (push) Failing after 4s
HLS tests / tests (push) Failing after 5s
Go Tests / test (push) Has been cancelled
Go Tests / test-bsds (map[name:freebsd version:12.2]) (push) Has been cancelled
Go Tests / test-bsds (map[name:openbsd version:6.8]) (push) Has been cancelled
Fixed segment cleanup deleting init.mp4 and reduced its cache duration to 20 seconds.
2026-04-03 09:52:15 -04:00

104 lines
2.7 KiB
Go

package storageproviders
import (
"os"
"path/filepath"
"sort"
"github.com/owncast/owncast/persistence/configrepository"
log "github.com/sirupsen/logrus"
)
// LocalStorage represents an instance of the local storage provider for HLS video.
type LocalStorage struct {
host string
}
// NewLocalStorage returns a new LocalStorage instance.
func NewLocalStorage() *LocalStorage {
return &LocalStorage{}
}
// Setup configures this storage provider.
func (s *LocalStorage) Setup() error {
configRepository := configrepository.Get()
s.host = configRepository.GetVideoServingEndpoint()
return nil
}
// SegmentWritten is called when a single segment of video is written.
func (s *LocalStorage) SegmentWritten(localFilePath string) {
if _, err := s.Save(localFilePath, 0); err != nil {
log.Warnln(err)
}
}
// VariantPlaylistWritten is called when a variant hls playlist is written.
func (s *LocalStorage) VariantPlaylistWritten(localFilePath string) {
if _, err := s.Save(localFilePath, 0); err != nil {
log.Errorln(err)
return
}
}
// MasterPlaylistWritten is called when the master hls playlist is written.
func (s *LocalStorage) MasterPlaylistWritten(localFilePath string) {
// If we're using a remote serving endpoint, we need to rewrite the master playlist
if s.host != "" {
if err := rewritePlaylistLocations(localFilePath, s.host, ""); err != nil {
log.Warnln(err)
}
} else {
if _, err := s.Save(localFilePath, 0); err != nil {
log.Warnln(err)
}
}
}
// Save will save a local filepath using the storage provider.
func (s *LocalStorage) Save(filePath string, retryCount int) (string, error) {
return filePath, nil
}
// Cleanup will remove old files from the storage provider.
func (s *LocalStorage) Cleanup() error {
// Determine how many files we should keep on disk
configRepository := configrepository.Get()
maxNumber := configRepository.GetStreamLatencyLevel().SegmentCount
buffer := 10
return localCleanup(maxNumber + buffer)
}
func getAllFilesRecursive(baseDirectory string) (map[string][]os.FileInfo, error) {
files := make(map[string][]os.FileInfo)
var directory string
err := filepath.Walk(baseDirectory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
directory = info.Name()
}
if filepath.Ext(info.Name()) == ".m4s" {
files[directory] = append(files[directory], info)
}
return nil
})
if err != nil {
return nil, err
}
// Sort by date so we can delete old files
for directory := range files {
sort.Slice(files[directory], func(i, j int) bool {
return files[directory][i].ModTime().UnixNano() > files[directory][j].ModTime().UnixNano()
})
}
return files, nil
}