owncast/playlistMonitor.go

73 lines
1.6 KiB
Go
Raw Normal View History

2020-05-30 03:08:33 +02:00
package main
import (
"io/ioutil"
2020-06-02 01:53:31 +02:00
"path"
2020-05-30 03:08:33 +02:00
"path/filepath"
"time"
2020-06-02 01:53:31 +02:00
log "github.com/sirupsen/logrus"
icore "github.com/ipfs/interface-go-ipfs-core"
2020-05-30 03:08:33 +02:00
"github.com/radovskyb/watcher"
)
var filesToUpload = make(map[string]string)
2020-06-02 01:53:31 +02:00
func monitorVideoContent(pathToMonitor string, configuration Config, ipfs *icore.CoreAPI) {
log.Printf("Using %s for IPFS files...\n", pathToMonitor)
2020-05-30 03:08:33 +02:00
w := watcher.New()
go func() {
for {
select {
case event := <-w.Event:
if event.Op != watcher.Write {
continue
}
if filepath.Base(event.Path) == "temp.m3u8" {
2020-06-02 01:53:31 +02:00
if configuration.IPFS.Enabled {
for filePath, objectID := range filesToUpload {
if objectID != "" {
continue
}
2020-05-30 03:08:33 +02:00
2020-06-02 01:53:31 +02:00
newObjectPath := save(path.Join(configuration.PrivateHLSPath, filePath), ipfs)
filesToUpload[filePath] = newObjectPath
}
2020-05-30 03:08:33 +02:00
}
2020-06-02 01:53:31 +02:00
2020-05-30 03:08:33 +02:00
playlistBytes, err := ioutil.ReadFile(event.Path)
verifyError(err)
playlistString := string(playlistBytes)
2020-06-02 01:53:31 +02:00
if configuration.IPFS.Enabled {
playlistString = generateRemotePlaylist(playlistString, configuration.IPFS.Gateway, filesToUpload)
}
2020-06-02 01:53:31 +02:00
writePlaylist(playlistString, path.Join(configuration.PublicHLSPath, "/stream.m3u8"))
2020-05-30 03:08:33 +02:00
} else if filepath.Ext(event.Path) == ".ts" {
2020-06-02 01:53:31 +02:00
if configuration.IPFS.Enabled {
filesToUpload[filepath.Base(event.Path)] = ""
}
2020-05-30 03:08:33 +02:00
}
case err := <-w.Error:
log.Fatalln(err)
case <-w.Closed:
return
}
}
}()
// Watch this folder for changes.
2020-06-02 01:53:31 +02:00
if err := w.Add(pathToMonitor); err != nil {
2020-05-30 03:08:33 +02:00
log.Fatalln(err)
}
if err := w.Start(time.Millisecond * 100); err != nil {
2020-05-30 03:08:33 +02:00
log.Fatalln(err)
}
}