Skip to content

Commit

Permalink
refactor(tmedia): extract item to self struct
Browse files Browse the repository at this point in the history
  • Loading branch information
iyear committed Nov 7, 2023
1 parent 892dcdd commit eb9b0c5
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 25 deletions.
9 changes: 4 additions & 5 deletions app/dl/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -32,7 +31,7 @@ import (
)

type media struct {
*downloader.Item
*tmedia.Media
MIME string
}

Expand Down Expand Up @@ -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")
}
Expand All @@ -163,7 +162,7 @@ func convItem(msg *tg.Message) (*media, error) {
}

return &media{
Item: item,
MIME: mime,
Media: md,
MIME: mime,
}, nil
}
7 changes: 4 additions & 3 deletions app/internal/dliter/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 3 additions & 6 deletions pkg/downloader/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"errors"

"github.com/gotd/td/tg"
"github.com/iyear/tdl/pkg/tmedia"
)

var ErrSkip = errors.New("skip")
Expand All @@ -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

Check failure on line 19 in pkg/downloader/iter.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/iyear/tdl) -s dot --custom-order (gci)
*tmedia.Media
}
6 changes: 2 additions & 4 deletions pkg/tmedia/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 8 additions & 3 deletions pkg/tmedia/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions pkg/tmedia/photo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down

0 comments on commit eb9b0c5

Please sign in to comment.