2020-06-03 11:34:05 +03: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
|
|
|
|
|
2020-06-10 00:01:42 +03:00
|
|
|
s3Endpoint string
|
2020-06-03 11:34:05 +03:00
|
|
|
s3Region string
|
|
|
|
s3Bucket string
|
|
|
|
s3AccessKey string
|
|
|
|
s3Secret string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *S3Storage) Setup(configuration Config) {
|
|
|
|
log.Println("Setting up S3 for external storage of video...")
|
|
|
|
|
2020-06-10 00:01:42 +03:00
|
|
|
s.s3Endpoint = configuration.S3.Endpoint
|
2020-06-03 11:34:05 +03:00
|
|
|
s.s3Region = configuration.S3.Region
|
|
|
|
s.s3Bucket = configuration.S3.Bucket
|
|
|
|
s.s3AccessKey = configuration.S3.AccessKey
|
|
|
|
s.s3Secret = configuration.S3.Secret
|
|
|
|
|
|
|
|
s.sess = s.connectAWS()
|
|
|
|
}
|
|
|
|
|
2020-06-18 08:01:53 +03:00
|
|
|
func (s *S3Storage) Save(filePath string, retryCount int) string {
|
2020-06-03 11:34:05 +03:00
|
|
|
// fmt.Println("Saving", filePath)
|
|
|
|
|
2020-06-09 19:31:27 +03:00
|
|
|
file, err := os.Open(filePath)
|
|
|
|
defer file.Close()
|
|
|
|
|
2020-06-03 11:34:05 +03:00
|
|
|
if err != nil {
|
2020-06-18 08:01:53 +03:00
|
|
|
log.Errorln(err)
|
2020-06-03 11:34:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-06-18 08:01:53 +03:00
|
|
|
log.Errorln(err)
|
|
|
|
if retryCount < 4 {
|
|
|
|
log.Println("Retrying...")
|
|
|
|
s.Save(filePath, retryCount+1)
|
|
|
|
}
|
2020-06-03 11:34:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// fmt.Println("Uploaded", filePath, "to", response.Location)
|
|
|
|
|
2020-06-09 11:52:15 +03:00
|
|
|
return response.Location
|
2020-06-03 11:34:05 +03:00
|
|
|
}
|
|
|
|
|
2020-06-09 11:52:15 +03:00
|
|
|
func (s *S3Storage) GenerateRemotePlaylist(playlist string, variant Variant) string {
|
2020-06-03 11:34:05 +03:00
|
|
|
var newPlaylist = ""
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(strings.NewReader(playlist))
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if line[0:1] != "#" {
|
2020-06-09 11:52:15 +03:00
|
|
|
fullRemotePath := variant.getSegmentForFilename(line)
|
|
|
|
if fullRemotePath != nil {
|
|
|
|
line = fullRemotePath.RemoteID
|
|
|
|
} else {
|
|
|
|
line = ""
|
|
|
|
}
|
2020-06-03 11:34:05 +03: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 {
|
2020-06-18 09:01:49 +03:00
|
|
|
log.Panicln(err)
|
2020-06-03 11:34:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
sess, err := session.NewSession(
|
|
|
|
&aws.Config{
|
2020-06-10 00:01:42 +03:00
|
|
|
Region: aws.String(s.s3Region),
|
|
|
|
Credentials: creds,
|
|
|
|
Endpoint: aws.String(s.s3Endpoint),
|
|
|
|
S3ForcePathStyle: aws.Bool(true),
|
2020-06-03 11:34:05 +03:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
2020-06-18 09:01:49 +03:00
|
|
|
log.Panicln(err)
|
2020-06-03 11:34:05 +03:00
|
|
|
}
|
|
|
|
return sess
|
|
|
|
}
|