2021-11-20 18:29:34 +00:00
|
|
|
package media
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"git.netflux.io/rob/clipper/generated/store"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GetVideoProgress struct {
|
|
|
|
PercentComplete float32
|
|
|
|
URL string
|
|
|
|
}
|
|
|
|
|
2021-11-29 11:46:33 +00:00
|
|
|
type GetVideoProgressReader interface {
|
|
|
|
// Next returns the next video progress status. When the stream has finished,
|
|
|
|
// a valid GetVideoProgress value will be returned with io.EOF.
|
|
|
|
Next() (GetVideoProgress, error)
|
|
|
|
}
|
|
|
|
|
2021-11-20 18:29:34 +00:00
|
|
|
type videoGetter struct {
|
2021-12-07 19:58:11 +00:00
|
|
|
store Store
|
|
|
|
fileStore FileStore
|
|
|
|
logger *zap.SugaredLogger
|
2021-11-20 18:29:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type videoGetterState struct {
|
|
|
|
*videoGetter
|
|
|
|
|
2022-01-03 12:32:39 +00:00
|
|
|
r io.Reader
|
2021-12-13 03:28:14 +00:00
|
|
|
count, exp int64
|
|
|
|
mediaSetID uuid.UUID
|
|
|
|
key, contentType string
|
|
|
|
url string
|
|
|
|
progressChan chan GetVideoProgress
|
|
|
|
errorChan chan error
|
2021-11-20 18:29:34 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 19:58:11 +00:00
|
|
|
func newVideoGetter(store Store, fileStore FileStore, logger *zap.SugaredLogger) *videoGetter {
|
|
|
|
return &videoGetter{store: store, fileStore: fileStore, logger: logger}
|
2021-11-20 18:29:34 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 19:58:11 +00:00
|
|
|
// GetVideo gets video from Youtube and uploads it to a filestore using the
|
|
|
|
// specified key and content type. The returned reader must have its Next()
|
2021-11-20 18:29:34 +00:00
|
|
|
// method called until error = io.EOF, otherwise a deadlock or other resource
|
|
|
|
// leakage is likely.
|
2022-01-03 12:32:39 +00:00
|
|
|
func (g *videoGetter) GetVideo(ctx context.Context, r io.Reader, exp int64, mediaSetID uuid.UUID, key, contentType string) (GetVideoProgressReader, error) {
|
2021-11-20 18:29:34 +00:00
|
|
|
s := &videoGetterState{
|
|
|
|
videoGetter: g,
|
2022-01-03 12:32:39 +00:00
|
|
|
r: newLogProgressReader(r, "video", exp, g.logger),
|
2021-11-20 18:29:34 +00:00
|
|
|
exp: exp,
|
|
|
|
mediaSetID: mediaSetID,
|
|
|
|
key: key,
|
|
|
|
contentType: contentType,
|
|
|
|
progressChan: make(chan GetVideoProgress),
|
|
|
|
errorChan: make(chan error, 1),
|
|
|
|
}
|
|
|
|
|
|
|
|
go s.getVideo(ctx)
|
|
|
|
|
|
|
|
// return s, exposing only the limited interface to the caller.
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2021-11-29 11:46:33 +00:00
|
|
|
// Write implements io.Writer. It is copied that same data that is written to
|
2021-12-07 19:58:11 +00:00
|
|
|
// the file store, to implement progress tracking.
|
2021-11-20 18:29:34 +00:00
|
|
|
func (s *videoGetterState) Write(p []byte) (int, error) {
|
|
|
|
s.count += int64(len(p))
|
|
|
|
pc := (float32(s.count) / float32(s.exp)) * 100
|
|
|
|
s.progressChan <- GetVideoProgress{PercentComplete: pc}
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *videoGetterState) getVideo(ctx context.Context) {
|
2022-01-03 12:32:39 +00:00
|
|
|
teeReader := io.TeeReader(s.r, s)
|
|
|
|
|
2021-12-07 19:58:11 +00:00
|
|
|
_, err := s.fileStore.PutObject(ctx, s.key, teeReader, s.contentType)
|
2021-11-20 18:29:34 +00:00
|
|
|
if err != nil {
|
2021-12-07 19:58:11 +00:00
|
|
|
s.errorChan <- fmt.Errorf("error uploading to file store: %v", err)
|
2021-11-20 18:29:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-07 19:58:11 +00:00
|
|
|
s.url, err = s.fileStore.GetURL(ctx, s.key)
|
2021-11-20 18:29:34 +00:00
|
|
|
if err != nil {
|
2021-12-07 19:58:11 +00:00
|
|
|
s.errorChan <- fmt.Errorf("error getting object URL: %v", err)
|
2021-11-20 18:29:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
storeParams := store.SetVideoUploadedParams{
|
2021-12-13 03:28:14 +00:00
|
|
|
ID: s.mediaSetID,
|
|
|
|
VideoS3Key: sqlString(s.key),
|
2021-11-20 18:29:34 +00:00
|
|
|
}
|
|
|
|
_, err = s.store.SetVideoUploaded(ctx, storeParams)
|
|
|
|
if err != nil {
|
|
|
|
s.errorChan <- fmt.Errorf("error saving to store: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
close(s.progressChan)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next implements GetVideoProgressReader.
|
|
|
|
func (s *videoGetterState) Next() (GetVideoProgress, error) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case progress, ok := <-s.progressChan:
|
|
|
|
if !ok {
|
|
|
|
return GetVideoProgress{PercentComplete: 100, URL: s.url}, io.EOF
|
|
|
|
}
|
|
|
|
return progress, nil
|
|
|
|
case err := <-s.errorChan:
|
|
|
|
return GetVideoProgress{}, fmt.Errorf("error waiting for progress: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type videoGetterDownloaded string
|
|
|
|
|
|
|
|
// Next() implements GetVideoProgressReader.
|
|
|
|
func (s *videoGetterDownloaded) Next() (GetVideoProgress, error) {
|
|
|
|
return GetVideoProgress{
|
|
|
|
PercentComplete: 100,
|
|
|
|
URL: string(*s),
|
|
|
|
}, io.EOF
|
|
|
|
}
|