2023-05-31 00:05:24 +03:00
|
|
|
package storageproviders
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/grafov/m3u8"
|
2023-06-16 03:11:44 +03:00
|
|
|
"github.com/owncast/owncast/services/config"
|
2023-06-15 03:39:49 +03:00
|
|
|
playlist "github.com/owncast/owncast/video/playlists"
|
2023-05-31 00:05:24 +03:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2023-09-22 03:58:02 +03:00
|
|
|
// rewritePlaylistLocations will take a local playlist and rewrite it to have absolute URLs to a specified location.
|
|
|
|
func rewritePlaylistLocations(localFilePath, remoteServingEndpoint, pathPrefix string) error {
|
2023-05-31 00:05:24 +03:00
|
|
|
f, err := os.Open(localFilePath) // nolint
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p := m3u8.NewMasterPlaylist()
|
|
|
|
if err := p.DecodeFrom(bufio.NewReader(f), false); err != nil {
|
|
|
|
log.Warnln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range p.Variants {
|
2023-08-02 23:35:47 +03:00
|
|
|
// Determine the final path to this playlist.
|
|
|
|
var finalPath string
|
|
|
|
if pathPrefix != "" {
|
|
|
|
finalPath = filepath.Join(pathPrefix, "/hls")
|
|
|
|
} else {
|
|
|
|
finalPath = "/hls"
|
|
|
|
}
|
|
|
|
item.URI = remoteServingEndpoint + filepath.Join(finalPath, item.URI)
|
2023-05-31 00:05:24 +03:00
|
|
|
}
|
2023-06-16 03:11:44 +03:00
|
|
|
c := config.GetConfig()
|
|
|
|
publicPath := filepath.Join(c.HLSStoragePath, filepath.Base(localFilePath))
|
2023-05-31 00:05:24 +03:00
|
|
|
|
|
|
|
newPlaylist := p.String()
|
|
|
|
|
|
|
|
return playlist.WritePlaylist(newPlaylist, publicPath)
|
|
|
|
}
|