refactor(tmedia): extract item to self struct

This commit is contained in:
iyear 2023-11-07 23:21:22 +08:00
parent 892dcdd2dd
commit 23df1f10fe
6 changed files with 23 additions and 25 deletions

View File

@ -22,7 +22,6 @@ import (
"github.com/iyear/tdl/pkg/consts"
"github.com/iyear/tdl/pkg/dcpool"
"github.com/iyear/tdl/pkg/downloader"
"github.com/iyear/tdl/pkg/kv"
"github.com/iyear/tdl/pkg/logger"
"github.com/iyear/tdl/pkg/storage"
@ -32,7 +31,7 @@ import (
)
type media struct {
*downloader.Item
*tmedia.Media
MIME string
}
@ -145,7 +144,7 @@ func handler(h func(w http.ResponseWriter, r *http.Request) error) http.Handler
}
func convItem(msg *tg.Message) (*media, error) {
item, ok := tmedia.GetMedia(msg)
md, ok := tmedia.GetMedia(msg)
if !ok {
return nil, errors.New("message is not a media")
}
@ -163,7 +162,7 @@ func convItem(msg *tg.Message) (*media, error) {
}
return &media{
Item: item,
MIME: mime,
Media: md,
MIME: mime,
}, nil
}

View File

@ -126,9 +126,10 @@ func (iter *Iter) item(ctx context.Context, i, j int) (*downloader.Item, error)
}
item.Name = buf.String()
item.ID = iter.ij2n(i, j)
return item, nil
return &downloader.Item{
ID: iter.ij2n(i, j),
Media: item,
}, nil
}
func (iter *Iter) Finish(_ context.Context, id int) error {

View File

@ -4,7 +4,7 @@ import (
"context"
"errors"
"github.com/gotd/td/tg"
"github.com/iyear/tdl/pkg/tmedia"
)
var ErrSkip = errors.New("skip")
@ -16,9 +16,6 @@ type Iter interface {
}
type Item struct {
ID int // unique in iter
InputFileLoc tg.InputFileLocationClass
Name string
Size int64
DC int
ID int // unique in iter
*tmedia.Media
}

View File

@ -5,17 +5,15 @@ import (
"github.com/gabriel-vasile/mimetype"
"github.com/gotd/td/tg"
"github.com/iyear/tdl/pkg/downloader"
)
func GetDocumentInfo(doc *tg.MessageMediaDocument) (*downloader.Item, bool) {
func GetDocumentInfo(doc *tg.MessageMediaDocument) (*Media, bool) {
d, ok := doc.Document.(*tg.Document)
if !ok {
return nil, false
}
return &downloader.Item{
return &Media{
InputFileLoc: &tg.InputDocumentFileLocation{
ID: d.ID,
AccessHash: d.AccessHash,

View File

@ -2,11 +2,16 @@ package tmedia
import (
"github.com/gotd/td/tg"
"github.com/iyear/tdl/pkg/downloader"
)
func GetMedia(msg tg.MessageClass) (*downloader.Item, bool) {
type Media struct {
InputFileLoc tg.InputFileLocationClass
Name string
Size int64
DC int
}
func GetMedia(msg tg.MessageClass) (*Media, bool) {
mm, ok := msg.(*tg.Message)
if !ok {
return nil, false

View File

@ -4,11 +4,9 @@ import (
"strconv"
"github.com/gotd/td/tg"
"github.com/iyear/tdl/pkg/downloader"
)
func GetPhotoInfo(photo *tg.MessageMediaPhoto) (*downloader.Item, bool) {
func GetPhotoInfo(photo *tg.MessageMediaPhoto) (*Media, bool) {
p, ok := photo.Photo.(*tg.Photo)
if !ok {
return nil, false
@ -18,7 +16,7 @@ func GetPhotoInfo(photo *tg.MessageMediaPhoto) (*downloader.Item, bool) {
if !ok {
return nil, false
}
return &downloader.Item{
return &Media{
InputFileLoc: &tg.InputPhotoFileLocation{
ID: p.ID,
AccessHash: p.AccessHash,