-
Notifications
You must be signed in to change notification settings - Fork 120
/
file.go
94 lines (79 loc) · 2.58 KB
/
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
package gotgbot
import (
"encoding/json"
"errors"
"io"
)
// InputFile (https://core.telegram.org/bots/api#inputfile)
//
// This object represents the contents of a file to be uploaded.
// Must be posted using multipart/form-data in the usual way that files are uploaded via the browser.
type InputFile interface {
InputFileOrString
justFiles()
}
// InputFileOrString (https://core.telegram.org/bots/api#inputfile)
//
// This object represents the contents of a file to be uploaded, or a publicly accessible URL to be reused.
// Files must be posted using multipart/form-data in the usual way that files are uploaded via the browser.
type InputFileOrString interface {
Attach(name string, data map[string]FileReader) error
getValue() string
}
var (
_ InputFileOrString = &FileReader{}
_ InputFile = &FileReader{}
)
type FileReader struct {
Name string
Data io.Reader
value string
}
func (f *FileReader) MarshalJSON() ([]byte, error) {
return json.Marshal(f.getValue())
}
var ErrAttachmentKeyAlreadyExists = errors.New("key already exists")
func (f *FileReader) justFiles() {}
func (f *FileReader) Attach(key string, data map[string]FileReader) error {
if f.Data == nil {
// if no data, this must be a string; nothing to "attach".
return nil
}
if _, ok := data[key]; ok {
return ErrAttachmentKeyAlreadyExists
}
f.value = "attach://" + key
data[key] = *f
return nil
}
// getValue returns the file attach reference for the relevant multipart form.
// Make sure to only call getValue after having called Attach(), to ensure any files have been included.
func (f *FileReader) getValue() string {
return f.value
}
// InputFileByURL is used to send a file on the internet via a publicly accessible HTTP URL.
func InputFileByURL(url string) InputFileOrString {
return &FileReader{value: url}
}
// InputFileByID is used to send a file that is already present on telegram's servers, using its telegram file_id.
func InputFileByID(fileID string) InputFileOrString {
return &FileReader{value: fileID}
}
// InputFileByReader is used to send a file by a reader interface; such as a filehandle from os.Open(), or from a byte
// buffer.
//
// For example:
//
// f, err := os.Open("some_file.go")
// if err != nil {
// return fmt.Errorf("failed to open file: %w", err)
// }
//
// m, err := b.SendDocument(<chat_id>, gotgbot.InputFileByReader("source.go", f), nil)
//
// Or
//
// m, err := b.SendDocument(<chat_id>, gotgbot.InputFileByReader("file.txt", strings.NewReader("Some file contents")), nil)
func InputFileByReader(name string, r io.Reader) InputFile {
return &FileReader{Name: name, Data: r}
}