-
Notifications
You must be signed in to change notification settings - Fork 11
/
input_file.go
112 lines (97 loc) · 2.28 KB
/
input_file.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
104
105
106
107
108
109
110
111
112
package tg
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
)
// InputFile represents the file that should be uploaded to the telegram.
type InputFile struct {
// Name of file
Name string
// Body of file
Body io.Reader
addr string
}
func (file *InputFile) MarshalJSON() ([]byte, error) {
if file.addr != "" {
return json.Marshal(file.addr)
}
return nil, fmt.Errorf("can't marshal InputFile without address")
}
// WithName creates new InputFile with overridden name.
func (file InputFile) WithName(name string) InputFile {
file.Name = name
return file
}
// Ptr returns pointer to InputFile. Helper method.
func (file InputFile) Ptr() *InputFile {
return &file
}
// Close closes body, if body impliments io.Closer.
func (file InputFile) Close() error {
closer, ok := file.Body.(io.Closer)
if ok {
return closer.Close()
}
return nil
}
// NewInputFile creates new InputFile with given name and body.
func NewInputFile(name string, body io.Reader) InputFile {
return InputFile{
Name: name,
Body: body,
}
}
// NewInputFileFromBytes creates new InputFile with given name and bytes slice.
//
// Example:
// file := NewInputFileBytes("test.txt", []byte("test, test, test..."))
func NewInputFileBytes(name string, body []byte) InputFile {
return NewInputFile(name, bytes.NewReader(body))
}
// NewInputFileLocal creates the InputFile from provided local file path.
// This method just open file by provided path.
// So, you should close it AFTER send.
//
// Example:
//
// file, err := NewInputFileLocal("test.png")
// if err != nil {
// return err
// }
// defer close()
//
func NewInputFileLocal(path string) (InputFile, error) {
file, err := os.Open(path)
if err != nil {
return InputFile{}, err
}
return NewInputFile(
filepath.Base(path),
file,
), nil
}
// NewInputFileFS creates the InputFile from provided FS and file path.
//
// Usage:
// //go:embed assets/*
// var assets embed.FS
// file, err := NewInputFileFS(assets, "images/test.png")
// if err != nil {
// return err
// }
// defer file.Close()
func NewInputFileFS(fs fs.FS, path string) (InputFile, error) {
file, err := fs.Open(path)
if err != nil {
return InputFile{}, fmt.Errorf("open file: %w", err)
}
return NewInputFile(
filepath.Base(path),
file,
), nil
}