2020-06-03 10:34:05 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
"github.com/aws/aws-sdk-go/service/s3/s3manager"
|
|
|
|
)
|
|
|
|
|
|
|
|
type S3Storage struct {
|
|
|
|
sess *session.Session
|
|
|
|
host string
|
|
|
|
|
|
|
|
s3Region string
|
|
|
|
s3Bucket string
|
|
|
|
s3AccessKey string
|
|
|
|
s3Secret string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *S3Storage) Setup(configuration Config) {
|
|
|
|
log.Println("Setting up S3 for external storage of video...")
|
|
|
|
|
|
|
|
s.s3Region = configuration.S3.Region
|
|
|
|
s.s3Bucket = configuration.S3.Bucket
|
|
|
|
s.s3AccessKey = configuration.S3.AccessKey
|
|
|
|
s.s3Secret = configuration.S3.Secret
|
|
|
|
|
|
|
|
s.sess = s.connectAWS()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *S3Storage) Save(filePath string) string {
|
|
|
|
// fmt.Println("Saving", filePath)
|
|
|
|
|
2020-06-09 18:31:27 +02:00
|
|
|
file, err := os.Open(filePath)
|
|
|
|
defer file.Close()
|
|
|
|
|
2020-06-03 10:34:05 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
uploader := s3manager.NewUploader(s.sess)
|
|
|
|
|
|
|
|
response, err := uploader.Upload(&s3manager.UploadInput{
|
|
|
|
Bucket: aws.String(s.s3Bucket), // Bucket to be used
|
|
|
|
Key: aws.String(filePath), // Name of the file to be saved
|
|
|
|
Body: file, // File
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// fmt.Println("Uploaded", filePath, "to", response.Location)
|
|
|
|
|
2020-06-09 10:52:15 +02:00
|
|
|
return response.Location
|
2020-06-03 10:34:05 +02:00
|
|
|
}
|
|
|
|
|
2020-06-09 10:52:15 +02:00
|
|
|
func (s *S3Storage) GenerateRemotePlaylist(playlist string, variant Variant) string {
|
2020-06-03 10:34:05 +02:00
|
|
|
var newPlaylist = ""
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(strings.NewReader(playlist))
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if line[0:1] != "#" {
|
2020-06-09 10:52:15 +02:00
|
|
|
fullRemotePath := variant.getSegmentForFilename(line)
|
|
|
|
if fullRemotePath != nil {
|
|
|
|
line = fullRemotePath.RemoteID
|
|
|
|
} else {
|
|
|
|
line = ""
|
|
|
|
}
|
2020-06-03 10:34:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
newPlaylist = newPlaylist + line + "\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
return newPlaylist
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s S3Storage) connectAWS() *session.Session {
|
|
|
|
creds := credentials.NewStaticCredentials(s.s3AccessKey, s.s3Secret, "")
|
|
|
|
_, err := creds.Get()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sess, err := session.NewSession(
|
|
|
|
&aws.Config{
|
|
|
|
Region: aws.String(s.s3Region),
|
|
|
|
Credentials: creds,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return sess
|
|
|
|
}
|