2019-04-19 03:22:51 +02:00
|
|
|
package thumbs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
2019-08-28 15:14:32 +02:00
|
|
|
"github.com/lbryio/lbry.go/extras/errors"
|
2019-04-19 03:22:51 +02:00
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
"github.com/aws/aws-sdk-go/service/s3/s3manager"
|
2019-08-04 00:34:48 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"google.golang.org/api/youtube/v3"
|
2019-04-19 03:22:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type thumbnailUploader struct {
|
|
|
|
name string
|
|
|
|
originalUrl string
|
|
|
|
mirroredUrl string
|
|
|
|
s3Config aws.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
const thumbnailPath = "/tmp/ytsync_thumbnails/"
|
2019-05-03 05:11:52 +02:00
|
|
|
const ThumbnailEndpoint = "https://thumbnails.lbry.com/"
|
2019-04-19 03:22:51 +02:00
|
|
|
|
|
|
|
func (u *thumbnailUploader) downloadThumbnail() error {
|
2019-08-11 04:50:43 +02:00
|
|
|
_ = os.Mkdir(thumbnailPath, 0777)
|
2019-04-19 03:22:51 +02:00
|
|
|
img, err := os.Create("/tmp/ytsync_thumbnails/" + u.name)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
defer img.Close()
|
|
|
|
|
|
|
|
resp, err := http.Get(u.originalUrl)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(img, resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *thumbnailUploader) uploadThumbnail() error {
|
2019-05-28 22:40:31 +02:00
|
|
|
key := &u.name
|
2019-04-19 03:22:51 +02:00
|
|
|
thumb, err := os.Open("/tmp/ytsync_thumbnails/" + u.name)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
defer thumb.Close()
|
|
|
|
|
|
|
|
s3Session, err := session.NewSession(&u.s3Config)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
uploader := s3manager.NewUploader(s3Session)
|
|
|
|
|
|
|
|
_, err = uploader.Upload(&s3manager.UploadInput{
|
2019-06-20 21:45:17 +02:00
|
|
|
Bucket: aws.String("thumbnails.lbry.com"),
|
|
|
|
Key: key,
|
|
|
|
Body: thumb,
|
|
|
|
ACL: aws.String("public-read"),
|
|
|
|
ContentType: aws.String("image/jpeg"),
|
2019-04-19 03:22:51 +02:00
|
|
|
})
|
2019-05-03 05:11:52 +02:00
|
|
|
u.mirroredUrl = ThumbnailEndpoint + u.name
|
2019-04-19 03:22:51 +02:00
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *thumbnailUploader) deleteTmpFile() {
|
|
|
|
err := os.Remove("/tmp/ytsync_thumbnails/" + u.name)
|
|
|
|
if err != nil {
|
|
|
|
log.Infof("failed to delete local thumbnail file: %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func MirrorThumbnail(url string, name string, s3Config aws.Config) (string, error) {
|
|
|
|
tu := thumbnailUploader{
|
|
|
|
originalUrl: url,
|
|
|
|
name: name,
|
|
|
|
s3Config: s3Config,
|
|
|
|
}
|
|
|
|
err := tu.downloadThumbnail()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer tu.deleteTmpFile()
|
|
|
|
|
|
|
|
err = tu.uploadThumbnail()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tu.mirroredUrl, nil
|
|
|
|
}
|
2019-05-03 05:11:52 +02:00
|
|
|
|
|
|
|
func GetBestThumbnail(thumbnails *youtube.ThumbnailDetails) *youtube.Thumbnail {
|
|
|
|
if thumbnails.Maxres != nil {
|
|
|
|
return thumbnails.Maxres
|
|
|
|
} else if thumbnails.High != nil {
|
|
|
|
return thumbnails.High
|
|
|
|
} else if thumbnails.Medium != nil {
|
|
|
|
return thumbnails.Medium
|
|
|
|
} else if thumbnails.Standard != nil {
|
|
|
|
return thumbnails.Standard
|
|
|
|
}
|
|
|
|
return thumbnails.Default
|
|
|
|
}
|