2019-04-19 03:22:51 +02:00
|
|
|
package thumbs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2020-08-06 23:53:03 +02:00
|
|
|
"strings"
|
2019-04-19 03:22:51 +02:00
|
|
|
|
2020-07-27 23:14:06 +02:00
|
|
|
"github.com/lbryio/ytsync/v5/downloader/ytdl"
|
|
|
|
|
2019-10-10 16:50:33 +02:00
|
|
|
"github.com/lbryio/lbry.go/v2/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"
|
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()
|
2020-08-06 23:53:03 +02:00
|
|
|
if strings.HasPrefix(u.originalUrl, "//") {
|
|
|
|
u.originalUrl = "https:" + u.originalUrl
|
|
|
|
}
|
2019-04-19 03:22:51 +02:00
|
|
|
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-11-11 21:11:34 +01:00
|
|
|
Bucket: aws.String("thumbnails.lbry.com"),
|
|
|
|
Key: key,
|
|
|
|
Body: thumb,
|
|
|
|
ACL: aws.String("public-read"),
|
|
|
|
ContentType: aws.String("image/jpeg"),
|
|
|
|
CacheControl: aws.String("public, max-age=2592000"),
|
2019-04-19 03:22:51 +02:00
|
|
|
})
|
2020-10-07 03:10:54 +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
|
|
|
|
}
|
|
|
|
|
2020-10-07 03:10:54 +02:00
|
|
|
ownS3Config := s3Config.Copy(&aws.Config{Endpoint: aws.String("s3.lbry.tech")})
|
|
|
|
|
|
|
|
tu2 := thumbnailUploader{
|
|
|
|
originalUrl: url,
|
|
|
|
name: name,
|
|
|
|
s3Config: *ownS3Config,
|
|
|
|
}
|
|
|
|
//own S3
|
|
|
|
err = tu2.uploadThumbnail()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2019-04-19 03:22:51 +02:00
|
|
|
return tu.mirroredUrl, nil
|
|
|
|
}
|
2019-05-03 05:11:52 +02:00
|
|
|
|
2020-07-27 23:14:06 +02:00
|
|
|
func GetBestThumbnail(thumbnails []ytdl.Thumbnail) *ytdl.Thumbnail {
|
2021-06-25 19:04:40 +02:00
|
|
|
var bestWidth ytdl.Thumbnail
|
2020-07-27 23:14:06 +02:00
|
|
|
for _, thumbnail := range thumbnails {
|
2021-06-25 19:04:40 +02:00
|
|
|
if bestWidth.Width < thumbnail.Width {
|
|
|
|
bestWidth = thumbnail
|
2020-07-27 23:14:06 +02:00
|
|
|
}
|
2019-05-03 05:11:52 +02:00
|
|
|
}
|
2021-06-25 19:04:40 +02:00
|
|
|
return &bestWidth
|
2019-05-03 05:11:52 +02:00
|
|
|
}
|