-
-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #585 from tdakkota/feat/fileid-creation-helpers
feat(fileid): add some creation helpers
- Loading branch information
Showing
6 changed files
with
327 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package fileid_test | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-faster/errors" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zaptest" | ||
|
||
"github.com/gotd/td/fileid" | ||
"github.com/gotd/td/internal/testutil" | ||
"github.com/gotd/td/telegram" | ||
"github.com/gotd/td/telegram/downloader" | ||
) | ||
|
||
func runBot(ctx context.Context, token, fileID string, logger *zap.Logger) error { | ||
bot := telegram.NewClient(telegram.TestAppID, telegram.TestAppHash, telegram.Options{ | ||
Logger: logger, | ||
}) | ||
d := downloader.NewDownloader() | ||
|
||
return bot.Run(ctx, func(ctx context.Context) error { | ||
auth, err := bot.Auth().Bot(ctx, token) | ||
if err != nil { | ||
return errors.Wrap(err, "auth bot") | ||
} | ||
user, ok := auth.User.AsNotEmpty() | ||
if !ok { | ||
return errors.Errorf("unexpected type %T", auth.User) | ||
} | ||
_ = user | ||
|
||
decoded, err := fileid.DecodeFileID(fileID) | ||
if err != nil { | ||
return errors.Wrap(err, "decode FileID") | ||
} | ||
|
||
loc, ok := decoded.AsInputFileLocation() | ||
if !ok { | ||
return errors.Errorf("can't map %q", fileID) | ||
} | ||
|
||
filename := "file.dat" | ||
switch decoded.Type { | ||
case fileid.Thumbnail, fileid.ProfilePhoto, fileid.Photo: | ||
filename = "file.jpg" | ||
case fileid.Video, | ||
fileid.Animation, | ||
fileid.VideoNote: | ||
filename = "file.mp4" | ||
case fileid.Audio: | ||
filename = "file.mp3" | ||
case fileid.Voice: | ||
filename = "file.ogg" | ||
case fileid.Sticker: | ||
filename = "file.png" | ||
} | ||
|
||
if _, err := d.Download(bot.API(), loc).ToPath(ctx, filename); err != nil { | ||
return errors.Wrap(err, "download") | ||
} | ||
return nil | ||
}) | ||
} | ||
|
||
func TestExternalE2ECheckFileID(t *testing.T) { | ||
testutil.SkipExternal(t) | ||
token := os.Getenv("GOTD_E2E_BOT_TOKEN") | ||
if token == "" { | ||
t.Skip("Set GOTD_E2E_BOT_TOKEN env to run test.") | ||
} | ||
fileID := os.Getenv("GOTD_E2E_FILE_ID") | ||
if fileID == "" { | ||
t.Skip("Set GOTD_E2E_FILE_ID env to run test.") | ||
} | ||
logger := zaptest.NewLogger(t) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute) | ||
defer cancel() | ||
|
||
if err := runBot(ctx, token, fileID, logger.Named("bot")); err != nil { | ||
t.Error(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package fileid | ||
|
||
import "github.com/gotd/td/tg" | ||
|
||
// FromDocument creates FileID from tg.Document. | ||
func FromDocument(doc *tg.Document) FileID { | ||
fileID := FileID{ | ||
Type: DocumentAsFile, | ||
DC: doc.DCID, | ||
ID: doc.ID, | ||
AccessHash: doc.AccessHash, | ||
FileReference: doc.FileReference, | ||
} | ||
for _, attr := range doc.Attributes { | ||
switch attr := attr.(type) { | ||
case *tg.DocumentAttributeAnimated: | ||
fileID.Type = Animation | ||
case *tg.DocumentAttributeSticker: | ||
fileID.Type = Sticker | ||
case *tg.DocumentAttributeVideo: | ||
fileID.Type = Video | ||
if attr.RoundMessage { | ||
fileID.Type = VideoNote | ||
} | ||
case *tg.DocumentAttributeAudio: | ||
fileID.Type = Audio | ||
if attr.Voice { | ||
fileID.Type = Voice | ||
} | ||
} | ||
} | ||
return fileID | ||
} | ||
|
||
// FromPhoto creates FileID from tg.Photo. | ||
func FromPhoto(photo *tg.Photo, thumbType rune) FileID { | ||
return FileID{ | ||
Type: Photo, | ||
DC: photo.DCID, | ||
ID: photo.ID, | ||
AccessHash: photo.AccessHash, | ||
FileReference: photo.FileReference, | ||
PhotoSizeSource: PhotoSizeSource{ | ||
Type: PhotoSizeSourceThumbnail, | ||
FileType: Photo, | ||
ThumbnailType: thumbType, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package fileid | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/gotd/td/tg" | ||
) | ||
|
||
func TestFromDocument(t *testing.T) { | ||
doc := func(attrs ...tg.DocumentAttributeClass) *tg.Document { | ||
return &tg.Document{ | ||
ID: 1, | ||
AccessHash: 2, | ||
FileReference: []byte{3}, | ||
DCID: 4, | ||
Attributes: attrs, | ||
} | ||
} | ||
fileID := func(typ Type) FileID { | ||
return FileID{ | ||
Type: typ, | ||
ID: 1, | ||
AccessHash: 2, | ||
FileReference: []byte{3}, | ||
DC: 4, | ||
} | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
doc *tg.Document | ||
want FileID | ||
}{ | ||
{"File", doc(), fileID(DocumentAsFile)}, | ||
{"Animation", doc(&tg.DocumentAttributeAnimated{}), fileID(Animation)}, | ||
{"Sticker", doc(&tg.DocumentAttributeSticker{}), fileID(Sticker)}, | ||
{"Video", doc(&tg.DocumentAttributeVideo{}), fileID(Video)}, | ||
{"VideoNote", doc(&tg.DocumentAttributeVideo{RoundMessage: true}), fileID(VideoNote)}, | ||
{"Audio", doc(&tg.DocumentAttributeAudio{}), fileID(Audio)}, | ||
{"Voice", doc(&tg.DocumentAttributeAudio{Voice: true}), fileID(Voice)}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
require.Equal(t, tt.want, FromDocument(tt.doc)) | ||
}) | ||
} | ||
} | ||
|
||
func TestFromPhoto(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
photo *tg.Photo | ||
size rune | ||
want FileID | ||
}{ | ||
{ | ||
"Photo", | ||
&tg.Photo{ | ||
ID: 1, | ||
AccessHash: 2, | ||
FileReference: []byte{3}, | ||
DCID: 4, | ||
}, | ||
'x', | ||
FileID{ | ||
Type: Photo, | ||
ID: 1, | ||
AccessHash: 2, | ||
FileReference: []byte{3}, | ||
DC: 4, | ||
PhotoSizeSource: PhotoSizeSource{ | ||
Type: PhotoSizeSourceThumbnail, | ||
FileType: Photo, | ||
ThumbnailType: 'x', | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
require.Equal(t, tt.want, FromPhoto(tt.photo, tt.size)) | ||
}) | ||
} | ||
} |
Oops, something went wrong.