mirror of
https://github.com/iyear/tdl
synced 2025-01-09 04:17:35 +08:00
feat(up): video thumb support. WIP
This commit is contained in:
parent
6dd9bbfb07
commit
ba2785c128
@ -2,18 +2,25 @@ package up
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/gabriel-vasile/mimetype"
|
||||
"github.com/iyear/tdl/pkg/uploader"
|
||||
"github.com/iyear/tdl/pkg/utils"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type file struct {
|
||||
file string
|
||||
thumb string
|
||||
}
|
||||
|
||||
type iter struct {
|
||||
files []string
|
||||
files []*file
|
||||
cur int
|
||||
}
|
||||
|
||||
func newIter(files []string) *iter {
|
||||
func newIter(files []*file) *iter {
|
||||
return &iter{
|
||||
files: files,
|
||||
cur: -1,
|
||||
@ -45,12 +52,12 @@ func (i *iter) Value(ctx context.Context) (*uploader.Item, error) {
|
||||
|
||||
cur := i.files[i.cur]
|
||||
|
||||
mime, err := mimetype.DetectFile(cur)
|
||||
fMime, err := mimetype.DetectFile(cur.file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
f, err := os.Open(cur)
|
||||
f, err := os.Open(cur.file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -60,11 +67,25 @@ func (i *iter) Value(ctx context.Context) (*uploader.Item, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var thumb *os.File
|
||||
// has thumbnail
|
||||
if cur.thumb != "" {
|
||||
tMime, err := mimetype.DetectFile(cur.thumb)
|
||||
if err != nil || !utils.Media.IsImage(tMime.String()) { // TODO(iyear): jpg only
|
||||
return nil, fmt.Errorf("invalid thumbnail file: %s", cur.thumb)
|
||||
}
|
||||
thumb, err = os.Open(cur.thumb)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &uploader.Item{
|
||||
R: f,
|
||||
Name: filepath.Base(f.Name()),
|
||||
MIME: mime.String(),
|
||||
Size: stat.Size(),
|
||||
File: f,
|
||||
Thumb: thumb,
|
||||
Name: filepath.Base(f.Name()),
|
||||
MIME: fMime.String(),
|
||||
Size: stat.Size(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
@ -1,13 +1,18 @@
|
||||
package up
|
||||
|
||||
import (
|
||||
"github.com/iyear/tdl/pkg/consts"
|
||||
"github.com/iyear/tdl/pkg/utils"
|
||||
"io/fs"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func walk(paths, excludes []string) ([]string, error) {
|
||||
files := make([]string, 0)
|
||||
excludesMap := make(map[string]struct{})
|
||||
func walk(paths, excludes []string) ([]*file, error) {
|
||||
files := make([]*file, 0)
|
||||
excludesMap := map[string]struct{}{
|
||||
consts.UploadThumbExt: {}, // ignore thumbnail files
|
||||
}
|
||||
|
||||
for _, exclude := range excludes {
|
||||
excludesMap[exclude] = struct{}{}
|
||||
@ -25,7 +30,13 @@ func walk(paths, excludes []string) ([]string, error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
files = append(files, path)
|
||||
f := file{file: path}
|
||||
t := strings.TrimRight(path, filepath.Ext(path)) + consts.UploadThumbExt
|
||||
if utils.FS.PathExists(t) {
|
||||
f.thumb = t
|
||||
}
|
||||
|
||||
files = append(files, &f)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
|
@ -1,7 +1,8 @@
|
||||
package consts
|
||||
|
||||
var (
|
||||
DataDir string
|
||||
KVPath string
|
||||
DocsPath = "docs"
|
||||
DataDir string
|
||||
KVPath string
|
||||
DocsPath = "docs"
|
||||
UploadThumbExt = ".thumb"
|
||||
)
|
||||
|
@ -18,8 +18,9 @@ type ReadSeekCloser interface {
|
||||
}
|
||||
|
||||
type Item struct {
|
||||
R ReadSeekCloser
|
||||
Name string
|
||||
MIME string
|
||||
Size int64
|
||||
File ReadSeekCloser
|
||||
Thumb ReadSeekCloser
|
||||
Name string
|
||||
MIME string
|
||||
Size int64
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package uploader
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/fatih/color"
|
||||
"github.com/gotd/td/telegram/message"
|
||||
"github.com/gotd/td/telegram/message/styling"
|
||||
@ -85,9 +86,10 @@ func (u *Uploader) Upload(ctx context.Context, limit int) error {
|
||||
}
|
||||
|
||||
func (u *Uploader) upload(ctx context.Context, item *Item) error {
|
||||
defer func(R io.ReadCloser) {
|
||||
_ = R.Close()
|
||||
}(item.R)
|
||||
defer func(r io.ReadCloser, t io.ReadCloser) {
|
||||
_ = r.Close()
|
||||
_ = t.Close()
|
||||
}(item.File, item.Thumb)
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
@ -100,7 +102,7 @@ func (u *Uploader) upload(ctx context.Context, item *Item) error {
|
||||
up := uploader.NewUploader(u.client).
|
||||
WithPartSize(u.partSize).WithThreads(u.threads).WithProgress(&_progress{tracker: tracker})
|
||||
|
||||
f, err := up.Upload(ctx, uploader.NewUpload(item.Name, item.R, item.Size))
|
||||
f, err := up.Upload(ctx, uploader.NewUpload(item.Name, item.File, item.Size))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -111,17 +113,23 @@ func (u *Uploader) upload(ctx context.Context, item *Item) error {
|
||||
styling.Code(item.MIME),
|
||||
).MIME(item.MIME).Filename(item.Name)
|
||||
|
||||
var media message.MediaOption = doc
|
||||
// upload thumbnail TODO(iyear): unavailable, unknown server policy. https://github.com/LonamiWebs/Telethon/blob/v1/telethon/client/uploads.py#L208-L218
|
||||
if thumb, err := uploader.NewUploader(u.client).
|
||||
FromReader(ctx, fmt.Sprintf("%s.thumb", item.Name), item.Thumb); err != nil {
|
||||
doc = doc.Thumb(thumb)
|
||||
}
|
||||
|
||||
var media message.MediaOption = doc
|
||||
if utils.Media.IsVideo(item.MIME) {
|
||||
// reset reader
|
||||
if _, err = item.R.Seek(0, io.SeekStart); err != nil {
|
||||
if _, err = item.File.Seek(0, io.SeekStart); err != nil {
|
||||
return err
|
||||
}
|
||||
dur, w, h, err := utils.Media.GetMP4Info(item.R)
|
||||
dur, w, h, err := utils.Media.GetMP4Info(item.File)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
media = doc.Video().DurationSeconds(dur).Resolution(w, h).SupportsStreaming()
|
||||
}
|
||||
if utils.Media.IsAudio(item.MIME) {
|
||||
|
Loading…
Reference in New Issue
Block a user