-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
upload.go
91 lines (73 loc) · 2.72 KB
/
upload.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package message
import (
"context"
"github.com/go-faster/errors"
"github.com/gotd/td/tg"
)
// Upload creates new UploadBuilder to upload and send attachments.
// Given option will be called only once, even if you call upload functions.
func (b *Builder) Upload(upd UploadOption) *UploadBuilder {
return &UploadBuilder{
builder: b.copy(),
option: upd,
}
}
// UploadBuilder is an attachment uploading helper.
type UploadBuilder struct {
builder *Builder
option UploadOption
}
func (u *UploadBuilder) file(ctx context.Context) (tg.InputFileClass, error) {
return u.option.apply(ctx, uploadBuilder{upload: u.builder.sender.uploader})
}
// AsInputFile uploads and returns uploaded file entity.
func (u *UploadBuilder) AsInputFile(ctx context.Context) (tg.InputFileClass, error) {
return u.file(ctx)
}
func (u *UploadBuilder) send(
ctx context.Context,
using func(ctx context.Context, file tg.InputFileClass, caption ...StyledTextOption) (tg.UpdatesClass, error),
caption []StyledTextOption,
) (tg.UpdatesClass, error) {
f, err := u.file(ctx)
if err != nil {
return nil, errors.Wrap(err, "upload")
}
return using(ctx, f, caption...)
}
// Photo uploads and sends photo.
func (u *UploadBuilder) Photo(ctx context.Context, caption ...StyledTextOption) (tg.UpdatesClass, error) {
return u.send(ctx, u.builder.UploadedPhoto, caption)
}
// Audio uploads and sends audio file.
func (u *UploadBuilder) Audio(ctx context.Context, caption ...StyledTextOption) (tg.UpdatesClass, error) {
return u.send(ctx, u.builder.Audio, caption)
}
// Voice uploads and sends voice message.
func (u *UploadBuilder) Voice(ctx context.Context) (tg.UpdatesClass, error) {
f, err := u.file(ctx)
if err != nil {
return nil, errors.Wrap(err, "upload")
}
return u.builder.Voice(ctx, f)
}
// Video uploads and sends video.
func (u *UploadBuilder) Video(ctx context.Context, caption ...StyledTextOption) (tg.UpdatesClass, error) {
return u.send(ctx, u.builder.Video, caption)
}
// RoundVideo uploads and sends round video.
func (u *UploadBuilder) RoundVideo(ctx context.Context, caption ...StyledTextOption) (tg.UpdatesClass, error) {
return u.send(ctx, u.builder.RoundVideo, caption)
}
// GIF uploads and sends gif file.
func (u *UploadBuilder) GIF(ctx context.Context, caption ...StyledTextOption) (tg.UpdatesClass, error) {
return u.send(ctx, u.builder.GIF, caption)
}
// Sticker uploads and sends sticker.
func (u *UploadBuilder) Sticker(ctx context.Context, caption ...StyledTextOption) (tg.UpdatesClass, error) {
return u.send(ctx, u.builder.UploadedSticker, caption)
}
// File uploads and sends plain file.
func (u *UploadBuilder) File(ctx context.Context, caption ...StyledTextOption) (tg.UpdatesClass, error) {
return u.send(ctx, u.builder.File, caption)
}