-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiles.go
130 lines (112 loc) · 3.08 KB
/
files.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package telebot
import (
"fmt"
"io"
"os"
)
// File object represents any sort of file.
type File struct {
FileID string `json:"file_id"`
UniqueID string `json:"file_unique_id"`
FileSize int64 `json:"file_size"`
// FilePath is used for files on Telegram server.
FilePath string `json:"file_path"`
// FileLocal is used for files on local file system.
FileLocal string `json:"file_local"`
// FileURL is used for file on the internet.
FileURL string `json:"file_url"`
// FileReader is used for file backed with io.Reader.
FileReader io.Reader `json:"-"`
fileName string
}
// FromDisk constructs a new local (on-disk) file object.
//
// Note, it returns File, not *File for a very good reason:
// in telebot, File is pretty much an embeddable struct,
// so upon uploading media you'll need to set embedded File
// with something. NewFile() returning File makes it a one-liner.
//
// photo := &tele.Photo{File: tele.FromDisk("chicken.jpg")}
func FromDisk(filename string) File {
return File{FileLocal: filename}
}
// FromURL constructs a new file on provided HTTP URL.
//
// Note, it returns File, not *File for a very good reason:
// in telebot, File is pretty much an embeddable struct,
// so upon uploading media you'll need to set embedded File
// with something. NewFile() returning File makes it a one-liner.
//
// photo := &tele.Photo{File: tele.FromURL("https://site.com/picture.jpg")}
func FromURL(url string) File {
return File{FileURL: url}
}
// FromReader constructs a new file from io.Reader.
//
// Note, it returns File, not *File for a very good reason:
// in telebot, File is pretty much an embeddable struct,
// so upon uploading media you'll need to set embedded File
// with something. NewFile() returning File makes it a one-liner.
//
// photo := &tele.Photo{File: tele.FromReader(bytes.NewReader(...))}
func FromReader(reader io.Reader) File {
return File{FileReader: reader}
}
// FromFileID constructs a new file from file_id.
func FromFileID(fileID string) File {
return File{FileID: fileID}
}
func (f *File) stealRef(g *File) {
if g.OnDisk() {
f.FileLocal = g.FileLocal
}
if g.FileURL != "" {
f.FileURL = g.FileURL
}
}
// InCloud tells whether the file is present on Telegram servers.
func (f *File) InCloud() bool {
return f.FileID != ""
}
// OnDisk will return true if file is present on disk.
func (f *File) OnDisk() bool {
_, err := os.Stat(f.FileLocal)
return err == nil
}
func (f *File) GetFileID() string {
return f.FileID
}
func (f *File) GetFileSize() int64 {
return f.FileSize
}
func (f *File) GetFilePath() string {
return f.FilePath
}
func (f *File) GetFileLocal() string {
return f.FileLocal
}
func (f *File) GetFileURL() string {
return f.FileURL
}
func (f *File) GetFileName() string {
return f.fileName
}
func (f *File) GetFileReader() io.Reader {
if f.OnDisk() {
file, err := os.Open(f.FileLocal)
if err != nil {
return nil
}
return file
}
return f.FileReader
}
func (f *File) ReflectType() string {
return fmt.Sprintf("%T", f)
}
func (f *File) Type() string {
return "File"
}
func (f *File) SetFileName(fileName string) {
f.fileName = fileName
}