-
-
Notifications
You must be signed in to change notification settings - Fork 118
/
helpers.go
103 lines (84 loc) · 2.65 KB
/
helpers.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
92
93
94
95
96
97
98
99
100
101
102
103
package uploader
import (
"bytes"
"context"
"io"
"io/fs"
"net/url"
"os"
"path/filepath"
"go.uber.org/multierr"
"golang.org/x/xerrors"
"github.com/gotd/td/telegram/uploader/source"
"github.com/gotd/td/tg"
)
// File is file abstraction.
type File interface {
Stat() (os.FileInfo, error)
io.Reader
}
// FromFile uploads given File.
// NB: FromFile does not close given file.
func (u *Uploader) FromFile(ctx context.Context, f File) (tg.InputFileClass, error) {
info, err := f.Stat()
if err != nil {
return nil, xerrors.Errorf("stat: %w", err)
}
return u.Upload(ctx, NewUpload(info.Name(), f, info.Size()))
}
// FromPath uploads file from given path.
func (u *Uploader) FromPath(ctx context.Context, path string) (tg.InputFileClass, error) {
return u.FromFS(ctx, osFS{}, path)
}
type osFS struct{}
func (o osFS) Open(name string) (fs.File, error) {
return os.Open(filepath.Clean(name))
}
// FromFS uploads file from fs using given path.
func (u *Uploader) FromFS(ctx context.Context, filesystem fs.FS, path string) (_ tg.InputFileClass, err error) {
f, err := filesystem.Open(path)
if err != nil {
return nil, xerrors.Errorf("open: %w", err)
}
defer func() {
multierr.AppendInto(&err, f.Close())
}()
return u.FromFile(ctx, f)
}
// FromReader uploads file from given io.Reader.
// NB: totally stream should not exceed the limit for
// small files (10 MB as docs says, may be a bit bigger).
func (u *Uploader) FromReader(ctx context.Context, name string, f io.Reader) (tg.InputFileClass, error) {
return u.Upload(ctx, NewUpload(name, f, -1))
}
// FromBytes uploads file from given byte slice.
func (u *Uploader) FromBytes(ctx context.Context, name string, b []byte) (tg.InputFileClass, error) {
return u.Upload(ctx, NewUpload(name, bytes.NewReader(b), int64(len(b))))
}
// FromURL uses given source to upload to Telegram.
func (u *Uploader) FromURL(ctx context.Context, rawURL string) (_ tg.InputFileClass, rerr error) {
return u.FromSource(ctx, u.src, rawURL)
}
// FromSource uses given source and URL to fetch data and upload it to Telegram.
func (u *Uploader) FromSource(ctx context.Context, src source.Source, rawURL string) (_ tg.InputFileClass, rerr error) {
parsed, err := url.Parse(rawURL)
if err != nil {
return nil, xerrors.Errorf("parse url %q: %w", rawURL, err)
}
f, err := src.Open(ctx, parsed)
if err != nil {
return nil, xerrors.Errorf("open %q: %w", rawURL, err)
}
defer func() {
multierr.AppendInto(&rerr, f.Close())
}()
name := f.Name()
if name == "" {
return nil, xerrors.Errorf("invalid name %q got from %q", name, rawURL)
}
size := f.Size()
if size < 0 {
size = -1
}
return u.Upload(ctx, NewUpload(f.Name(), f, size))
}