2015-07-03 18:54:40 +00:00
|
|
|
package telebot
|
|
|
|
|
|
|
|
import (
|
2018-10-11 11:39:07 +00:00
|
|
|
"io"
|
2015-07-03 18:54:40 +00:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
// File object represents any sort of file.
|
|
|
|
type File struct {
|
2015-07-06 16:27:10 +00:00
|
|
|
FileID string `json:"file_id"`
|
2020-04-26 16:40:11 +00:00
|
|
|
UniqueID string `json:"file_unique_id"`
|
2015-07-03 18:54:40 +00:00
|
|
|
FileSize int `json:"file_size"`
|
2017-11-18 18:16:16 +00:00
|
|
|
|
|
|
|
// file on telegram server https://core.telegram.org/bots/api#file
|
2016-09-27 12:04:13 +00:00
|
|
|
FilePath string `json:"file_path"`
|
2015-07-03 18:54:40 +00:00
|
|
|
|
2017-11-18 18:16:16 +00:00
|
|
|
// file on local file system.
|
2017-11-25 14:22:13 +00:00
|
|
|
FileLocal string `json:"file_local"`
|
2017-11-18 18:16:16 +00:00
|
|
|
|
|
|
|
// file on the internet
|
2017-11-25 14:22:13 +00:00
|
|
|
FileURL string `json:"file_url"`
|
2018-10-11 11:39:07 +00:00
|
|
|
|
|
|
|
// file backed with io.Reader
|
|
|
|
FileReader io.Reader `json:"-"`
|
2017-11-18 18:16:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 := &tb.Photo{File: tb.FromDisk("chicken.jpg")}
|
|
|
|
//
|
|
|
|
func FromDisk(filename string) File {
|
|
|
|
return File{FileLocal: filename}
|
2015-07-03 18:54:40 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 18:47:04 +00:00
|
|
|
// FromURL constructs a new file on provided HTTP URL.
|
2015-07-03 18:54:40 +00:00
|
|
|
//
|
2017-11-18 18:16:16 +00:00
|
|
|
// 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 := &tb.Photo{File: tb.FromURL("https://site.com/picture.jpg")}
|
|
|
|
//
|
2017-11-25 14:22:13 +00:00
|
|
|
func FromURL(url string) File {
|
|
|
|
return File{FileURL: url}
|
2017-11-18 18:16:16 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 11:39:07 +00:00
|
|
|
// 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 := &tb.Photo{File: tb.FromReader(bytes.NewReader(...))}
|
|
|
|
//
|
|
|
|
func FromReader(reader io.Reader) File {
|
|
|
|
return File{FileReader: reader}
|
|
|
|
}
|
|
|
|
|
2017-11-18 18:33:20 +00:00
|
|
|
func (f *File) stealRef(g *File) {
|
|
|
|
if g.OnDisk() {
|
|
|
|
f.FileLocal = g.FileLocal
|
2015-07-03 18:54:40 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 14:22:13 +00:00
|
|
|
if g.FileURL != "" {
|
2017-11-18 18:33:20 +00:00
|
|
|
f.FileURL = g.FileURL
|
|
|
|
}
|
2015-07-03 18:54:40 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 18:16:16 +00:00
|
|
|
// InCloud tells whether the file is present on Telegram servers.
|
|
|
|
func (f *File) InCloud() bool {
|
2015-10-13 14:38:15 +00:00
|
|
|
return f.FileID != ""
|
2015-07-03 18:54:40 +00:00
|
|
|
}
|
2015-07-06 16:58:54 +00:00
|
|
|
|
2017-11-18 18:16:16 +00:00
|
|
|
// OnDisk will return true if file is present on disk.
|
|
|
|
func (f *File) OnDisk() bool {
|
2020-06-09 20:28:28 +00:00
|
|
|
_, err := os.Stat(f.FileLocal)
|
|
|
|
return err == nil
|
2015-07-06 16:58:54 +00:00
|
|
|
}
|